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

Processing shapes dynamically

Let's look at the JavaScript functions that allow the new canvas element to draw rectangles.

How to do it...

fillRect(x,y,width,height)
strokeRect(x,y,width,height)

In order:

fillRect(x,y,width,height)

draws a filled rectangle. Next,

strokeRect(x,y,width,height)

draws an outline around the rectangle.

Now, let's draw some shapes.

How it works...

We'll start with our basic canvas code and incorporate our new functions:

<!DOCTYPE html> <html> <head> <title>Canvas</title> <meta charset="utf-8" /> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/ jquery.min.js"></script> <script> $(document).ready(function() { var canvas = document.getElementById("FirstCanvas"); var ctx = canvas.getContext("2d"); ctx.strokeRect(10, ...

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