Skip to content

Commit 3ad9481

Browse files
committed
added animation types: linear, once
1 parent 2a2e978 commit 3ad9481

File tree

2 files changed

+78
-13
lines changed

2 files changed

+78
-13
lines changed

js/core.js

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ const PREAMBLE_RE = /^\s*(\w+)\s*:\s*(.+)*$/;
114114
const POS_INTEGER_RE = /^\d+$/;
115115
const DURATION_RE = /^(\d+(\.\d+)?)\s*(s|ms)?$/;
116116

117-
const PREAMBLE_KEYS = ['depth', 'duration'];
117+
const PREAMBLE_KEYS = ['depth', 'duration', 'animationType'];
118+
const ANIMATION_TYPES = ['once', 'linear', 'bounce'];
118119
const MAX_LEVEL = 50;
119120
const TIMEOUT_MILLIS = 1000;
120121

@@ -125,8 +126,21 @@ function bounce(t) {
125126
return 0.5 - Math.cos(a) * 0.5;
126127
}
127128

128-
function lerp(min, max, t) {
129-
t = bounce(t);
129+
function linear(t) {
130+
return t;
131+
}
132+
133+
function lerp(min, max, t, animType) {
134+
switch(animType){
135+
case "linear": t = linear(t);
136+
break;
137+
138+
case "bounce": t = bounce(t);
139+
break;
140+
141+
default: t = bounce(t);
142+
break;
143+
}
130144
return min + t * (max - min);
131145
}
132146

@@ -956,7 +970,8 @@ class Interpreter {
956970
}
957971

958972
visitAnimRange(node) {
959-
return lerp(node.start, node.end, this.t);
973+
const type = this.phraseBook['%preamble'].animationType;
974+
return lerp(node.start, node.end, this.t, type);
960975
}
961976

962977
interpret() {
@@ -1021,6 +1036,14 @@ function parsePreamble(preamble, key, value, lineno) {
10211036
}
10221037
}
10231038
}
1039+
else if (key === 'animationType') {
1040+
if(ANIMATION_TYPES.indexOf(value) !== -1){
1041+
preamble[key] = value;
1042+
}
1043+
else{
1044+
throw new Error(`Line ${lineno}: unknown value of property animationType`);
1045+
}
1046+
}
10241047
}
10251048

10261049
function parsePhraseBook(s) {

js/editor.js

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -249,17 +249,59 @@ class Editor extends Component {
249249
return 2.0;
250250
}
251251

252+
animationType(){
253+
const type = this.phraseBook['%preamble'].animationType;
254+
if(type !== undefined) return type;
255+
else return "bounce";
256+
}
257+
252258
onDoFrame() {
253259
if (this.state.playing) {
254-
try {
255-
const elapsedSeconds = (Date.now() - this.startTime) / 1000.0;
256-
const durationSeconds = this.durationSeconds();
257-
const t = (elapsedSeconds / durationSeconds) % 1.0;
258-
const result = generateString(this.phraseBook, 'root', {}, this.state.seed, t);
259-
this.setState({ frame: this.state.frame + 1, result: result, debugOutput: '' });
260-
window.requestAnimationFrame(this.onDoFrame.bind(this));
261-
} catch (e) {
262-
this.setState({ frame: 0, playing: false, debugOutput: e.message });
260+
const animationType = this.animationType();
261+
262+
switch(animationType){
263+
case "once":
264+
try {
265+
const elapsedSeconds = (Date.now() - this.startTime) / 1000.0;
266+
const durationSeconds = this.durationSeconds();
267+
const t = (elapsedSeconds / durationSeconds) % 1.0;
268+
269+
if(durationSeconds <= elapsedSeconds) return false;
270+
else{
271+
const result = generateString(this.phraseBook, 'root', {}, this.state.seed, t);
272+
this.setState({ frame: this.state.frame + 1, result: result, debugOutput: '' });
273+
window.requestAnimationFrame(this.onDoFrame.bind(this));
274+
}
275+
} catch (e) {
276+
this.setState({ frame: 0, playing: false, debugOutput: e.message });
277+
}
278+
break;
279+
280+
case "linear":
281+
try {
282+
const elapsedSeconds = (Date.now() - this.startTime) / 1000.0;
283+
const durationSeconds = this.durationSeconds();
284+
const t = (elapsedSeconds / durationSeconds) % 1.0;
285+
const result = generateString(this.phraseBook, 'root', {}, this.state.seed, t);
286+
this.setState({ frame: this.state.frame + 1, result: result, debugOutput: '' });
287+
window.requestAnimationFrame(this.onDoFrame.bind(this));
288+
} catch (e) {
289+
this.setState({ frame: 0, playing: false, debugOutput: e.message });
290+
}
291+
break;
292+
293+
case "bounce":
294+
try {
295+
const elapsedSeconds = (Date.now() - this.startTime) / 1000.0;
296+
const durationSeconds = this.durationSeconds();
297+
const t = (elapsedSeconds / durationSeconds) % 1.0;
298+
const result = generateString(this.phraseBook, 'root', {}, this.state.seed, t);
299+
this.setState({ frame: this.state.frame + 1, result: result, debugOutput: '' });
300+
window.requestAnimationFrame(this.onDoFrame.bind(this));
301+
} catch (e) {
302+
this.setState({ frame: 0, playing: false, debugOutput: e.message });
303+
}
304+
break;
263305
}
264306
}
265307
}

0 commit comments

Comments
 (0)