In this guide, we'll show a few common controls that allow users to control the playback of a 3D model animation in the display-3d-model component. This includes the ability to:
Start playing and pausing a 3D model animation
Moving the playback position forwards or backwards
Setting the playback position to a specified point
Note that we have set the autoplay attribute to false, since we want users to be able to control the playback manually. We have also added an id attribute, which is used by component methods to identify the component in the view XML.
In the view JS, we'll initialize a global variable for isPlaying which is used to dynamically show and hide the play and pause buttons.
main.js
var isPlaying =false;// This function is called when the app navigates to this view (using a link)functioninit() {// initialize any data here that should be available when the view is shownview.scale =0.1;}
Next, we'll need to define the logic for playing and pausing the animation. In the view JS, we'll add two functions and add a playheadPosition variable to track the elapsed playback time:
main.js
var isPlaying =false;var playheadPosition =0;// This function is called when the app navigates to this view (using a link)functioninit() {// initialize any data here that should be available when the view is shownview.scale =0.1;}// This function is called when the user returns to this view from another viewfunctionresume(from) { }functionplayAnimation() {console.log("Playing animation")// Set the component's playhead to the specified playhead positioncomponent.display3dModel({ id:"example" }).setPlayhead(playheadPosition);// Start the animation from the defined playheadcomponent.display3dModel({ id:"example" }).animationStart();// Hide the "Play" button and show the "Pause" button isPlaying =true;}functionpauseAnimation() {console.log("Pausing animation")// Stop playing the animationcomponent.display3dModel({ id:"example" }).animationStop();// Show the "Play" button isPlaying =false;// Store the current playhead position playheadPosition =component.display3dModel({ id:"example" }).getPlayhead();}
Note that the above functions, to start and stop the animation playback, make use of multiple component methods of display-3d-model.
Move the playhead forwards, backwards or set it to a specific point
Here we are adding controls to move the animation playhead position forwards and backwards by a set amount, as well as directly specifying the playhead position.
We'll update the relevant part of the view XML code to the following:
In the view JS, we'll define the skipAnimation and setPlayhead functions:
main.js
functionsetPlayhead() {// Allow the user to enter a new position in msvar position =journey.dialog.input({ title:'Set Playhead position', message:"Enter the value in ms", inputValue:playheadPosition.toString(), });if (position !=null) {// Store the new position playheadPosition =Number(position);// Show the "Play" button isPlaying =false; }}functionskipAnimation(timeValue) {// Update the playhead position playheadPosition = playheadPosition + timeValue;// Automatically start/continue playing from this new positionplayAnimation();}
Complete view code
Below is the complete View XML and JS code used in this guide:
var isPlaying =false;var playheadPosition =0;// This function is called when the app navigates to this view (using a link)functioninit() {// initialize any data here that should be available when the view is shownview.scale =0.1;}// This function is called when the user returns to this view from another viewfunctionresume(from) { }functionsetPlayhead() {// Allow the user to enter a new position in msvar position =journey.dialog.input({ title:'Set Playhead position', message:"Enter the value in ms", inputValue:playheadPosition.toString(), });if (position !=null) {// Store the new position playheadPosition =Number(position);// Show the "Play" button isPlaying =false; }}functionskipAnimation(timeValue) {// Update the playhead position playheadPosition = playheadPosition + timeValue;// Automatically start/continue playing from this new positionplayAnimation();}functionplayAnimation() {console.log("Playing animation")// Set the component's playhead to the specified playhead positioncomponent.display3dModel({ id:"example" }).setPlayhead(playheadPosition);// Start the animation from the defined playheadcomponent.display3dModel({ id:"example" }).animationStart();// Hide the "Play" button and show the "Pause" button isPlaying =true;}functionpauseAnimation() {console.log("Pausing animation")// Stop playing the animationcomponent.display3dModel({ id:"example" }).animationStop();// Show the "Play" button isPlaying =false;// Store the current playhead position playheadPosition =component.display3dModel({ id:"example" }).getPlayhead();}