Skip to content

Commit 0b00df5

Browse files
committed
Prevented autoplay on interactive code for performence and stability
1 parent 1ecb811 commit 0b00df5

File tree

2 files changed

+40
-17
lines changed

2 files changed

+40
-17
lines changed

docs/.vuepress/components/MojsInteractive.vue

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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() {

docs/tutorials/shape-swirl/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ Obviously, you can style the shape as you want (play around with the different p
4747

4848
<MojsInteractive
4949
id="circle"
50+
:playbutton=false
5051
code=
5152
"new mojs.Shape({
5253
parent: '#circle',
@@ -72,6 +73,7 @@ Numeric properties may be unit based (like `top`/`left` below) or can be express
7273

7374
<MojsInteractive
7475
id="values"
76+
:playbutton=false
7577
code=
7678
"new mojs.Shape({
7779
parent: '#values',
@@ -96,6 +98,7 @@ The `radius` property sets shape's (no prizes for guessing) radius. Also, you ca
9698

9799
<MojsInteractive
98100
id="radius_example"
101+
:playbutton=false
99102
code=
100103
"new mojs.Shape({
101104
parent: '#radius_example',
@@ -266,6 +269,7 @@ To keep the `APIs` declarative, `mojs` uses `delta` objects to describe transiti
266269

267270
<MojsInteractive
268271
id="delta"
272+
:playbutton=false
269273
code=
270274
"new mojs.Shape({
271275
parent: '#delta',

0 commit comments

Comments
 (0)