## 借来的创意

2011-12-01 08:54  【当耐特】  阅读(4004)  评论(8编辑  收藏

# 三.粒子计数器实现

        var canvas = document.getElementById("myCanvas");
var cxt = canvas.getContext("2d");
cxt.fillStyle = "#FF0000";

for (var i = 0; i < 4; i++) {
for (var j = 0; j < 7; j++) {
cxt.arc(100 + i * 20, 100 + j * 20, 5, 0, Math.PI * 2, true);
}
}


        var one = [];
one.push({ x: 3, y: 0 });
one.push({ x: 3, y: 1 });
one.push({ x: 3, y: 2 });
one.push({ x: 3, y: 3 });
one.push({ x: 3, y: 4 });
one.push({ x: 3, y: 5 });
one.push({ x: 3, y: 6 });


        var two = [];
two.push({ x: 0, y: 0 });
two.push({ x: 1, y: 0 });
two.push({ x: 2, y: 0 });
two.push({ x: 3, y: 0 });
two.push({ x: 3, y: 1 });
two.push({ x: 3, y: 2 });
two.push({ x: 3, y: 3 });
two.push({ x: 2, y: 3 });
two.push({ x: 1, y: 3 });
two.push({ x: 0, y: 3 });
two.push({ x: 0, y: 4 });
two.push({ x: 0, y: 5 });
two.push({ x: 0, y: 6 });
two.push({ x: 1, y: 6 });
two.push({ x: 2, y: 6 });
two.push({ x: 3, y: 6 });



            if (num === 1) {
for (i in one) {
var result = JSLINQ(two).
Count(function (item) { return item.x == one[i].x && item.y == one[i].y; });
if (result == 0) {
var ball = { x: 100 + one[i].x * 20, y: 100 + one[i].y * 20, r: 8, vx: getRandomNumber(-250,250), vy: getRandomNumber(-150, 150) };
dropBall.push(ball);
}
}
}


        var drawAsync = eval(Jscex.compile("async", function () {
while (true) {
try {
if (dropBall.length > 100) dropBall.shift();
cxt.clearRect(0, 0, canvas.width, canvas.height);
cxt.beginPath();
draw(count % 10);

for (i in dropBall) {
cxt.arc(dropBall[i].x, dropBall[i].y, dropBall[i].r, 0, Math.PI * 2, true);

dropBall[i].y += dropBall[i].vy * cyc / 1000;

dropBall[i].x += dropBall[i].vx * cyc / 1000;

if (dropBall[i].r + dropBall[i].y > canvas.height) {

dropBall[i].vy *= -0.7;

dropBall[i].y = canvas.height - dropBall[i].r;

}
dropBall[i].vy += a;

}
cxt.fill();
t += cyc;
if (t >= 1000) {
getBalls(count % 10);
t = 0;
count++;
draw(count % 10);
}

}
catch (e) {
}
\$await(Jscex.Async.sleep(cyc));
}

}))


                    t += cyc;
if (t >= 1000) {
getBalls(count % 10);
t = 0;
count++;
draw(count % 10);
}