Вместо того, чтобы рисовать пузыри один за другим, как насчет рисования сразу?т.е. вывести ctx.beginPath()
и ctx.stroke()
из цикла?Это выглядит намного быстрее на Firefox.:)
$.extend(Number.prototype, {
times : function(cb){ for(var i=0; i<this; i++){ cb(i); }},
interval : function(cb){ return setInterval(cb, this); },
timeout : function(cb){ return setTimeout(cb, this); }
});
$(function(){
var $canvas = $('<canvas></canvas>'),
ctx = $canvas[0].getContext('2d'),
$cont = $('#fish-bubbles'),
generators = [],
bubbles = [],
w, h;
$cont.append($canvas);
$(window).bind('resize', onResize);
onResize();
5..times(createBubbleGenerator);
1..interval(drawBubbles);
function drawBubbles(){
ctx.clearRect(0, 0, w, h);
var newBubbles = [],
x, y, i, j, m, imgData, offset;
for(var i=0, l=generators.length; i<l; i++){
for(var j=0, m=0|Math.random()*6; j<m; j++){
newBubbles.push( 0|generators[i] + j );
}
generators[i] = Math.max(0, Math.min(w, generators[i] + Math.random()*10 - 5));
}
bubbles.unshift(newBubbles);
for(i=0; i<bubbles.length; i++){
y = h - i*2;
if(y<0){
bubbles.splice(i);
break;
}
ctx.beginPath();
for(j=0, m=bubbles[i].length; j<m; j++){
x = 0|(bubbles[i][j] += Math.random() * 6 - 3);
ctx.moveTo(x, y);
ctx.lineTo(x+1, y+1);
}
ctx.stroke();
}
}
function createBubbleGenerator(){
generators.push(0|Math.random() * w);
}
function onResize(){
w = $cont.width();
h = $cont.height();
$canvas.attr('width', w).attr('height', h);
ctx.strokeStyle = '#AAA';
}
});
Замедляется, когда появляется больше пузырьков.