forked from colonistio/test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
93 lines (78 loc) · 1.87 KB
/
app.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
84
85
86
87
88
89
90
91
92
93
var app = {
//initial variables
canvas : null,
context : null,
//resizing
width : 800,
height : 400,
background : new Image(),
//nodes
nodes : [],
//timing
timestamp : 0,
now : 0,
lastUpdate : 0,
init : function(){
this.canvas = document.getElementById('canvas');
this.context = this.canvas.getContext('2d');
/**
* Download from:
* https://pixabay.com/es/photos/madera-piso-fondo-antecedentes-1866667
*/
this.background.src = "images/wood.jpg";
this.render();
this.onInit();
},
render : function(){
this.clear();
this.update();
window.requestAnimationFrame(this.render.bind(this));
},
clear : function(){
this.context.clearRect(0, 0, this.width, this.height);
},
update : function(){
var dt = Date.now() - this.lastUpdate;
this.onUpdate(dt);
this.context.drawImage(this.background,0,0);
for(var index in this.nodes){
var node = this.nodes[index];
// Create new node for Text
if(node.istext) {
this.context.fillStyle = 'black'
this.context.font = node.size+'px serif';
this.context.fillText(node.text, node.x, node.y);
}else {
if(node.isball) {
this.context.fillStyle = node.color;
this.context.beginPath();
this.context.arc(node.x, node.y, node.r, 0, Math.PI * 2);
this.context.closePath();
this.context.fill()
}else {
this.context.fillStyle = node.color;
this.context.fillRect(node.x, node.y, node.width, node.height);
}
}
}
this.lastUpdate = Date.now();
this.timestamp+=dt;
},
getNode : function(id){
for(var index in this.nodes){
var node = this.nodes[index];
if(node.id == id){
return node;
}
}
return { x : null, y : null, width : null, height : null };
},
//events
onInit : function(){},
onUpdate : function(){},
pause : function() {},
reset: function() {}
};
window.onload = function(){
app.init();
};