@@ -23,6 +23,19 @@ or if you wanna declare a height or a controller:
2323 radius: {20: 80},
2424})"
2525/>
26+
27+ or with no controlls at all (static animation, unless you provide a .play() function):
28+
29+ <MojsInteractive
30+ id="unique_id"
31+ :playbutton=false
32+ code=
33+ "new mojs.Shape({
34+ parent: '#unique_id',
35+ shape: 'circle',
36+ radius: {20: 80},
37+ }).play()"
38+ />
2639*/
2740
2841<template>
@@ -46,7 +59,7 @@ or if you wanna declare a height or a controller:
4659 :class="'mojs-interactive__result ' + (dark ? 'mojs-interactive__result--dark' : '')"
4760 :style="style"
4861 >
49- <button class="button button--icon button--control" v-if="!controller" v-on:click="playPause" :aria-label="isPlaying ? 'Pause animation' : 'Play animation'">{{isPlaying ? '⑊' : '︎︎︎▶︎'}}</button>
62+ <button class="button button--icon button--control" v-if="!controller && playbutton " v-on:click="playPause" :aria-label="isPlaying ? 'Pause animation' : 'Play animation'">{{isPlaying ? '⑊' : '︎︎︎▶︎'}}</button>
5063 <!-- <button class="button button--icon button--control" v-if="!isPlaying && !controller" v-on:click="play" aria-label="Play animation">▶︎</button> -->
5164 <div v-if="controller" :id="this.id + '_controller'" class="mojs-interactive__controller"></div>
5265 </div>
@@ -63,12 +76,14 @@ or if you wanna declare a height or a controller:
6376 },
6477
6578 props: {
66- id: { type: String, default: 'code_example' },
67- controller: { type: [String, Boolean], default: false },
68- height: { type: String, default: '300px' },
69- code: { type: String, default: '' },
70- dark: { type: Boolean, default: false },
79+ id: { type: String, default: 'code_example' }, // A unique ID
80+ controller: { type: [String, Boolean], default: false }, // this will create a mojs.Player controller
81+ playbutton: { type: Boolean, default: true }, // use this if you want a simple contoller with a play button
82+ height: { type: String, default: '300px' }, // add a custom height to the container, takes all CSS values
83+ code: { type: String, default: '' }, // the code (as a string) to be executed
84+ dark: { type: Boolean, default: false }, // if you want the demo to be dark 🕶
7185 notice: { type: [String, Boolean], default: false }, // to show a "click somewhere to activate animation" text
86+ autoplay: { type: Boolean, default: false }, // if your REALY want it to autoplay. Use with responsibility!
7287 },
7388
7489 data: function () {
@@ -90,36 +105,41 @@ or if you wanna declare a height or a controller:
90105 this.rawCode = c;
91106 },
92107
93- handleCode: function(code) {
108+ handleCode: function(code, play ) {
94109
95110 if (!window) return; // For SSR
96-
111+
97112 // Do some cleaning
113+ // Stop, remove and delete previous instance of: demo_', this.id
98114 if (window['demo_' + this.id]) { // the mojs animation element
99115 window['demo_' + this.id].stop();
100116 window['demo_' + this.id].el.remove(); // remove the DOM node
101117 delete window['demo_' + this.id];
102118 }
119+ // Remove and delete previous instance of player: mojsPlayer_', this.id
103120 if (window['mojsPlayer_' + this.id]) { // the mojs player element
104121 window['mojsPlayer_' + this.id].el.remove(); // remove the DOM node
105122 delete window['mojsPlayer_' + this.id];
106123 }
124+
107125 // Creating a global window object from a provided mojs object (code), and play it.
108126 const func = new Function('window["demo_' + this.id + '"] = ' + code);
109127 try {
110128 func();
111- if (!this.controller) {
129+ if (!this.controller && this.playbutton ) {
112130 this.timeline = new mojs.Timeline({
113131 onPlaybackComplete: () => {
114132 this.isPlaying = false;
115133 }
116134 })
117135 .add(
118136 window['demo_' + this.id]
119- )
120- .play();
121-
122- this.isPlaying = true;
137+ );
138+ // Autoplay the timeline when we press the "Update code" button
139+ if (play) {
140+ this.timeline.play();
141+ this.isPlaying = true;
142+ }
123143 }
124144 }
125145 catch(error) {
@@ -129,22 +149,21 @@ or if you wanna declare a height or a controller:
129149 // Set the prop :controller=true to include a mojs player
130150 if (this.controller && window['demo_' + this.id]) {
131151 const parentDOM = document.getElementById(this.id + '_controller');
132-
133152 // Create a global mojs player instance
134153 window['mojsPlayer_' + this.id] = new MojsPlayer({
135154 add: window['demo_' + this.id],
136155 parent: parentDOM,
137156 className: 'controller',
138- isSaveState: true ,
139- isPlaying: true,
157+ isSaveState: false ,
158+ isPlaying: play ? true : this.autoplay, // Autoplay/continue the MojsPlayer when we press the "Update code" button
140159 isRepeat: true,
141160 name: 'demo_' + this.id,
142161 });
143162 }
144163 },
145164
146165 updateCode: function() {
147- this.handleCode(this.rawCode);
166+ this.handleCode(this.rawCode, true );
148167 },
149168
150169 reset: function() {
0 commit comments