Web Games Development · Animation & media · ~150 minutes
setInterval and stop it with clearInterval.Number().type="number", min, max).drawImage and embed video with <video>.Create a folder named Week3 and copy the materials (candy.png, reunion.jpg,
pearl.jpg, readers.jpg, the talk video files) into it. Create each file
below, writing the code shown. Save and refresh after each change. Tick each checkpoint.
Week3\bouncingballinputs.html
<!DOCTYPE html>
<html lang="en">
<head><title>Bouncing Ball with inputs</title></head>
<body>
<canvas id="canvas" width="400" height="300"></canvas>
<br />
<form name="f" onsubmit="return change();">
Horizontal velocity <input name="hv" value="4" type="number" min="-10" max="10" />
Vertical velocity <input name="vv" value="8" type="number" min="-10" max="10" />
<input type="submit" value="CHANGE" />
</form>
<script>
var ctx = document.getElementById("canvas").getContext("2d");
var boxx = 20, boxy = 30, boxwidth = 350, boxheight = 250;
var ballrad = 10, ballx = 50, bally = 60, ballvx = 4, ballvy = 8;
var boxboundx = boxwidth + boxx - ballrad;
var boxboundy = boxheight + boxy - ballrad;
var inboxboundx = boxx + ballrad;
var inboxboundy = boxy + ballrad;
function moveball() {
// TODO: clear, move, bounce and draw
}
function change() {
// TODO: read the inputs with Number()
return false;
}
// TODO: setInterval(moveball, 100)
</script>
</body>
</html>
function moveball() {
ctx.clearRect(boxx, boxy, boxwidth, boxheight);
moveandcheck();
ctx.beginPath();
ctx.fillStyle = "rgb(200,0,50)";
ctx.arc(ballx, bally, ballrad, 0, Math.PI * 2, true);
ctx.fill();
ctx.strokeRect(boxx, boxy, boxwidth, boxheight);
}
moveandcheck() for the wall bounce.
function moveandcheck() {
var nballx = ballx + ballvx, nbally = bally + ballvy;
if (nballx > boxboundx || nballx < inboxboundx) { ballvx = -ballvx; }
if (nbally > boxboundy || nbally < inboxboundy) { ballvy = -ballvy; }
ballx = nballx; bally = nbally;
}
change() and start the animation.
function change() {
ballvx = Number(document.f.hv.value);
ballvy = Number(document.f.vv.value);
return false;
}
setInterval(moveball, 100);
Week3\bouncingballinputsvalidate.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bouncing Ball with inputs</title>
<style>
form { width: 330px; margin: 20px; background-color: brown; padding: 20px; }
/* TODO: style valid and invalid inputs */
</style>
</head>
<body>
<canvas id="canvas" width="400" height="300"></canvas>
<form name="f" onsubmit="return change();">
Horizontal velocity <input name="hv" value="4" type="number" min="-10" max="10" />
Vertical velocity <input name="vv" value="8" type="number" min="-10" max="10" />
<input type="submit" value="CHANGE" />
</form>
<script>
// paste your bouncing ball code here
</script>
</body>
</html>
bouncingballinputs.html into the script block.input:valid { background: green; }
input:invalid { background: red; }
required to one input and test it.
<input name="hv" type="number" min="-10" max="10" required />
Week3\bouncingcandybackground.html
<!DOCTYPE html>
<html lang="en">
<head><title>Bouncing cotton candy!</title></head>
<body>
<canvas id="canvas" width="400" height="300"></canvas>
<br />
<button onclick="return stopcc();">STOP</button>
<button onclick="return resume();">RESUME</button>
<script>
var ctx = document.getElementById("canvas").getContext("2d");
var ballrad = 10, ballx = 50, bally = 60, ballvx = 4, ballvy = 8;
var tid;
// TODO: create and load the images
function moveball() {
// TODO: draw the background and the ball
}
// TODO: add stopcc() and resume()
</script>
</body>
</html>
var bkg = new Image(); bkg.src = "reunion.jpg";
var ball = new Image(); ball.src = "candy.png";
moveball().
ctx.clearRect(0, 0, 400, 300);
ballx = ballx + ballvx;
bally = bally + ballvy;
if (ballx > 400 || ballx < 0) { ballvx = -ballvx; }
if (bally > 300 || bally < 0) { ballvy = -ballvy; }
ctx.drawImage(bkg, 0, 0, 4000, 3000, 0, 0, 400, 300);
ctx.drawImage(ball, 0, 0, 388, 435, ballx - ballrad, bally - ballrad, 388/10, 435/10);
tid = setInterval(moveball, 100);
function stopcc() { clearInterval(tid); return false; }
function resume() { tid = setInterval(moveball, 100); return false; }
Week3\bouncintballinputsimggradients.html
<!DOCTYPE html>
<html lang="en">
<head><title>Bouncing Ball with inputs</title></head>
<body>
<canvas id="canvas" width="400" height="300"></canvas>
<br />
<form name="f" onsubmit="return change();">
Horizontal velocity <input name="hv" value="4" type="number" min="-10" max="10" />
Vertical velocity <input name="vv" value="8" type="number" min="-10" max="10" />
<input type="submit" value="CHANGE" />
</form>
<script>
var ctx = document.getElementById("canvas").getContext("2d");
var boxx = 20, boxy = 30, boxwidth = 350, boxheight = 250;
var ballrad = 10, ballx = 50, bally = 60, ballvx = 4, ballvy = 8;
var img = new Image();
img.src = "pearl.jpg";
var grad;
// TODO: build a gradient and draw the walls and the ball
</script>
</body>
</html>
grad = ctx.createLinearGradient(boxx, boxy, boxx + boxwidth, boxy + boxheight);
grad.addColorStop(0, "red");
grad.addColorStop(1, "blue");
ctx.fillStyle = grad;
moveball() to move and draw.
function moveball() {
ctx.clearRect(boxx, boxy, boxwidth, boxheight);
ballx = ballx + ballvx;
bally = bally + ballvy;
if (ballx > boxx + boxwidth - ballrad || ballx < boxx + ballrad) { ballvx = -ballvx; }
if (bally > boxy + boxheight - ballrad || bally < boxy + ballrad) { ballvy = -ballvy; }
ctx.drawImage(img, ballx - ballrad, bally - ballrad, 2 * ballrad, 2 * ballrad);
ctx.fillRect(boxx, boxy, boxwidth, ballrad);
ctx.fillRect(boxx, boxy + boxheight - ballrad, boxwidth, ballrad);
ctx.fillRect(boxx, boxy, ballrad, boxheight);
ctx.fillRect(boxx + boxwidth - ballrad, boxy, ballrad, boxheight);
}
setInterval(moveball, 100);
Week3\bouncingVideoOk2.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bouncing Video</title>
<style>
#videoE { position: absolute; display: none; }
</style>
</head>
<body>
<video id="videoE" controls width="300">
<source src="talk.webmvp8.webm" type="video/webm" />
<source src="talk.mp4video.mp4" type="video/mp4" />
<source src="talk.theora.ogv" type="video/ogg" />
</video>
<button onclick="startV()">Click to start</button>
<script>
var v, ballx = 250, bally = 260, ballvx = 14, ballvy = 18;
function startV() {
v = document.getElementById("videoE");
// TODO: play the video and start moving it
}
// TODO: add moveball() that moves the video and bounces at the edges
</script>
</body>
</html>
startV() to play the video and start moving it.
function startV() {
v = document.getElementById("videoE");
v.style.display = "block";
v.play();
setInterval(moveball, 100);
}
moveball() to move the video and bounce at the edges.
function moveball() {
ballx = ballx + ballvx;
bally = bally + ballvy;
if (ballx > 600 || ballx < 0) { ballvx = -ballvx; }
if (bally > 400 || bally < 0) { ballvy = -ballvy; }
v.style.left = ballx + "px";
v.style.top = bally + "px";
}
| Criterion | Points | Score |
|---|---|---|
| Animation with setInterval and clear/redraw | 3 | |
| Velocity and wall collision handled | 3 | |
| Form inputs read with Number() | 2 | |
| Input validation applied | 2 | |
| Images drawn with drawImage | 2 | |
| Video element with multiple sources | 2 | |
| Runs with no console errors | 1 | |
| Total | 15 |
Number().Progress and quiz scores are also tracked on the Week 3 lab page in the class website.