Game Development Reference
In-Depth Information
PARTICLES_LENGTH = MAX_PARTICLES * NFIELDS,
// compatibility with legacy browsers
Float32Array = window.Float32Array || Array,
requestAnimationFrame = window.requestAnimationFrame ||
window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame ||
window.msRequestAnimationFrame || function(f){window.setTimeout(f, 5); },
canvas = document.getElementById('c'),
ctx = canvas.getContext('2d'),
controls = new window.input.Handler(canvas),
particles = new Float32Array(PARTICLES_LENGTH),
// position to insert the next particle
particles_i = 0,
// time in ms
t0 = new Date()*1,
// some shortcuts, they don't seem to make to code faster
PI = Math.PI,
random = Math.random,
cos = Math.cos,
sin = Math.sin;
function emit(x, y) {
for(var i = 0; i < 250; i++) {
particles_i = (particles_i+NFIELDS) % PARTICLES_LENGTH;
particles[particles_i] = x;
particles[particles_i+1] = y;
var alpha = fuzzy(PI),
radius = random()*100,
vx = cos(alpha)*radius,
vy = sin(alpha)*radius,
age = random();
particles[particles_i+2] = vx;
particles[particles_i+3] = vy;
particles[particles_i+4] = age;
}
}
function draw(){
var t1 = new Date()*1,
// time delta in seconds
td = (t1-t0)/1000,
MAX_AGE = 5,
width = canvas.width,
height = canvas.height,
gravity = 50,
drag = 0.999,
// color
r = 120,
g = 55,
b = 10;
t0 = t1;
// emit particles only when we have focus
// if we don't have focus the coordinates are off anyway
Search WWH ::




Custom Search