@@ -10,11 +10,22 @@ Usage:
10
10
})"
11
11
/>
12
12
13
- or if you wanna declare a height or a controller:
13
+ With autoplay on the timeline:
14
+ <MojsInteractive
15
+ id="unique_id"
16
+ autoplay
17
+ code=
18
+ "new mojs.Shape({
19
+ parent: '#unique_id',
20
+ shape: 'circle',
21
+ radius: {20: 80},
22
+ })"
23
+ />
24
+
25
+ or if you wanna declare a height:
14
26
15
27
<MojsInteractive
16
28
id="unique_id"
17
- :controller=true
18
29
height="200px"
19
30
code=
20
31
"new mojs.Shape({
@@ -24,26 +35,54 @@ or if you wanna declare a height or a controller:
24
35
})"
25
36
/>
26
37
27
- or with no controlls at all (static animation, unless you provide a .play() function):
38
+ or with no controlls (will have no animation, unless you provide a .play() function yourself ):
28
39
29
40
<MojsInteractive
30
41
id="unique_id"
31
- :playbutton =false
42
+ :controller =false
32
43
code=
33
44
"new mojs.Shape({
34
45
parent: '#unique_id',
35
46
shape: 'circle',
36
47
radius: {20: 80},
37
48
}).play()"
38
49
/>
50
+
51
+ If you want to show (and use) the decleration of the animation like this:
52
+
53
+ const bouncyCircle = new mojs.Shape({
54
+ ...
55
+ });
56
+
57
+ bouncyCircle.play()
58
+
59
+ Then you may wanna use the global mode.
60
+ The string passed in the global prop should equal the variable you declared.
61
+ Note that it will be global, so make sure it's unique, and use it carefully:
62
+
63
+ <MojsInteractive
64
+ id="bouncyCircle"
65
+ autoplay
66
+ global="circles"
67
+ code=
68
+ "const circles = new mojs.Shape({
69
+ parent: '#bouncyCircle',
70
+ shape: 'circle',
71
+ fill: {'#F64040': '#FC46AD'},
72
+ });
73
+
74
+ circles.play()"
75
+ >
76
+ </MojsInteractive >
77
+
39
78
*/
40
79
41
80
<template >
42
81
<div class =" mojs-interactive" >
43
82
<div
44
83
class =" mojs-interactive__code"
45
84
>
46
- <prism-editor :code="code " language="js" @change="change"></prism-editor>
85
+ <prism-editor :code =" rawCode " language =" js" @change =" change" ></prism-editor >
47
86
<div class =" buttons" >
48
87
<button class =" button button--secondary" v-on:click =" reset" >Reset</button >
49
88
<button class =" button" v-on:click =" updateCode" >Update code</button >
@@ -55,8 +94,6 @@ or with no controlls at all (static animation, unless you provide a .play() func
55
94
:class =" 'mojs-interactive__result ' + (dark ? 'mojs-interactive__result--dark' : '')"
56
95
:style =" style"
57
96
>
58
- <button class="button button--icon button--control" v-if="!controller && playbutton" v-on:click="playPause" :aria-label="isPlaying ? 'Pause animation' : 'Play animation'">{{isPlaying ? '⑊' : '︎︎︎▶︎'}}</button>
59
- <!-- <button class="button button--icon button--control" v-if="!isPlaying && !controller" v-on:click="play" aria-label="Play animation">▶︎</button> -->
60
97
<div v-if =" controller" :id =" this.id + '_controller'" class =" mojs-interactive__controller" ></div >
61
98
</div >
62
99
</div >
@@ -73,13 +110,14 @@ or with no controlls at all (static animation, unless you provide a .play() func
73
110
74
111
props: {
75
112
id: { type: String , default: ' code_example' }, // A unique ID
76
- controller: { type: [String, Boolean], default: false }, // this will create a mojs.Player controller
77
- playbutton: { type: Boolean, default: true }, // use this if you want a simple contoller with a play button
113
+ controller: { type: [String , Boolean ], default: true }, // this will create a mojs.Player controller
114
+ playbutton: { type: Boolean , default: false }, // use this if you want a simple contoller with a play button
78
115
height: { type: String , default: ' 300px' }, // add a custom height to the container, takes all CSS values
79
116
code: { type: String , default: ' ' }, // the code (as a string) to be executed
80
117
dark: { type: Boolean , default: false }, // if you want the demo to be dark 🕶
81
118
notice: { type: [String , Boolean ], default: false }, // to show a "click somewhere to activate animation" text
82
119
autoplay: { type: Boolean , default: false }, // if your REALY want it to autoplay. Use with responsibility!
120
+ global: { type: String , default: ' ' }
83
121
},
84
122
85
123
data : function () {
@@ -105,48 +143,46 @@ or with no controlls at all (static animation, unless you provide a .play() func
105
143
if (! window ) return ; // For SSR
106
144
107
145
// Do some cleaning
146
+ var domRef = window [' demo_' + this .id ] || (this .global !== ' ' && window [this .global ]);
147
+
108
148
// Stop, remove and delete previous instance of: demo_', this.id
109
- if (window['demo_' + this.id]) { // the mojs animation element
110
- window['demo_' + this.id].stop();
111
- window['demo_' + this.id].el.remove(); // remove the DOM node
112
- delete window['demo_' + this.id];
149
+ if (domRef && domRef .stop ) { // the mojs animation element
150
+ domRef .stop ();
151
+ domRef .el .remove (); // remove the DOM node
113
152
}
114
153
// Remove and delete previous instance of player: mojsPlayer_', this.id
115
154
if (window [' mojsPlayer_' + this .id ]) { // the mojs player element
116
155
window [' mojsPlayer_' + this .id ].el .remove (); // remove the DOM node
117
156
delete window [' mojsPlayer_' + this .id ];
118
157
}
119
158
159
+ // Normalize variable declaration and moves them to the windows object instead
160
+ // Then runs the code using new Function()
161
+ if (this .global !== ' ' ) {
162
+ let normalizedCode = code .replaceAll (" const " + this .global , " window." + this .global );
163
+ normalizedCode = normalizedCode .replaceAll (" var " + this .global , " window." + this .global );
164
+ normalizedCode = normalizedCode .replaceAll (" let " + this .global , " window." + this .global );
165
+
166
+ new Function (normalizedCode)();
167
+ } else {
120
168
// Creating a global window object from a provided mojs object (code), and play it.
121
- const func = new Function('window["demo_' + this.id + '"] = ' + code);
122
- try {
123
- func();
124
- if (!this.controller && this.playbutton) {
125
- this.timeline = new mojs.Timeline({
126
- onPlaybackComplete: () => {
127
- this.isPlaying = false;
128
- }
129
- })
130
- .add(
131
- window['demo_' + this.id]
132
- );
133
- // Autoplay the timeline when we press the "Update code" button
134
- if (play) {
135
- this.timeline.play();
136
- this.isPlaying = true;
137
- }
169
+
170
+ const func = new Function (' window["demo_' + this .id + ' "] = ' + code);
171
+ try {
172
+ func ();
173
+ }
174
+ catch (error) {
175
+ console .error (' Woops, please check your code for errors.' , error)
138
176
}
139
177
}
140
- catch(error) {
141
- console.error('Woops, please check your code for errors.', error)
142
- }
143
-
178
+
144
179
// Set the prop :controller=true to include a mojs player
145
- if (this.controller && window['demo_' + this.id]) {
180
+ domRef = window [' demo_' + this .id ] || (this .global !== ' ' && window [this .global ]);
181
+ if (this .controller && domRef) {
146
182
const parentDOM = document .getElementById (this .id + ' _controller' );
147
183
// Create a global mojs player instance
148
184
window [' mojsPlayer_' + this .id ] = new MojsPlayer ({
149
- add: window['demo_' + this.id] ,
185
+ add: domRef ,
150
186
parent: parentDOM,
151
187
className: ' controller' ,
152
188
isSaveState: false ,
@@ -163,14 +199,14 @@ or with no controlls at all (static animation, unless you provide a .play() func
163
199
164
200
reset : function () {
165
201
this .handleCode (this .code );
202
+ this .rawCode = this .code ;
166
203
},
167
204
168
205
playPause : function () {
169
206
if (this .isPlaying ) {
170
207
this .timeline .pause ();
171
208
} else {
172
209
this .timeline .play ();
173
-
174
210
}
175
211
this .isPlaying = ! this .isPlaying ;
176
212
},
0 commit comments