O'Reilly logo

HTML5 Multimedia Development Cookbook by Lee Jordan, Dale Cruse

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Bouncing a ball

We've looked at how do draw shapes using the new canvas element, and next we'll turn our attention to making those shapes move. Author Vinci Rufus shows us how.

How to do it...

We'll start with our usual canvas HTML code:

<!DOCTYPE html>
<html>
<head>
<title>Canvas</title>
<meta charset="utf-8" />
</head>
<body>
<canvas id="FirstCanvas" width="800" height="600">
<!-- Fallback code goes here -->
</canvas>
</body>
</html>

Next up is the unique part: the JavaScript. Here, Vinci chose a slightly different method of calling the 2d canvas rendering context than we did, but his method works just fine too. Check it out:

<script> var context; function init() { context= myCanvas.getContext('2d'); context.beginPath(); context.fillStyle="#0000ff"; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required