aboutsummaryrefslogtreecommitdiffhomepage
path: root/experimental/docs/animationCommon.js
blob: 1733ec203200a68903eaa136f39a888b28d46ead (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
var animationState = {};
animationState.reset = function (engine) {
    if ('string' === typeof engine) {
        this.defaultEngine = engine;
    }
    this.defaults = {};
    this.displayList = [];
    this.displayDict = {};
    this.start = null;
    this.time = 0;
    this.timeline = [];
    this.timelineIndex = 0;
    this.requestID = null;
    this.paused = false;
    this.displayEngine = 'undefined' === typeof engine ? this.defaultEngine : engine;
}

function addActions(frame, timeline) {
    var keyframe = keyframes[frame];
    var len = keyframe.length;
    for (var i = 0; i < len; ++i) {
        var action = keyframe[i];
        loopOver(action, timeline);
    }
}

function animateList(now) {
    if (animationState.paused) {
        return;
    }
    if (animationState.start == null) {
        animationState.start = now - animationState.time;
    }
    animationState.time = now - animationState.start;
    var stillAnimating = false;
    for (var index = animationState.timelineIndex; index < animationState.timeline.length; ++index) {
        var animation = animationState.timeline[index];
        if (animation.time > animationState.time) {
            stillAnimating = true;
            break;
        }
        if (animation.time + animation.duration < animationState.time) {
            if (animation.finalized) {
                continue;
            }
            animation.finalized = true;
        }
        stillAnimating = true;
        var actions = animation.actions;
        for (var aIndex = 0; aIndex < actions.length; ++aIndex) {
            var action = actions[aIndex];
            var hasDraw = 'draw' in action;
            var hasRef = 'ref' in action;
            var displayIndex;
            if (hasDraw) {
                var ref = hasRef ? action.ref : "anonymous_" + index + "_" + aIndex;
                assert('string' == typeof(ref));
                if (ref in animationState.displayDict) {
                    displayIndex = animationState.displayDict[ref];
                } else {
                    assert('string' == typeof(action.draw));
                    var draw = (new Function("return " + action.draw))();
                    assert('object' == typeof(draw));
                    var paint;
                    if ('paint' in action) {
                        assert('string' == typeof(action.paint));
                        paint = (new Function("return " + action.paint))();
                        assert('object' == typeof(paint) && !isArray(paint));
                    } else {
                        paint = animationState.defaults.paint;
                    }
                    displayIndex = animationState.displayList.length;
                    animationState.displayList.push( { "ref":ref, "draw":draw, "paint":paint,
                        "drawSpec":action.draw, "paintSpec":action.paint,
                        "drawCopied":false, "paintCopied":false,
                        "drawDirty":true, "paintDirty":true, "once":false } );
                    animationState.displayDict[ref] = displayIndex;
                }
            } else if (hasRef) {
                assert('string' == typeof(action.ref));
                displayIndex = animationState.displayDict[action.ref];
            } else {
                assert(actions.length == 1);
                for (var prop in action) {
                    if ('paint' == prop) {
                        assert('string' == typeof(action[prop]));
                        var obj = (new Function("return " + action[prop]))();
                        assert('object' == typeof(obj) && !isArray(obj));
                        animationState.defaults[prop] = obj;
                    } else {
                        animationState.defaults[prop] = action[prop];
                    }
                }
                continue;
            }
            var targetSpec = 'target' in action ? action.target : animationState.defaults.target;
            assert(targetSpec);
            assert('string' == typeof(targetSpec));
            assert(displayIndex < animationState.displayList.length);
            var display = animationState.displayList[displayIndex];
            var modDraw = targetSpec.startsWith('draw');
            assert(modDraw || targetSpec.startsWith('paint'));
            var modType = modDraw ? "draw" : "paint";
            var copied = modDraw ? display.drawCopied : action.paintCopied;
            if (!copied) {
                var copy;
                if (!modDraw || display.drawSpec.startsWith("text")) {
                    copy = {};
                    var original = modDraw ? display.draw : display.paint;
                    for (var p in original) {
                        copy[p] = original[p];
                    }
                } else if (display.drawSpec.startsWith("paths")) {
                    copy = [];
                    for (var i = 0; i < display.draw.length; ++i) {
                        var curves = display.draw[i];
                        var curve = Object.keys(curves)[0];
                        copy[i] = {};
                        copy[i][curve] = curves[curve].slice(0);  // clone the array of curves
                    }
                } else {
                    assert(display.drawSpec.startsWith("pictures"));
                    copy = [];
                    for (var i = 0; i < display.draw.length; ++i) {
                        var entry = display.draw[i];
                        copy[i] = { "draw":entry.draw, "paint":entry.paint };
                    }
                }
                display[modType] = copy;
                display[modType + "Copied"] = true;
            }
            var targetField, targetObject, fieldOffset;
            if (targetSpec.endsWith("]")) {
                fieldOffset = targetSpec.lastIndexOf("[");
                assert(fieldOffset >= 0);
                targetField = targetSpec.substring(fieldOffset + 1, targetSpec.length - 1);
                var arrayIndex = +targetField;
                if (!isNaN(arrayIndex) && targetField.length > 0) {
                    targetField = arrayIndex;
                }

            } else {
                fieldOffset = targetSpec.lastIndexOf(".");
                if (fieldOffset >= 0) {
                    targetField = targetSpec.substring(fieldOffset + 1, targetSpec.length);
                } else {
                    targetObject = display;
                    targetField = targetSpec;
                }
            }
            if (fieldOffset >= 0) {
                var sub = targetSpec.substring(0, fieldOffset);
                targetObject = (new Function('display', "return display." + sub))(display);
            }
            assert(null != targetObject[targetField]);
            if (!('start' in action) || action.start < animation.time) {
                for (var p in animationState.defaults) {
                    if ('draw' == p || 'paint' == p || 'ref' == p) {
                        continue;
                    }
                    assert('range' == p || 'target' == p || 'formula' == p || 'params' == p);
                    if (!(p in action)) {
                        action[p] = animationState.defaults[p];
                    }
                }
                if ('number' == typeof(action.formula)) {
                    targetObject[targetField] = action.formula;
                    action.once = true;
                }
                action.start = animation.time;
            }
            if (action.once) {
                continue;
            }
            var value = Math.min(1, (animationState.time - animation.time) / animation.duration);
            var scaled = action.range[0] + (action.range[1] - action.range[0]) * value;
            if ('params' in action) {
                if (!('func' in action)) {
                    if (isArray(action.params)) {
                        action.funcParams = [];
                        var len = action.params.length;
                        for (var i = 0; i < len; ++i) {
                            action.funcParams[i] = 'target' == action.params[i]
                                ? targetObject[targetField]
                                : (new Function("return " + action.params[i]))();
                        }
                    } else {
                        action.funcParams = 'target' == action.params
                                ? targetObject[targetField]
                                : (new Function("return " + action.params))();
                    }
                    assert('formula' in action && 'string' == typeof(action.formula));
                    // evaluate inline function to get value
                    action.func = new Function('value', 'params', "return " + action.formula);
                }
                scaled = action.func(scaled, action.funcParams);
            }
            if (targetObject[targetField] != scaled) {
                if (modDraw) {
                    display.drawDirty = true;
                } else {
                    display.paintDirty = true;
                }
                targetObject[targetField] = scaled;
            }
        }
    }
    displayBackend(animationState.displayEngine, animationState.displayList);

    if (stillAnimating) {
        animationState.requestID = requestAnimationFrame(animateList);
    }
}

function flattenPaint(paint) {
    if (!paint.paint) {
        return;
    }
    var parent = paints[paint.paint];
    flattenPaint(parent);
    for (var prop in parent) {
        if (!(prop in paint)) {
            paint[prop] = parent[prop];
        }
    }
    paint.paint = null;
}

function init(engine, keyframe) {
    animationState.reset(engine);
    setupPaint();
    setupBackend(animationState.displayEngine);
    keyframeInit(keyframe);
}

function keyframeInit(frame) {
    animationState.reset();
    addActions("_default", animationState.timeline);
    addActions(frame, animationState.timeline);
    for (var index = 0; index < animationState.timeline.length; ++index) {
        animationState.timeline[index].position = index;
    }
    animationState.timeline.sort(function(a, b) {
        if (a.time == b.time) {
            return a.position - b.position;
        }
        return a.time - b.time;
    });
    keyframeBackendInit(animationState.displayEngine, animationState.displayList,
            keyframes[frame][0]);
    animationState.requestID = requestAnimationFrame(animateList);
}

function loopAddProp(action, propName) {
    var funcStr = "";
    var prop = action[propName];
    if ('draw' != propName && isArray(prop)) {
        funcStr += '[';
        for (var index = 0; index < prop.length; ++index) {
            funcStr += loopAddProp(prop, index);
            if (index + 1 < prop.length) {
                funcStr += ", ";
            }
        }
        funcStr += ']';
        return funcStr;
    }
    assert("object" != typeof(prop));
    var useString = "string" == typeof(prop) && isAlpha(prop.charCodeAt(0));
    if (useString) {
        funcStr += "'";
    }
    funcStr += prop;
    if (useString) {
        funcStr += "'";
    }
    return funcStr;
}

function loopOver(rec, timeline) {
    var funcStr = "";
    if (rec.for) {
        funcStr += "for (" + rec.for[0] + "; " + rec.for[1] + "; " + rec.for[2] + ") {\n";
    }
    funcStr += "    var time = " + ('time' in rec ? rec.time : 0) + ";\n";
    funcStr += "    var duration = " + ('duration' in rec ? rec.duration : 0) + ";\n";
    funcStr += "    var actions = [];\n";
    var len = rec.actions.length;
    for (var i = 0; i < len; ++i) {
        funcStr += "    var action" + i + " = {\n";
        var action = rec.actions[i];
        for (var p in action) {
            funcStr += "        '" + p + "':";
            funcStr += loopAddProp(action, p);
            funcStr += ",\n";
        }
        funcStr = funcStr.substring(0, funcStr.length - 2);
        funcStr += "\n    };\n";
        funcStr += "    actions.push(action" + i + ");\n";
    }
    funcStr += "    timeline.push( { 'time':time, 'duration':duration, 'actions':actions,"
            + "'finalized':false } );\n";
    if (rec.for) {
        funcStr += "}\n";
    }
    var func = new Function('rec', 'timeline', funcStr);
    func(rec, timeline);
}

function setupPaint() {
    for (var prop in paints) {
        flattenPaint(paints[prop]);
    }
}