forked from ericbowden/KineticJS-Sand-Particles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
View.js
156 lines (134 loc) · 3.63 KB
/
View.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
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
//animations
window.requestAnimFrame = (function(callback){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(callback){
window.setTimeout(callback, 1000 / 30);
};
})();
function animate(lastTime){
// update
var date = new Date();
var time = date.getTime();
var timeDiff = time - lastTime;
var period = timeDiff/1000; //times per second, our period
//console.log(1000/timeDiff);
//if(!PAUSE) {
for (var i = 0; i < UNIT_ARRAY.length; i++) {
var unit = UNIT_ARRAY[i];
unit.MoveTo(period);
unit.SyncShape();
}
// clear
lastTime = time;
// draw
BOARD.stage.layers[0].draw();
//}
// request new frame
requestAnimFrame(function(){
animate(lastTime);
});
}
function GetUrlVars()
{
var vars = {}, hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars[hash[0]] = hash[1];
}
return vars;
}
// Add a script element as a child of the body
// Use this to 'lazy' load scripts after page load
function loadJSScript(JSscript, callBack) {
var js = document.createElement("script");
//js.type= 'text/javascript';
js.src = JSscript;
document.head.appendChild(js);
js.onload = function () {
console.log('JS onload fired');
callBack();
//removes the call once loaded, script will remain in memory
//document.head.removeChild(js);
}
}
function loadImages(sources, callback){
var images = {};
var loadedImages = 0;
var numImages = 0;
for (var src in sources) {
numImages++;
}
for (var src in sources) {
images[src] = new Image();
images[src].onload = function(){
if (++loadedImages >= numImages) {
callback(images);
}
};
images[src].src = sources[src];
}
//callback(images);
}
function fpsCounter() {
//fps stat---------------------------
var stats = new Stats();
stats.domElement.style.position = 'fixed';
stats.domElement.style.left = '0px';
stats.domElement.style.top = '0px';
$('html').append( stats.domElement );
setInterval( function () {
stats.update();
}, 1000 / 60 );
}
$(document).ready(function(){ //main function
UNIT_ARRAY = [];
fpsCounter();
new Board(({
id: 'BOARD',
rowNum: 300,
colNum: 400,
top: 10,
left: 250,
color: 'gray',
cellSize: 1, //px by px
}));
var vars = GetUrlVars();
if(typeof vars['num'] == 'undefined')
vars['num'] = 1500;
for(var i = 0; i < vars['num']; i++) {
var rand = Math.ceil(Math.random()*10);
var rand2 = Math.ceil(Math.random()*10);
var id = 'sand_'+i;
var unit = new Unit(({
id: id,
board: BOARD,
array: UNIT_ARRAY,
row: 4,
col: 5,
size: 3,//(11-rand),
speed: rand*15, //rand 1-10 * 15 cell per sec
color: (rand2 > 5) ?'white': 'tan',
}));
//console.log(unit);
UNIT_ARRAY[i] = unit;//window[id];
}
$('#board-canvas').mousemove(function(e){
var thisObj = BOARD;
var x = e.pageX - this.offsetLeft;
var y = e.pageY - this.offsetTop;
var row = Math.floor(y/thisObj.cellSize);
var col = Math.floor(x/thisObj.cellSize);
//console.log(row,col);
for(var i = 0; i < UNIT_ARRAY.length; i++)
UNIT_ARRAY[i].SetMoveTo(row,col);
});
var date = new Date();
var time = date.getTime();
animate(time);
});