Javascript Functions
Refer various functions applicable for WebAR SDK integration
Last updated
Refer various functions applicable for WebAR SDK integration
Last updated
The WebAR’s functions are accessed through the global variable WEBARSDK, which is available after the WebAR SDK script has been loaded. The functions in this page are compatible with A-Frame, BabylonJS & Playcanvas integration if not otherwise specified.
Usage
Description
Associate a specific marker id with an aframe entity's id. When the marker id's image is detected, then the associated aframe entity will be displayed over the marker image.
This API only works with AFrame renderer.
Parameters
Parameter | Type | Description |
---|---|---|
Returns
-
Example
Usage
Description
Disables the auto scaling which happens only object is being anchored, only once. (API only works with "Surface-Tracking")
If auto scale is enabled(recommended), the AR object is scaled in such a way that it fits the screen, otherwise the AR object would appear based on the detected surface's extent(which would not be consistent).
This API is deprecated, use API "SetAutoScale()" instead.
Returns
-
Example
Usage
Description
This function is defined for a portal experience and is used to disable tracking, after the mobile device approaches a portal door, and to start the gyro camera if required. The parameter enableGyroCam is supported only in AFrame rendering engine. It will be ignored for other rendering engines.
Parameters
Returns
-
Example
Usage
Description
This function is specifically designed to enable desktop tracking for face tracking scenarios. This function is particularly useful in instances where the application initially starts in a 'lazy-mode' and subsequently determines which tracking mode to initiate. EnableTrackingOnDesktop()
activates the face tracking capabilities of the SDK when running on a desktop environment. It is supported only in AFrame rendering engine for face tracking mode. It will be ignored for other rendering engines.
Additionally, desktop tracking can also be controlled via the enable-tracking-on-desktop
attribute.
Parameters
Returns
-
Example
Usage
Description
Gets the active marker id i.e. UUID of the current detected marker.
Parameters
-
Returns
Example
Usage
Description
Returns a 4x4 matrix that transforms a stage 3D-object into world-coordinates. Matrix should be accessed in ‘row-major’ order. Please note that tracking assumes a Static-Camera-With-A-Moving-Stage by default. However, it can be changed to Moving-Camera-With-A-Static-Stage using attribute 'static-camera'.
Parameters
-
Returns
Example
Usage
Description
Returns the position of the stage/camera object based on attribute 'static-camera'. Please note that tracking assumes a Static-Camera-With-A-Moving-Stage by default. However, it can be changed to Moving-Camera-With-A-Static-Stage using attribute 'static-camera'.
Parameters
-
Returns
Example
Usage
Description
Returns a 4x4 pure rotation matrix of the stage/camera object based on attribute 'static-camera'. It is the orientation of stage/camera with respect to the world.
Parameters
-
Returns
Example
Usage
Description
Returns the scale of the stage object.
Parameters
-
Returns
Example
Usage
Description
Returns the app loading progress as a percentage. User may find this useful while creating custom splash screen to create custom animation for loading progress.
Parameters
-
Returns
Example
Usage
Description
Returns the magnitude of the distance between the upper and lower lips when the mouth is opened. For example, it can be used to trigger some animation of the webar-face model, if the magnitude is greater than 0.2.
Parameters
-
Returns
Example
Usage
Description
If the SDK script attribute auto-init is set to 'false', then tracking has to be started manually by calling Init() method. For example, this will be useful when the user has to display some information in an overlaid html div, and only after the user clicks a button to enter into the 'Surface Tracking' or 'Marker Tracking' experience.
Usage
Description
This function is used to initialise the SDK with BabylonJs scene Objects for Surface Tracking or Marker Tracking experience.
Parameters
Returns
-
Example
Babylon.Js Example. Refer Section on surface tracking and marker tracking.
Usage
This API is particularly useful if you want to provide a customized reset button within your application. By default, there is a reset button located at the top-right corner of the interface. However, if you wish to use your own button design, you can disable the default button by setting the hide-reset-button
attribute to true
. Then, you can call PerformReset()
from your custom button using the WEBARSDK
instance to achieve the same functionality.
Description
PerformReset()
API is used to reset the tracking in the AR environment. When invoked, it will reset the tracking and the cursor will reappear.
Parameters
-
Returns
-
Example
Usage
Description
Removes the association of a marker id with an entity in the scene and the user will not be able to see any AR object when the marker id's image is detected.
This API only works with AFrame renderer.
Parameters
Returns
-
Example
Usage
Description
This function is used to set a callback function when app is completely loaded. For example, this callback function can be used to fade out the custom animation of splash screen.
Parameters
Returns
-
Example
Usage
Description
The SetARModelPlaceCallback
function is designed to register a callback function that triggers when an AR model is successfully placed within the scene. This allows developers to define custom behavior that occurs immediately after the model placement, such as initiating animations, logging events, or enabling additional UI elements.
Parameters
This function's utility is predicated on the webar-ux-control
being properly set up with stageCursorUX
set to true
in the scene. Without this configuration, the reset button—and consequently the reset callback—will not function. (Only for AFrame & BabylonJS)
Returns
-
Example
Usage
Description
This function is used to overlay auto marker detection default style, user can use custom user experience to enhance visuals. User can also pass a flag to disable the scan instruction text
Parameters
Returns
-
Example
Usage
Description
Auto Scale is enabled by default. To disable the auto scale, pass boolean 'false' in the argument and call this function after Init().
What is Auto Scaling in context of 'Marker Tracking' & 'Surface Tracking'?
**Marker Tracking:**The scaling is applied in such a way that a (1,1) unit plane fits the width/height of the marker(where unit is meter).
**Surface Tracking:**It scales the AR object based on distance from the camera
Parameters
Returns
-
Example
Usage
Description
If set to false, the eyes area of the facemesh appears hollow .i.e no texture/material is rendered on the both the eyes area. If set to true, eyes of the facemesh appears closed.
Parameters
Returns
-
Example
Usage
Description
This function configures the scale at which AR objects are displayed relative to the user’s face in a web-based AR experience. It computes a scale factor based on the bounding box dimensions of a reference model (e.g., human-head) and applies this scale factor to the webar-face
and webar-face-pivot
entities. As a result, all AR objects that are children of these entities will have their scale and position adjusted according to the scale factor, ensuring the AR objects appear proportionally shrinked or enlarged as necessary.
Parameters
Returns
-
Example
Usage
Description
This function is used to set custom guide animations for surface tracking and marker tracking (only with scan button). Please note that it only supports lottie animations json format.(https://lottiefiles.com)
Parameters
Returns
-
Example
Usage
Description
This API gives a callback once the surface tracking experience loads (Splash screen) and when the default Hand with phone animation guide starts(startGuideViewCallback) and then stops/hides (stopGuideViewCallback) after a surface is detected.
If you want to display a custom Hand with phone/Surface detection guide, set the SDK attribute show-guide-view="false", and use the two callback parameters passed via startGuideViewCallback and stopGuideViewCallback, to show and then hide it.
Other cases of using these callbacks are:
A custom loading splash/progress full screen div element can be overlaid and can be removed/hidden after startGuideViewCallback is received
Start the animation of a 3D model after stopGuideViewCallback is received.
Example
Usage
Description
This is used to set a callback function when the 3D object appears on the screen after a marker image is detected. For example, this callback function can be used to start the animation of the 3D model.
Parameters
Returns
-
Example
Usage
Description
This is used to set a callback function when a marker image tracking is lost.
Parameters
Returns
-
Example
Usage
Description
If set to false, the mouth area of the facemesh appears hollow .i.e no texture/material is rendered on the mouth area. If set to true, mouth of the facemesh is closed.
Parameters
Returns
-
Example
Usage
Description
This is a portal experience and is used to set a callback function when the mobile device approaches a 3D portal/door entrance.
Parameters
The callback function for when the mobile device approaches a 3D object.
Returns
-
Example
Usage
When the app opens into the camera feed, a fade-in motion is introduced to create a smooth visual transition. To prevent any unwanted HTML elements from appearing during this transition, you can use this API to hide or adjust these elements beforehand. This ensures that they do not appear momentarily during the transition, which can occur if you use SetVideoStartedCallback()
API.
Description
SetPrepareForCameraTransitionCallback() API allows developers to define a callback function that is triggered just before the camera transition begins during app loading. This feature is particularly useful for managing the visibility of HTML elements to ensure a smooth transition into the camera feed.
Parameters
Returns
-
Example
Usage
Description
This function is designed to link a user-defined callback to the reset button's action within an AR interface. When the reset button is pressed, the specified callback function is executed, allowing developers to implement custom logic such as resetting the scene, clearing user progress, or other application-specific reset behavior.
Parameters
This function's utility is predicated on the webar-ux-control
being properly set up with stageCursorUX
set to true
in the scene. Without this configuration, the reset button—and consequently the reset callback—will not function. (Only for AFrame & BabylonJS)
Returns
-
Example
Usage
Description
The SetResetButtonVisibility
function controls the display of the reset button within the AR interface. It provides the ability to dynamically show or hide the reset button based on the needs of the AR experience or user preferences.
Parameters
Returns
-
Example
Usage
Description
This function is used to set a callback function when the object appears for the first time on screen after the surface. For example, this callback function can be used to start the animation of the 3D model.
Parameters
This is the callback function that gets called when the 3D object appears on screen for the first time after the surface has been detected.
Returns
-
Example
Usage
Description
This is used to set a callback function when either 'Surface' or 'Marker' tracking quality gets changed.
Parameters
Returns
-
Example
Usage
Description
This function is used to set a callback function which gives a callback when either 'Surface' or 'Marker' tracking has started.
Parameters
Returns
-
Example
Usage
Description
This function is used to set a callback function which gives a callback when either 'Surface' or 'Marker' tracking has stopped.
Parameters
Returns
-
Example
Usage
Description
This function is used to start the tracking, if tracking has stopped or param 'auto-start' is set to 'false'. It either starts Surface or Marker Tracking based on the 'webar-mode' attribute.
auto-init is associated with initializing the sdk core whereas auto-start enables the user to control when to start the tracking in the app.
Parameters
Returns
-
Example
Usage
Description
This function is used to stop the tracking, if tracking has already started.
auto-init is associated with initializing the sdk core whereas auto-start enables the user to control when to start the tracking in the app.
Parameters
Returns
-
Example
Usage
Description
This function allows you to enable or disable user rotation.
Parameters
Returns
-
Example
Usage
Description
This function enables or disables user scaling.
Parameters
Returns
-
Example
Usage
Description
This function is used to set the webar mode at a delayed stage of application. It provides a dynamic loading using lazy initialisation. It should be called only-if webar-mode script attribute is specified with 'lazy-mode'.
This method has to be called before StartTracking().
Parameters
Returns
-
Example
Usage
Description
This function is used to capture a snapshot of the AR experience.
Only for Playcanvas! To get the snapshot API to work correctly in Playcanvas rendering engine, enable Preserve Drawing Buffer flag. Go to Playcanvas Project Editor, then navigate to project SETTINGS > RENDERING tab and make sure "Preserve Drawing Buffer" is checked.
Parameters
Returns
-
Example
Parameter | Type | Description |
---|---|---|
Parameter | Type | Description |
---|---|---|
Parameter | Type | Description |
---|---|---|
Returns | Type | Description |
---|---|---|
Returns | Type | Description |
---|---|---|
Returns | Type | Description |
---|---|---|
Returns | Type | Description |
---|---|---|
Returns | Type | Description |
---|---|---|
Returns | Type | Description |
---|---|---|
Returns | Type | Description |
---|---|---|
Parameter | Type | Description |
---|---|---|
Parameter | Type | Description |
---|---|---|
Parameter | Type | Description |
---|---|---|
Parameter | Type | Description |
---|---|---|
Parameter | Type | Description |
---|---|---|
Parameter | Type | Description |
---|---|---|
Parameter | Type | Description |
---|---|---|
Parameter | Type | Description |
---|---|---|
Parameter | Type | Description |
---|---|---|
Parameter | Type | Description |
---|---|---|
Parameter | Type | Description |
---|---|---|
Parameter | Type | Description |
---|---|---|
Parameter | Type | Description |
---|---|---|
Parameter | Type | Description |
---|---|---|
Parameter | Type | Description |
---|---|---|
Parameter | Type | Description |
---|---|---|
Parameter | Type | Description |
---|---|---|
Parameter | Type | Description |
---|---|---|
Parameter | Description | |
---|---|---|
Parameter | Type | Description |
---|---|---|
Parameter | Type | Description |
---|---|---|
Parameter | Type | Description |
---|---|---|
Parameter | Type | Description |
---|---|---|
Parameter | Type | Description |
---|---|---|
Parameter | ||
---|---|---|
Parameter | Type | Description |
---|---|---|
disableAutoScaling
boolean
if true, disables the autoscale if false, enables the autoscale.
enableGyroCam
(This parameter works only for AFrame)
boolean
If true, disables surface tracking and starts A-Frame’s gyro camera.
If false, disables surface tracking but does not start A-Frame’s gyro camera. The camera’s position is not updated.
enableTracking
boolean
If true, enables face tracking on desktop
If false, disables surface tracking on desktop if already enabled by enable-tracking-on-desktop
markerId
string
'marker-id' i.e. uuid string of the detected marker i.e. value obtained from the Blippar Hub Manager after uploading respective marker image. Returns empty string "", if there is no active marker.
poseMatrix
array matrix
The first 3x3 sub-matrix describes the rotation and the first-3 entries of the last column describe the position (location of stage-object).
position
Float32Array
Array of size 3 in order [x, y z] where x, y & z denotes the position on x-axis, y-axis & z-axis respectively.
rotationMatrix
Float32Array
The first 3x3 sub-matrix describes the rotation.
scale
Float32Array
Array of size 3 in order [sx, sy sz] where sx, sy & sz denotes the scale of the stage object in x-axis, y-axis & z-axis respectively.
loadingProgress
float
Loading progress of application
magnitude
float
Returns the magnitude of the distance between the upper and the lower lips when the mouth is opened. Its typical value ranges from 0.0 to 2.0
canvasElement
DOM Element object
DOM Element object of the html canvas element which is used by the Babylon rendering engine.
sceneObject
Babylon Scene object
Scene object created using Babylon
cameraMeshObject
Babylon UniversalCamera object
UniversalCamera object created using Babylon
stageMeshObject | webarMarkerMeshMapArray
Babylon Mesh object (or) Array of JS objects
Surface Tracking
In surface-tracking webar-mode, a stageMeshObject has to be passed.
A stageMeshObject is an empty Babylon mesh which has to be set as the parent for the 3D objects that have to be displayed on the surface.
(or)
Marker Tracking
In marker-tracking webar-mode, a webarMarkerMeshMapArray has to be passed.
A webarMarkerMeshMapArray is an array of Marker Mesh Map objects and it has the following structure:
Please see this example on how to construct and pass a Marker Mesh Map Array.
webarUXControlMesh
Babylon Mesh object
Surface Tracking(optional)
To activate interactive user experience (UX) in surface-tracking mode, a webarUXControlMesh
must be used. This empty Babylon mesh is attached as a child to the stageMeshObject
, adding a stage cursor for precise AR model placement. It also enables gesture-based interactions like rotation and scaling, making the scene more interactive and engaging.
The webarOptions
property of the webarUXControl
mesh manages interactive features. Activating stageCursorUX
displays a placement cursor, while userGestureRotation
and userGestureScale enable intuitive model interactions.
markerId
uuid
Marker id value obtained from the Blippar Hub Manager after uploading an image.
callbackFunction()
function
Callback function which can be used to define custom behaviour when gyro and camera is ready to be used.
callbackFunction()
function
Callback function which can be used to define custom behaviour when AR Object is placed.
autoMarkerUX
html element
HTML div element contains custom visual code
showScanInstructionText
boolean
A boolean flag to enable/disable the scan instructions appear at the bottom of the screen when auto marker detection is enabled. e.g.
'Look for a marker to scan...'
bEnable
boolean
flag to disable/enable the auto scaling, by default auto scaling is enabled
fillEyesArea
boolean
false - Eyes area appears hollow.
true - Eyes area appears filled.
scaleFactor
float
A value calculated as the inverse of the x dimension of the bounding box of the reference model. It is applied as a uniform scale to all AR objects relative to the webar-face
and webar-face-pivot
entities.
pathURL
string
Path of json file created using lottie animations
callbackFunction(markerId)
function
Callback function with markerId as argument that gets called when a marker image is detected and its corresponding 3D object appears.
markerId - Id of the currently detected marker image.
callbackFunction(markerId)
function
Callback function with markerId as argument that gets called when a marker is lost
markerId - ID of the lost marker image.
fillMouthArea
boolean
false - Mouth area appears hollow.
true - Mouth area appears filled.
callbackFunction
function
Callback function that gets called when the mobile device approaches a 3d object.
closenessRatio
float
Specifies the object closeness threshold required to trigger this callback.
0.25 to 0.4 represent a range of typical values depending on application.
0.25 is closer than 0.4 .
callbackFunction
function
Callback function that gets called when the reset button is clicked.
callbackFunction
function
Callback function that gets called when the reset button is clicked.
isVisible
boolean
Determines the visibility of the reset button. Pass true
to show the button or false
to hide it.
callbackFunction(markerId)
function
Callback function with markerId as argument that gets called when the 3d object appears for the first time on screen after the surface has been detected.
markerId - Id of the currently detected marker image.
callbackFunction(trackingQuality)
function
Callback function with trackingQuality as an argument that gets called whenever tracking quality gets changed.
trackingQuality - quality of tracking:
LOW
MEDIUM
HIGH
LOST
callbackFunction()
function
Callback function is called when tracking has started
callbackFunction()
function
Callback function is called when tracking has stopped
keepARAfterLost
boolean
By keeping this flag to true, AR object remains visible when tracking is lost. By default, flag value is kept to false.
keepARVisible
boolean
By keeping this flag true, AR object remains visible when tracking is stopped. By default, flag value is kept false.
enabled
bool
The API allows enabling user gesture rotation by passing true
; otherwise, passing false
disables it.
enabled
bool
The API enables user gesture scale by passing true
; otherwise, passing false
disables it.
trackingMode
string
Two tracking modes are available, anyone can be passed as an argument:
surface-tracking
marker-tracking
callback
function
The callback is called after a snapshot is ready. It provides the canvas instance as an argument in which the screenshot buffer is drawn using its '2d' context. Example: (canvas) => { }
canvas
HTML Canvas Element
Instance of a HTML canvas element. The captured sanpshot is drawn on the 2d context of this user given canvas and also passed as an argument in the callback(1st parameter). If this parameter is ignored or null or undefined, then sdk creates a canvas element to draw the snapshot in 2d context and passes it to the callback(1st parameter).
markerId
uuid
Marker id value obtained from the Blippar Hub Manager after uploading an image.
entityId
string
Value of id attribute of html element.