-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodetree.js
83 lines (72 loc) · 2.83 KB
/
nodetree.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
(function(exports) {
exports.NodePrototype = {
x: 0, y: 0,
skewX: 0, skewY: 0,
scaleX: 1, scaleY: 1,
rotation: 0,
hidden: false,
children: [],
_update: function(delta) {
this.update(delta)
for (c in this.children)
this.children[c]._update(delta)
},
_draw: function(context) {
if(this.hidden) return
context.transform(this.scaleX, this.skewX, this.skewY, this.scaleY, this.x, this.y)
if(this.rotation != 0)
context.rotate(this.rotation)
this.draw(context)
for (c in this.children)
this.children[c]._draw(context)
context.transform(1/this.scaleX, -this.skewX, -this.skewY, 1/this.scaleY, -this.x, -this.y)
if(this.rotation != 0)
context.rotate(-this.rotation)
},
update: function(delta) {},
draw: function(context) {}
}
var makeGame = function(id, callback) {
var vendors = ['ms', 'moz', 'webkit', 'o']
for(var x=0; x < vendors.length && !window.requestAnimationFrame; ++x) {
window.requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame']
window.cancelRequestAnimationFrame = window[vendors[x] + 'CancelRequestAnimationFrame']
}
if (!window.requestAnimationFrame) {
window.requestAnimationFrame = function(callback, element) {
var currentTime = new Date().getTime()
var timeToCall = Math.max(0, 16 - (currentTime - lastTime))
var id = window.setTimeout(function() { callback(currentTime + timeToCall) }, timeToCall)
lastTime = currentTime + timeToCall
return id
}
}
if (!window.cancelAnimationFrame) {
window.cancelAnimationFrame = function(id) {
clearTimeout(id)
}
}
var canvas = document.getElementById(id)
var context, lastTimeStamp = 0
var animationFrame = function(timeStamp) {
window.requestAnimationFrame(animationFrame)
delta = timeStamp - lastTimeStamp
lastTimeStamp = timeStamp
if (exports.paused) return
context.setTransform(1, 0, 0, 1, 0, 0)
context.clearRect(0, 0, canvas.width, canvas.height)
callback(context, delta*0.001)
}
if (typeof (canvas.getContext) !== undefined) {
context = canvas.getContext('2d')
animationFrame(0)
}
}
exports.start = function(id, rootNode) {
makeGame(id, function(context, delta) {
rootNode._update(delta)
rootNode._draw(context)
})
}
exports.paused = false
})(typeof exports === 'undefined'? this['nodetree']={}: exports)