Hi I have the following Canvas application: http://dev.driz.co.uk/canvas/
But it's not rendering anything out. It should be showing a bunch of balls that move around the screen when the user moves the cursor. I think it's just a small bug somewhere开发者_Python百科 because it was working fine in an older version here: http://dev.driz.co.uk/app/
It's taking a while to load as if it's doing something but just snagging somewhere, but the code looks fine to me. After testing in Firebug the following error comes up: 'Cannot read property x of undefined' on several lines mainly around the gradient creation. I have no idea what the problem is :/ It also complains about property color as well. I have tried for a couple of hours to try and fix this, but I'm lost, really need some help.
If anyone can help it'd be much appreciated. Thank you
EDIT: Here is the code:
function App()
{
var pool = document.getElementById('pool');
var canvas = pool.getContext('2d');
var cwidth = pool.width = document.width;
var cheight = pool.height = document.height;
var ctop = pool.offsetTop;
var cleft = pool.offsetLeft;
var size = [5, 10, 15, 20, 25, 30];
var numBalls = 10;
var i;
var x;
var y;
var mouseX = 0;
var mouseY = 0;
// GIVES EACH BALL A RANDOM COLOR
function rgb()
{
var color = 'rgb(';
for( i=0; i<3; i++)
{
color += Math.floor(Math.random() * 255) + ',';
}
return color.replace(/\,$/,')');
}
// CREATES A BALL
function CreateBall(x, y, vx, vy, r, s)
{
this.color = rgb();
this.x = x;
this.y = y;
this.vX = vx;
this.vY = vy;
this.r = r;
this.size = s;
}
var ball = [], x, y, vx, vy, r, s;
for ( i = 0; i < numBalls; i++ )
{
/*x = Math.random() * cwidth >> 0;
y = Math.random() * cheight >> 0;
vx = Math.random() * 20 - 6;
vy = Math.random() * 20 - 6;
r = Math.random() * 30 + 30;*/
x = cwidth / 2;
y = cheight / 2;
vx = Math.random() * 20 - 6;
vy = Math.random() * 20 - 6;
r = Math.random() * 30 + 30;
s = size[Math.random() * size.length >> 0];
// CREATES THE BALLS
ball.push( new CreateBall(x, y, vx, vy, r, s));
}
setInterval(function ()
{
canvas.clearRect(0, 0, cwidth, cheight);
for ( i = 0; i < balls.length; i++ )
{
var gradient = canvas.createRadialGradient(ball[i].x + ball[i].r/4, ball[i].y - ball[i].r/4, ball[i].r/5, ball[i].x, ball[i].y, ball[i].r);
gradient.addColorStop(0, '#fff');
gradient.addColorStop(.85, ball[i].color);
gradient.addColorStop(1, '#222');
canvas.fillStyle = gradient;
ball[i].vx *= 0.99;
ball[i].vy *= 0.99;
ball[i].x += ball[i].vx;
ball[i].y += ball[i].vy;
if ( ball[i].x < ball[i].r || ball[i].x > cwidth - ball[i].r )
{
ball[i].vx = -ball[i].vx;
ball[i].x += ball[i].vx;
}
if ( ball[i].y < ball[i].r || ball[i].y > cheight - ball[i].r )
{
ball[i].vy = -ball[i].vy;
ball[i].y += ball[i].vy;
}
canvas.beginPath();
canvas.arc ( ball[i].x, ball[i].y, ball[i].r, 0, Math.PI * 2, true );
canvas.fill();
}
}, 30);
// END FOR
// MOUSE MOVEMENT - BALLS SHOULD MOVE AWAY FROM MOUSE CURSOR
pool.onmousemove = function ( e )
{
x = e.pageX - cleft;
y = e.pageY - ctop;
for ( i = 0; i < balls.length; i++ )
{
if ( Math.abs( x - ball[i].x ) < 20 && Math.abs( y - ball[i].y ) < 20 )
{
ball[i].vx = ( x - ball[i].x ) / 1;
ball[i].vy = ( y - ball[i].y ) / 1;
}
}
};
// END APP
}
function App() {
var pool = document.getElementById('pool');
var canvas = pool.getContext('2d');
var cwidth = pool.width = document.width;
var cheight = pool.height = document.height;
var ctop = pool.offsetTop;
var cleft = pool.offsetLeft;
var size = [5, 10, 15, 20, 25, 30];
var numBalls = 3;
var i;
var x;
var y;
var mouseX = 0;
var mouseY = 0;
function rgb() {
var color = 'rgb(';
for (var i = 0; i < 3; i++) {
color += Math.floor(Math.random() * 255) + ',';
}
return color.replace(/\,$/, ')');
}
function CreateBall(x, y, vx, vy, r, s) {
this.color = rgb();
this.x = x;
this.y = y;
this.vx = vx;
this.vy = vy;
this.r = r;
this.size = s;
}
var ball = [],
x, y, vx, vy, r, s;
for (var i = 0; i < numBalls; i++) {
x = cwidth / 2;
y = cheight / 2;
vx = Math.random() * 20 - 6;
vy = Math.random() * 20 - 6;
r = Math.random() * 30 + 30;
s = size[Math.random() * size.length >> 0];
ball.push(new CreateBall(x, y, vx, vy, r, s));
}
setInterval(function () {
canvas.clearRect(0, 0, cwidth, cheight);
for (var i = 0; i < ball.length; i++) {
var gradient = canvas.createRadialGradient(ball[i].x + ball[i].r / 4, ball[i].y - ball[i].r / 4, ball[i].r / 5, ball[i].x, ball[i].y, ball[i].r);
gradient.addColorStop(0, '#fff');
gradient.addColorStop(.85, ball[i].color);
gradient.addColorStop(1, '#222');
canvas.fillStyle = gradient;
ball[i].vx *= 0.99;
ball[i].vy *= 0.99;
ball[i].x += ball[i].vx;
ball[i].y += ball[i].vy;
if (ball[i].x < ball[i].r || ball[i].x > cwidth - ball[i].r) {
ball[i].vx = -ball[i].vx;
ball[i].x += ball[i].vx;
}
if (ball[i].y < ball[i].r || ball[i].y > cheight - ball[i].r) {
ball[i].vy = -ball[i].vy;
ball[i].y += ball[i].vy;
}
canvas.beginPath();
canvas.arc(ball[i].x, ball[i].y, ball[i].r, 0, Math.PI * 2, 0);
canvas.fill();
}
}, 1000 / 30);
pool.onmousemove = function (e) {
x = e.pageX - cleft;
y = e.pageY - ctop;
for (var i = 0; i < ball.length; i++) {
if (Math.abs(x - ball[i].x) < 20 && Math.abs(y - ball[i].y) < 20) {
ball[i].vx = (x - ball[i].x) / 1;
ball[i].vy = (y - ball[i].y) / 1;
}
}
};
}
I just kicked it's ass here, Do you wanna kick it's ass too ??
精彩评论