@@ -23,6 +23,19 @@ or if you wanna declare a height or a controller:
23
23
radius: {20: 80},
24
24
})"
25
25
/>
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
+ />
26
39
*/
27
40
28
41
<template>
@@ -46,7 +59,7 @@ or if you wanna declare a height or a controller:
46
59
:class="'mojs-interactive__result ' + (dark ? 'mojs-interactive__result--dark' : '')"
47
60
:style="style"
48
61
>
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>
50
63
<!-- <button class="button button--icon button--control" v-if="!isPlaying && !controller" v-on:click="play" aria-label="Play animation">▶︎</button> -->
51
64
<div v-if="controller" :id="this.id + '_controller'" class="mojs-interactive__controller"></div>
52
65
</div>
@@ -63,12 +76,14 @@ or if you wanna declare a height or a controller:
63
76
},
64
77
65
78
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 🕶
71
85
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!
72
87
},
73
88
74
89
data: function () {
@@ -90,36 +105,41 @@ or if you wanna declare a height or a controller:
90
105
this.rawCode = c;
91
106
},
92
107
93
- handleCode: function(code) {
108
+ handleCode: function(code, play ) {
94
109
95
110
if (!window) return; // For SSR
96
-
111
+
97
112
// Do some cleaning
113
+ // Stop, remove and delete previous instance of: demo_', this.id
98
114
if (window['demo_' + this.id]) { // the mojs animation element
99
115
window['demo_' + this.id].stop();
100
116
window['demo_' + this.id].el.remove(); // remove the DOM node
101
117
delete window['demo_' + this.id];
102
118
}
119
+ // Remove and delete previous instance of player: mojsPlayer_', this.id
103
120
if (window['mojsPlayer_' + this.id]) { // the mojs player element
104
121
window['mojsPlayer_' + this.id].el.remove(); // remove the DOM node
105
122
delete window['mojsPlayer_' + this.id];
106
123
}
124
+
107
125
// Creating a global window object from a provided mojs object (code), and play it.
108
126
const func = new Function('window["demo_' + this.id + '"] = ' + code);
109
127
try {
110
128
func();
111
- if (!this.controller) {
129
+ if (!this.controller && this.playbutton ) {
112
130
this.timeline = new mojs.Timeline({
113
131
onPlaybackComplete: () => {
114
132
this.isPlaying = false;
115
133
}
116
134
})
117
135
.add(
118
136
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
+ }
123
143
}
124
144
}
125
145
catch(error) {
@@ -129,22 +149,21 @@ or if you wanna declare a height or a controller:
129
149
// Set the prop :controller=true to include a mojs player
130
150
if (this.controller && window['demo_' + this.id]) {
131
151
const parentDOM = document.getElementById(this.id + '_controller');
132
-
133
152
// Create a global mojs player instance
134
153
window['mojsPlayer_' + this.id] = new MojsPlayer({
135
154
add: window['demo_' + this.id],
136
155
parent: parentDOM,
137
156
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
140
159
isRepeat: true,
141
160
name: 'demo_' + this.id,
142
161
});
143
162
}
144
163
},
145
164
146
165
updateCode: function() {
147
- this.handleCode(this.rawCode);
166
+ this.handleCode(this.rawCode, true );
148
167
},
149
168
150
169
reset: function() {
0 commit comments