GithubHelp home page GithubHelp logo

tufik2 / msc-lens Goto Github PK

View Code? Open in Web Editor NEW

This project forked from meistudioli/msc-lens

0.0 0.0 0.0 54 KB

Google Lens is famous and powerful for image search. Users could use its fancy UI for image selection. I like its effect and wanna to apply it for services I owned. That's also the main reason I wrapped it into <msc-lens />. Developers could received exactly the image which users just selected and do some analytics and recommend. Then render results through event which <msc-lens /> provided.

License: MIT License

HTML 6.33% JavaScript 93.67%

msc-lens's Introduction

msc-lens

Published on webcomponents.org DeepScan grade

Google Lens is famous and powerful for image search. Users could use its fancy UI for image selection. I like its effect and wanna to apply it for services I owned. That's also the main reason I wrapped it into <msc-lens />.

Developers could received exactly the image which users just selected and do some analytics and recommend. Then render results through event which <msc-lens /> provided.

<msc-lens />

Basic Usage

<msc-lens /> is a web component. All we need to do is put the required script into your HTML document. Then follow <msc-lens />'s html structure and everything will be all set.

  • Required Script
<script
  type="module"
  src="https://your-domain/wc-msc-lens.js">        
</script>
  • Structure

Put img[slot="msc-lens-vision"] inside <msc-lens /> as its child. It will use it as source.

<msc-lens>
  <script type="application/json">
    {
      "sensorsize": 28,
      "active": false,
      "delay": 500,
      "format": "blob",
      "webservice": {
        "uri": "https://your-domain/analytic",
        "fieldName": "lens",
        "params": {
          "origin": "extra param you like",
          "id": "extra param you like"
        }
      }
    }
  </script>
  <img src="https://picsum.photos/id/635/1000/670" slot="msc-lens-vision" />
</msc-lens>

Otherwise, developers could also choose remoteconfig to fetch config for <msc-lens />.

<msc-lens remoteconfig="https://your-domain/api-path">
  <img src="https://picsum.photos/id/635/1000/670" slot="msc-lens-vision" />
</msc-lens>

JavaScript Instantiation

<msc-lens /> could also use JavaScript to create DOM element. Here comes some examples.

<script type="module">
import { MscLens } from 'https://your-domain/wc-msc-lens.js';

const content = document.querySelector('img[slot="msc-lens-vision"]');

// use DOM api
const nodeA = document.createElement('msc-lens');
nodeA.appendChild(content.cloneNode(true));
document.body.appendChild(nodeA);
nodeA.webservice = {
  uri: 'https://your-domain/analytic',
  fieldName: 'lens',
  params: {
    origin: 'extra param you like',
    id: 'extra param you like'
  }
};

// new instance with Class
const nodeB = new MscLens();
nodeB.appendChild(content.cloneNode(true));
document.body.appendChild(nodeB);
nodeB.webservice = {
  uri: 'https://your-domain/analytic',
  fieldName: 'lens',
  params: {
    origin: 'extra param you like',
    id: 'extra param you like'
  }
};

// new instance with Class & default config
const config = {
  sensorsize: 40,
  webservice: {
    uri: 'https://your-domain/analytic',
    fieldName: 'lens',
    params: {
      origin: 'extra param you like',
      id: 'extra param you like'
    }
  }
};
const nodeC = new MscLens(config);
nodeC.appendChild(content.cloneNode(true));
document.body.appendChild(nodeC);
</script>

Style Customization

<msc-lens /> uses CSS variables to style its interface. That means developer could easy change them into the lookup you like.

<style>
msc-lens {
  --msc-lens-overlay-color: rgba(0,0,0,.5);
  --msc-lens-sensor-color: rgba(255,255,255,1);
}
</style>

Attributes

<msc-lens /> supports some attributes to let it become more convenience & useful.

  • sensorsize

Set sersor size for <msc-lens />. Default is 28 (px).

<msc-lens
  sensorsize="28"
>
  <img src="https://picsum.photos/id/635/1000/670" slot="msc-lens-vision" />
</msc-lens>
  • active

Set active for <msc-lens />. It will switch to select mode once set. Default is false (not set).

<msc-lens
  active
>
  <img src="https://picsum.photos/id/635/1000/670" slot="msc-lens-vision" />
</msc-lens>
  • delay

Set delay for <msc-lens />. It will delay fetch web service once user finish select. Default is 500 (ms).

<msc-lens
  delay="500"
>
  <img src="https://picsum.photos/id/635/1000/670" slot="msc-lens-vision" />
</msc-lens>
  • format

Set image format for <msc-lens />. This attribute can only accept "blob" or "dataURL". Default is "blob".

<msc-lens
  format="blob"
>
  <img src="https://picsum.photos/id/635/1000/670" slot="msc-lens-vision" />
</msc-lens>
  • webservice

Set web service information for <msc-lens />. It should be JSON string. Developers could set urifieldName and extra params here.(uri must be full path)

<msc-lens
  webservice='{"uri":"https://your-domain/analytic","fieldName":"lens","params":{"origin":"extra param you like","id":"extra param you like"}}'
>
  <img src="https://picsum.photos/id/635/1000/670" slot="msc-lens-vision" />
</msc-lens>

Properties

Property Name Type Description
sensorsize Number Getter / Setter for senser size. Developers could use this property to setup sensor size.
active Boolean Getter / Setter for active. It will switch to select / normal mode.
delay Number Getter / Setter for delay. It will delay fetch web service once user finish select.
format String Getter / Setter for format. It will set image format. This property can only accept "blob" or "dataURL". Default is "blob".
webservice Object Getter / Setter for web service information. Developers could set urifieldName and extra params here.(uri must be full path)

Method

Method Signature Description
toggle([force]) Toggle <msc-lens /> select or normal mode. When argument is present: If the argument is true, <msc-lens /> will switch to select mode, and if it is false, back to normal.

Event

Event Signature Description
msc-lens-switch Fired when <msc-lens /> mode switched. Developers could get active through event.detail.
msc-lens-capture Fired when <msc-lens /> captures image selection. Developers could get image through event.detail.
msc-lens-process Fired when <msc-lens /> fetch web service.
msc-lens-result Fired when <msc-lens /> finished web service fetching. Developers could get result through event.detail.

Reference

msc-lens's People

Contributors

meistudioli avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.