@wannaby/wanna-sdk 2.2.2 → 2.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,52 +1,548 @@
1
- # How to use
1
+ # Get started with WANNA SDK
2
2
 
3
- ## Install library
3
+ WANNA SDK enhances your website with virtual try-on experience for sneakers and watches, which works directly in a mobile web browser without installing any apps.
4
4
 
5
- 1) Add the library localy in package.json
6
- ```js
7
- "wanna-sdk": "file:~Downloads/wanna-sdk"
5
+ <!-- TOC -->
6
+ - [Requirements and limitations](#requirements-and-limitations)
7
+ - [License](#license)
8
+ - [Installation](#installation)
9
+ - [Usage](#usage)
10
+ - [Example](#example)
11
+ <!-- /TOC -->
12
+
13
+ ## Requirements and limitations
14
+
15
+ Supported environments:
16
+ * iOS:
17
+ * Safari 14 and later
18
+ * Google Chrome on iOS 14.3 and later
19
+ * in-app browsers for most common messengers: WhatsApp, Facebook, Telegram, Snapchat, and Instagram
20
+ * Android:
21
+ * Google Chrome 88 and later
22
+ * in-app browsers for most common messengers: WhatsApp, Facebook, Telegram, and Instagram
23
+
24
+ ## License
25
+
26
+ To work with WANNA SDK you need a license key. Contact our sales representative at account@wanna.fashion to get one.
27
+
28
+ ## Installation
29
+
30
+ 1) Add `@wannaby/wanna-sdk` package to your project dependencies
31
+ ```
32
+ yarn add @wannaby/wanna-sdk
8
33
  ```
9
- OR
10
34
 
11
- 1) Add the library remotely in package.json
35
+ 2) Add the following rules into your Webpack configuration:
12
36
  ```js
13
- "wanna-sdk": "https:// ... .tar.gz"
37
+ rules: [
38
+ ...
39
+ {
40
+ test: /@wannaby\/wanna-sdk\/.*iframe.html$/,
41
+ loader: 'file-loader',
42
+ },
43
+ {
44
+ test: /@wannaby\/wanna-sdk\/.*core.js$/,
45
+ loader: 'file-loader',
46
+ options: {
47
+ name: 'core.js',
48
+ },
49
+ },
50
+ ]
14
51
  ```
15
52
 
16
- ## Import files
17
- TODO: add webpack config to use the library without importing /core manualy (3rd line)
53
+ If instead of Webpack you use Rollup JS, or something similar, you have to add some rules like that to add core.js and iframe.html files into your public folder and get iframe.html path to import it in the example below.
54
+
18
55
 
19
- 2) Import wanna sdk files
56
+ *Note:* Some platform providers will restrict the use of the hosted frame. Check the headers for the following settings:
57
+
58
+ * `frame-ancestors` in the CSP header set to `deny`, or to a host or scheme different from the page that runs WANNA SDK (see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/frame-ancestors)
59
+ * `X-Frame-options` set to `DENY` (see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Frame-Options)
60
+
61
+ These settings may prevent your using the hosted frame on other pages. In that case host our files elsewhere and specify the full paths to these files in **index.html** and the `iframeSrc` parameter of the `init` method.
62
+
63
+ ## Usage
64
+
65
+ 1) Import Wanna SDK files
20
66
  ```js
21
- import wannaSdk from 'wanna-sdk'
22
- import wannaSdkIframe from 'wanna-sdk/iframe'
23
- import 'wanna-sdk/core'
67
+ import wannaSdk from '@wannaby/wanna-sdk'
68
+ import wannaSdkIframe from '@wannaby/wanna-sdk/iframe.html'
69
+ import '@wannaby/wanna-sdk/core'
24
70
  ```
25
71
 
26
- 3) Init wanna sdk ussing imported iframe
72
+ 2) *(optional)* Check if the current environment supports WANNA SDK. It depends on the device and browser. You may want to hide the virtual try-on button in unsupported environments.<br>
27
73
  ```js
28
- wannaSdk.init({
74
+ await wanna.checkEnvironment()
75
+ ```
76
+
77
+ 3) Initialize the WANNA library using the `init` method. Pass in the element where the virtual try-on will be rendered, license string that authorizes your use of WANNA SDK, type of session (`wannaSdk.MODEL_TYPE_SNEAKER` or `wannaSdk.MODEL_TYPE_WATCH`), the link to the iframe, and optionally the iframe size (by default it takes up the whole container):
78
+ ```javascript
79
+ await wanna.init({
80
+ container: '<HTML_container_id>',
29
81
  iframeSrc: wannaSdkIframe,
30
- ...
82
+ type: wannaSdk.MODEL_TYPE_SNEAKER,
83
+ license: '<PUT_YOUR_LICENSE_STRING_HERE>',
31
84
  })
32
- ```
85
+ ```
86
+ The good time to initialize is when the user goes to the page that offers virtual try-on and you expect them to use it. Note that initialization will take up some processing resources and download traffic on the user's device. To prevent overuse of these resources, avoid initializing too early. On pages that are showing the whole range of content your website offers, wait until the user actually chooses virtual try-on.
33
87
 
34
- 4) Add webpack config for iframe.html.
88
+ 4) *(optional)* Call the `initVideo` method to check and request camera permissions, and start showing the video stream once they are granted. Call it at the same time as the `init` method or immediately afterwards.
89
+
90
+ The purpose of this step is to show the user some activity while the library is initializing. Once the video is initialized, you may display the `root` element that will have the raw camera output:
35
91
  ```js
36
- {
37
- test: /wanna-sdk\/.*iframe.html$/,
38
- loader: 'file-loader',
39
- },
92
+ wanna.initVideo().then(() => {
93
+ showElement('<HTML_container_id>')
94
+ })
40
95
  ```
41
- `NOTE: Check all webpack config that iframe.html is processing just by this config, not html-loader and etc!`
42
96
 
43
- 5) Add webpack config for core.js
97
+ 5) *(optional)* Download from the CDN the model that will be used for virtual try-on. This will reduce the time to start up the try-on experience, as the model will be loaded and ready when you go to the next step.<br>
44
98
  ```js
45
- {
46
- test: /wanna-sdk\/.*core.js$/,
47
- loader: 'file-loader',
48
- options: {
49
- name: 'core.js',
99
+ await wanna.downloadModel({ id: 'wanna01' })
100
+ ```
101
+
102
+ If you know the model ID — for example, because of starting up on the particular product's page—we recommend downloading the model immediately after the initialization completes, to save the user's time. If step 2, `init`, has completed before step 3, `initVideo`, just start downloading the model without waiting for the video.
103
+
104
+ 6) Start the try-on session. If you haven't done it beforehand, the method will first download the model, then render it on the screen.<br>
105
+ ```js
106
+ await wanna.downloadAndSetModel({ id: 'wanna01' })
107
+ ```
108
+
109
+ **Important:**
110
+
111
+ - If the user hasn't granted access to the camera, virtual try-on will not work. Handle the `NotAllowedError` to tell the user that camera access is required.
112
+ - For non-localhost addresses, HTTPS is required for correct operation of the camera.
113
+ - The SDK initialization can fail on unsupported devices. In this case, the `init` method will return an error. Handle errors using `try...catch` statement. Please use the code samples provided together with the SDK as a reference implementation.
114
+
115
+ ## Example
116
+
117
+ ```js
118
+ import wannaSdk from '@wannaby/wanna-sdk'
119
+ import wannaSdkIframe from '@wannaby/wanna-sdk/iframe.html'
120
+ import '@wannaby/wanna-sdk/core'
121
+
122
+ async function runExample () {
123
+ const canInit = await wannaSdk.checkEnvironment()
124
+
125
+ if (canInit) {
126
+ try {
127
+ await Promise.all([
128
+ wannaSdk.initVideo(),
129
+ wannaSdk.init({
130
+ container: '<HTML_container_id>',
131
+ iframeSrc: wannaSdkIframe,
132
+ type: 'sneakers',
133
+ license: '<PUT_YOUR_LICENSE_STRING_HERE>',
134
+ }),
135
+ ])
136
+ } catch {
137
+ console.error('Try-on not available')
138
+ return
139
+ }
140
+
141
+ try {
142
+ await wanna.downloadAndSetModel({ id: 'wanna01' })
143
+ } catch (error) {
144
+ if (error.name === 'NotAllowedError') {
145
+ // Ask user to allow camera access or check site settings
146
+ }
147
+ }
148
+ } else {
149
+ console.error('Environment does not fit requirements')
150
+ }
151
+ }
152
+
153
+ runExample()
154
+ ```
155
+
156
+ # WANNA SDK Web API Reference
157
+
158
+ This document describes JavaScript API of WANNA SDK. It enhances your website with augmented reality (AR) try-on experience for sneakers and watches, which works directly in a mobile web browser without installing any apps.
159
+
160
+ <!-- TOC -->
161
+ - [Methods](#methods)
162
+ - [checkEnvironment](#checkenvironment)
163
+ - [init](#init)
164
+ - [initVideo](#initvideo)
165
+ - [downloadModel](#downloadmodel)
166
+ - [downloadAndSetModel](#downloadandsetmodel)
167
+ - [getCamerasList](#getcameraslist)
168
+ - [changeCamera](#changecamera)
169
+ - [startPipeline](#startpipeline)
170
+ - [pausePipeline](#pausepipeline)
171
+ - [getPipelineStatus](#getpipelinestatus)
172
+ - [takePhoto](#takephoto)
173
+ - [destroy](#destroy)
174
+ - [Events](#events)
175
+ - [General](#general)
176
+ - [Resource loading](#resource-loading)
177
+ - [Camera](#camera)
178
+ - [Try-on object presence](#try-on-object-presence)
179
+ <!-- /TOC -->
180
+
181
+ ## Methods
182
+
183
+ The basic usage scenario requires you to `init`, then `downloadAndSetModel` to show the try-on and `destroy` the library when done.
184
+
185
+ ### checkEnvironment
186
+
187
+ Checks if the SDK can work in the current environment: device and browser.
188
+
189
+ ```javascript
190
+ checkEnvironment() => Promise<bool>
191
+ ```
192
+
193
+ #### Parameters
194
+
195
+ No input parameters.
196
+
197
+ #### Returns
198
+
199
+ A promise that resolves with a boolean value indicating if the current environment is supported.
200
+
201
+ ### init
202
+
203
+ Initializes the library, loading the resources required for try-on, except the 3D models. May be called several times with the same options.
204
+
205
+ ```javascript
206
+ init(options) => Promise
207
+
208
+ options: {
209
+ container:node|string,
210
+ license:string,
211
+ verbose?:bool=false,
212
+ type:string,
213
+ iframeSrc:string,
214
+ iframeSize?:{
215
+ width?:string|number='100%',
216
+ height?:string|number='100%',
217
+ modelsCacheSize?:number,
50
218
  },
51
- },
219
+ }
220
+ ```
221
+
222
+ #### Parameters
223
+
224
+ Initialization options:
225
+
226
+ * *container* — the DOM element in which the try-on session will be displayed, or its identifier
227
+ * *license* — the string with WANNA SDK license information
228
+ * *verbose* — (optional) specifies if the full log should be written; default value is `false`
229
+ * *type* — the kind of product to try on: `'sneakers'` or `'watch'`;
230
+ **Note:** the previously used `'sneaker'` type is deprecated
231
+ * *iframeSrc* — the path to **iframe.html**, found in the distribution pack; if it's at the same location as the page with the *container*, you don't have to specify this parameter
232
+ **Note:** the **core.js** script should be located at the same path as **iframe.html**
233
+ * *iframeSize* `{width?: string|number='100%', height?: string|number='100%'}` — (optional) the width and height of the frame where the try-on will be rendered; by default it's the size of the container
234
+ * *modelsCacheSize* — (optional) the limit to the number of 3D models that may be stored in cache; the default is 5
235
+
236
+ #### Returns
237
+
238
+ A promise that is resolved when the library is initialized successfully.
239
+
240
+ ### initVideo
241
+
242
+ Asks the user for the camera permission and starts showing the stream from camera. Call this method either after `init` or at the same time with it, because the promise this method returns will only be resolved if `init` has been called.
243
+
244
+ This method is designed to let you start showing the video as soon as possible. If you don't use it, the video stream will be shown when you call [`downloadAndSetModel`](#downloadandsetmodel) or [`startPipeline`](#startpipeline).
245
+
246
+ ```javascript
247
+ initVideo() => Promise
248
+ ```
249
+
250
+ #### Parameters
251
+
252
+ No input parameters.
253
+
254
+ #### Returns
255
+
256
+ A promise that is resolved when the required permissions are granted and the camera stream starts showing, but NOT if the `init` method wasn't called yet.
257
+
258
+ ### downloadModel
259
+
260
+ Downloads the model from the CDN.
261
+
262
+ ```javascript
263
+ downloadModel({id: string}) => Promise
264
+ ```
265
+
266
+ #### Parameters
267
+
268
+ * *id* — the unique identifier of the model, different for different colors
269
+
270
+ #### Returns
271
+
272
+ A promise that is resolved when the model is downloaded.
273
+
274
+ ### downloadAndSetModel
275
+
276
+ Downloads the model from the CDN and renders it on the try-on screen as soon as it's ready. If you have already loaded the model with the help of `downloadModel`, this method will work faster.
277
+
278
+ If you haven't started the pipeline yet, it will be started within this method call.
279
+
280
+ ```javascript
281
+ downloadAndSetModel({id: string}) => Promise
282
+ ```
283
+
284
+ #### Parameters
285
+
286
+ * *id* — the unique identifier of the model, different for different colors
287
+
288
+ #### Returns
289
+
290
+ A promise that is resolved when the model is downloaded.
291
+
292
+ ### getCamerasList
293
+
294
+ Lists all available device cameras that can be used to get the input video stream.
295
+
296
+ **Note:** the method only works on Android.
297
+
298
+ ```javascript
299
+ getCamerasList() => Promise
300
+ ```
301
+
302
+ #### Parameters
303
+
304
+ No input parameters.
305
+
306
+ #### Returns
307
+
308
+ A promise that resolves with a list of string camera identifiers and human-readable labels: `[{id: 'cameraId1, label: 'cameraLabel1'}, {id: 'cameraId2', label: 'cameraLabel2'}, ...]`.
309
+
310
+ ### changeCamera
311
+
312
+ Selects the camera to be used for the input video stream. Only call this method while the pipeline is active.
313
+
314
+ **Note:** Android-only method.
315
+
316
+ ```javascript
317
+ changeCamera(cameraId: string) => Promise
318
+ ```
319
+
320
+ #### Parameters
321
+
322
+ * *cameraId* — the camera identifier, taken from `getCamerasList`
323
+
324
+ #### Returns
325
+
326
+ A promise that is resolved when the view switches to the selected camera.
327
+
328
+ ### startPipeline
329
+
330
+ Starts the virtual try-on. You may also start try-on directly with the [`downloadAndSetModel`](#downloadandsetmodel) method.
331
+
332
+ ```javascript
333
+ startPipeline() => Promise
334
+ ```
335
+
336
+ #### Parameters
337
+
338
+ No input parameters.
339
+
340
+ #### Returns
341
+
342
+ A promise that is resolved when the try-on session starts.
343
+
344
+ ### pausePipeline
345
+
346
+ Pauses the virtual try-on. The raw camera view is still shown.
347
+
348
+ ```javascript
349
+ pausePipeline() => Promise
52
350
  ```
351
+ #### Parameters
352
+
353
+ No input parameters.
354
+
355
+ #### Returns
356
+
357
+ A promise that is resolved when the try-on session pauses and the model is no longer rendered.
358
+
359
+ ### getPipelineStatus
360
+
361
+ Checks if the pipeline is active.
362
+
363
+ ```javascript
364
+ string getPipelineStatus()
365
+ ```
366
+
367
+ #### Parameters
368
+
369
+ No input parameters.
370
+
371
+ #### Returns
372
+
373
+ A string description of the current status. Can be one of:
374
+
375
+ * `sdk.STATUS_PREPARING` — the library is loading
376
+ * `sdk.STATUS_SDK_LOADED` — the library successfully loaded
377
+ * `sdk.STATUS_INITIALIZING` — the pipeline is initializing
378
+ * `sdk.STATUS_INITIALIZED` — the pipeline successfully initialized
379
+ * `sdk.STATUS_RUNNING` — the pipeline is active
380
+ * `sdk.STATUS_PAUSED` — the pipeline is paused
381
+ * `sdk.STATUS_ERROR` — an error has occurred
382
+ * `sdk.STATUS_WRONG_WORKING_CONDITIONS` — unsupported working conditions (for example, landscape orientation isn't supported)
383
+
384
+ ### takePhoto
385
+
386
+ Makes a snapshot of the current try-on view.
387
+
388
+ ```javascript
389
+ takePhoto() => Promise
390
+ ```
391
+
392
+ #### Parameters
393
+
394
+ No input parameters.
395
+
396
+ #### Returns
397
+
398
+ A promise that resolves with a data blob containing the image.
399
+
400
+ ### destroy
401
+
402
+ Unloads the library and detaches the iframe.
403
+
404
+ All pending promises from WANNA SDK will be rejected with an error (error name will be `sdk.ERROR_SDK_DESTROYED`), but first the [`sdk.EVENT_DESTROYED`](#sdkevent_destroyed) event will be emitted, and you can handle the rejections gracefully using that.
405
+
406
+ ```javascript
407
+ destroy() => Promise
408
+ ```
409
+
410
+ #### Parameters
411
+
412
+ No input parameters.
413
+
414
+ #### Returns
415
+
416
+ A promise that is resolved when the library is unloaded. Now you can initialize the SDK in another container.
417
+
418
+ ## Events
419
+
420
+ The SDK informs you of various events occurring during initialization, model loading, etc. Listen to these events and handle the outcomes to show progress and hints to your users, manage the errors that may happen.
421
+
422
+ ### General
423
+
424
+ #### sdk.EVENT_SDK_LOADED
425
+
426
+ Occurs when the iframe and the scripts for it are loaded successfully.
427
+
428
+ #### sdk.EVENT_INIT_STARTED
429
+
430
+ Occurs when initialization starts.
431
+
432
+ #### sdk.EVENT_INIT_FINISHED
433
+
434
+ Occurs when initialization has finished successfully.
435
+
436
+ #### sdk.EVENT_INIT_FAILED
437
+
438
+ Occurs when initialization has failed.
439
+
440
+ Arguments:
441
+
442
+ * *error* — an `Error` object that describes the reason for the failure
443
+
444
+ #### sdk.EVENT_ERROR
445
+
446
+ Occurs when the SDK encounters an error.
447
+
448
+ Arguments:
449
+
450
+ * *error* — an `Error` object
451
+
452
+ #### sdk.EVENT_DESTROYED
453
+
454
+ Occurs at the [`destroy`](#destroy) method call. This event handler will be called before rejecting the WANNA SDK pending promises. Use it to handle the promises rejection gracefully.
455
+
456
+ ### Resource loading
457
+
458
+ #### sdk.EVENT_RESOURCE_LOAD_STARTED
459
+
460
+ Occurs when the SDK starts to download a resource.
461
+
462
+ Arguments:
463
+
464
+ * *type* — the kind of resource, for example, a 3D model or a neural network model
465
+ * *id* — the identifier of the resource; for 3D models, it's the same ID that you use to `downloadModel` or `downloadAndSetModel`
466
+
467
+ #### sdk.EVENT_RESOURCE_LOAD_PROGRESS
468
+
469
+ Occurs while the resource is loading.
470
+
471
+ Arguments:
472
+
473
+ * *type* — the kind of resource, for example, a 3D model or a neural network model
474
+ * *id* — the identifier of the resource; for 3D models, it's the same ID that you use to `downloadModel` or `downloadAndSetModel`
475
+ * *progress* — percentage completed
476
+
477
+ #### sdk.EVENT_RESOURCE_LOAD_FAILED
478
+
479
+ Occurs when a resource couldn't be loaded.
480
+
481
+ Arguments:
482
+
483
+ * *type* — the kind of resource, for example, a 3D model or a neural network model
484
+ * *id* — the identifier of the resource; for 3D models, it's the same ID that you use to `downloadModel` or `downloadAndSetModel`
485
+
486
+ #### sdk.EVENT_RESOURCE_LOAD_FINISHED
487
+
488
+ Occurs when the resource is loaded successfully.
489
+
490
+ Arguments:
491
+
492
+ * *type* — the kind of resource, for example, a 3D model or a neural network model
493
+ * *id* — the identifier of the resource; for 3D models, it's the same ID that you use to `downloadModel` or `downloadAndSetModel`
494
+
495
+ ### Camera
496
+
497
+ #### sdk.EVENT_CAMERA_CHANGED
498
+
499
+ Occurs when the try-on is switched to use the view from a different camera.
500
+
501
+ Arguments:
502
+
503
+ * *cameraId* — the identifier of the camera now used
504
+
505
+ #### sdk.EVENT_CAMERA_ACCESS_REQUESTED
506
+
507
+ Occurs when the library needs camera access.
508
+
509
+
510
+ #### sdk.EVENT_CAMERA_ACCESS_GRANTED
511
+
512
+ Occurs when the user grants camera access.
513
+
514
+ Arguments contain the data from [MediaStreamTrack](https://developer.mozilla.org/en-US/docs/Web/API/MediaStreamTrack) object representing the raw stream from camera:
515
+
516
+ * *id* — the identifier of the track
517
+ * *kind* — the type of content, in this case `"video"`
518
+ * *label* — the camera label
519
+ * *muted* — indicates if the track is unable to provide the media data because of a technical issue
520
+ * *readyState* — the status of the track: `"live"` or `"ended"`
521
+ * *contentHint* — the string hint about the track content
522
+
523
+ #### sdk.EVENT_CAMERA_ACCESS_FAILED
524
+
525
+ Occurs when the camera can't be accessed, whether because the permission was refused or for another reason.
526
+
527
+ Arguments:
528
+
529
+ * *error* — an `Error` object that describes the reason for the failure
530
+
531
+ ### Try-on object presence
532
+
533
+ #### sdk.EVENT_OBJECT_APPEARED
534
+
535
+ Occurs when the object on which the try-on should be rendered appears in camera view.
536
+
537
+ <!--Arguments:
538
+
539
+ * *type* — the type of object: feet for sneakers, other objects for other products in future versions-->
540
+
541
+ #### sdk.EVENT_OBJECT_DISAPPEARED
542
+
543
+ Occurs when the object on which the try-on should be rendered disappears from camera view.
544
+
545
+ <!--Arguments:
546
+
547
+ * *type* — the type of object: feet for sneakers, other objects for other products in future versions-->
548
+
package/package.json CHANGED
@@ -1,7 +1,11 @@
1
1
  {
2
2
  "name": "@wannaby/wanna-sdk",
3
3
  "description": "Wanna WEB-AR SDK to import as package dependency",
4
- "version": "2.2.2",
4
+ "version": "2.2.5",
5
5
  "main": "./sdk/sdk.js",
6
- "license": "SEE LICENSE IN LICENSE.md"
7
- }
6
+ "license": "SEE LICENSE IN LICENSE.md",
7
+ "files": [
8
+ "core",
9
+ "iframe"
10
+ ]
11
+ }