@wannaby/wanna-sdk 2.2.6 → 2.2.9

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