Skip to content

Commit 1414963

Browse files
author
Nako Sung
committed
Merge branch 'opt'
2 parents 1473619 + 65ddca0 commit 1414963

File tree

6 files changed

+437
-386
lines changed

6 files changed

+437
-386
lines changed
Lines changed: 64 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,93 @@
1-
(function () {
1+
(function (global) {
22
"use strict"
3+
function currentTime() {
4+
return global.$time
5+
}
6+
7+
function $default_access(target, key) {
8+
var h = target["Set" + key]
9+
if (typeof h == 'function') return h.bind(target)
10+
}
311

4-
function animationDriver() {
5-
function currentTime() {
6-
return new Date().getTime() / 1000
7-
}
8-
9-
let alive = true
10-
let running = false
11-
let animations = []
12-
function applyAnim(target, meta, anim) {
13-
let duration = meta.duration || 0.25
14-
let loop = meta.loop || 1
15-
let started = currentTime()
16-
17-
let $default_access = (target, key) => {
18-
let h = target["Set" + k]
19-
if (typeof h == 'function') return h.bind(target)
20-
}
21-
let $access = meta.$access || $default_access
22-
23-
let tracks = []
24-
for (var k in anim) {
25-
let fn = anim[k]
26-
let h = $access(target,k)
12+
function animationDriver() {
13+
var alive = true
14+
var running = false
15+
var animations = []
16+
17+
class Anim {
18+
constructor(target,meta,anim) {
19+
var $access = meta.$access || $default_access
20+
var tracks = this.tracks = []
21+
for (var k in anim) {
22+
var fn = anim[k]
23+
var h = $access(target,k)
2724

28-
if (typeof h == 'function') {
29-
tracks.push(t => {
30-
let value = fn(t)
31-
if (value != undefined) {
32-
h.call(null,value)
33-
}
34-
})
35-
} else {
36-
console.error(`No such track{${k}}`)
25+
if (typeof h == 'function') {
26+
tracks.push([fn,h])
27+
} else {
28+
console.error(`No such track{${k}}`)
29+
}
3730
}
31+
this.duration = meta.duration || 0.25
32+
this.loop = meta.loop || 1
33+
this.started = currentTime()
34+
this.tracks = tracks
35+
this.added = false
36+
this.warm = meta.warm
3837
}
39-
40-
let instance = t => {
41-
let alpha = (t - started) / duration
42-
let lap = Math.floor(alpha)
43-
let shouldQuit = (loop != 0 && lap >= loop)
38+
tick(t) {
39+
var alpha = (t - this.started) / this.duration
40+
var lap = Math.floor(alpha)
41+
var shouldQuit = (this.loop != 0 && lap >= this.loop)
4442
if (shouldQuit) {
4543
alpha = 1
4644
} else {
4745
alpha -= lap
4846
}
49-
tracks.forEach(track => track(alpha))
47+
this.tracks.forEach(track => {
48+
let fn = track[0]
49+
let h = track[1]
50+
let value = fn(alpha)
51+
if (value != undefined) {
52+
h.call(null,value)
53+
}
54+
})
5055
if (shouldQuit) {
51-
remove()
56+
this.remove()
5257
}
5358
}
54-
let added = false
55-
function add() {
56-
if (added) return
57-
added = true
58-
animations.push(instance)
59+
add() {
60+
if (this.added) return
61+
this.added = true
62+
animations.push(this)
5963

60-
if (meta.warm) {
61-
instance(0)
64+
if (this.warm) {
65+
this.tick(0)
6266
}
6367
}
64-
function remove() {
65-
if (!added) return
66-
added = false
67-
animations.splice(animations.indexOf(instance), 1)
68+
remove() {
69+
if (!this.added) return
70+
this.added = false
71+
animations.splice(animations.indexOf(this), 1)
6872
if (animations.length == 0) {
6973
stop()
7074
}
7175
}
76+
}
77+
function applyAnim(target, meta, anim) {
78+
let I = new Anim(target,meta,anim)
79+
7280
if (!running) {
7381
run()
7482
}
75-
add()
76-
return remove
83+
I.add()
84+
return I.remove
7785
}
7886

7987
function loop() {
8088
if (!running) return
81-
let t = currentTime()
82-
animations.forEach(anim => anim(t))
89+
var t = currentTime()
90+
animations.forEach(anim => anim.tick(t))
8391
process.nextTick(loop)
8492
}
8593

@@ -105,4 +113,4 @@
105113
}
106114

107115
module.exports = animationDriver
108-
})()
116+
})(this)

0 commit comments

Comments
 (0)