-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStopMotion.js
61 lines (51 loc) · 1.41 KB
/
StopMotion.js
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
import Scene, {SceneItem} from "scenejs";
const interval = 0.00001;
function stopMotion(item, start = 0, end, count) {
if (!count || count < 1) {
return;
}
const depth = (end - start) / count;
for (let i = 1; i <= count - 1; ++i) {
item.setFrame(start + i * depth, item.getNowFrame(start + i * depth));
}
for (let i = 1; i <= count; ++i) {
item.setFrame(start + i * depth - interval, item.getFrame(start + (i - 1) * depth));
}
}
function test(inst, target) {
if (Array.isArray(inst)) {
return inst.indexOf(target);
} else if (typeof inst === "string") {
return inst === target;
} else {
return inst.test(target);
}
}
export default function StopMotion(obj, options = {}) {
const {include, exclude} = options;
if (obj instanceof Scene) {
const items = obj.items;
for (const id in items) {
const item = items[id];
if ((include && !test(include, id)) ||
(exclude && test(exclude, id))) {
continue;
}
stopMotion(item, 0, item.getDuration(), options.count);
}
} else if (obj instanceof SceneItem) {
stopMotion(obj, 0, obj.getDuration(), options.count);
} else {
const item = new SceneItem(obj, options);
return StopMotion(item, options);
}
return obj;
}
Scene.prototype.setStopMotion = function setStopMotion(options) {
StopMotion(this, options);
return this;
};
SceneItem.prototype.setStopMotion = function setStopMotion(options) {
StopMotion(this, options);
return this;
};