HTML and CSS Reference
In-Depth Information
context
.
strokeRect
(
1
,
1
,
theCanvas
.
width
-
2
,
theCanvas
.
height
-
2
);
update
();
testWalls
();
collide
();
render
();
}
function
function
update
() {
for
for
(
var
var
i
=
0
;
i
<
balls
.
length
;
i
++
) {
ball
=
balls
[
i
];
ball
.
nextx
=
(
ball
.
x
+=
ball
.
velocityx
);
ball
.
nexty
=
(
ball
.
y
+=
ball
.
velocityy
);
}
}
function
function
testWalls
() {
var
var
ball
;
var
var
testBall
;
for
for
(
var
var
i
=
0
;
i
<
balls
.
length
;
i
++
) {
ball
=
balls
[
i
];
iif
(
ball
.
nextx
+
ball
.
radius
>
theCanvas
.
width
) {
ball
.
velocityx
=
ball
.
velocityx
*-
1
;
ball
.
nextx
=
theCanvas
.
width
-
ball
.
radius
;
}
else
else iif
(
ball
.
nextx
-
ball
.
radius
<
0
) {
ball
.
velocityx
=
ball
.
velocityx
*-
1
;
ball
.
nextx
=
ball
.
radius
;
}
else
else iif
(
ball
.
nexty
+
ball
.
radius
>
theCanvas
.
height
) {
ball
.
velocityy
=
ball
.
velocityy
*-
1
;
ball
.
nexty
=
theCanvas
.
height
-
ball
.
radius
;
}
else
else iif
(
ball
.
nexty
-
ball
.
radius
<
0
) {
ball
.
velocityy
=
ball
.
velocityy
*-
1
;
ball
.
nexty
=
ball
.
radius
;
}
}
}