April 2013
Beginner
750 pages
20h 21m
English
The clearRect() function
takes in the start x,y location and the width and height
to clear the Canvas:
varw=theCanvas.width;varh=theCanvas.height;context.clearRect(0,0,w,h);
Let’s test out using the clearRect() function by animating a path
across the Canvas (Example 2-28). We will
accomplish this by implementing the setTimeOut() function presented in Chapter 1. It will be used to repeatedly
call our drawScreen() function and
update the location of the path. The entire set of code for this example
is presented because it is more involved than simply drawing a path or a
shape on the Canvas at a single time.
Example 2-28. Using the clearRect() function
<!doctype html><htmllang="en"><head><metacharset="UTF-8"><title>Chapter 2 Example 28: Animating a Path</title>
<scriptsrc="modernizr.js"></script><scripttype="text/javascript">window.addEventListener('load',eventWindowLoaded,false);functioneventWindowLoaded(){canvasApp();}functioncanvasSupport(){returnModernizr.canvas;}functioncanvasApp(){if(!canvasSupport()){return;}else{vartheCanvas=document.getElementById('canvas');varcontext=theCanvas.getContext('2d');}varyOffset=0;functiondrawScreen(){context.clearRect(0,0,theCanvas.width,theCanvas.height);varcurrentPath=context.beginPath();context.strokeStyle="red";//need list of available colorscontext.lineWidth=5;context.moveTo(0,0+yOffset);context.lineTo(50,0+yOffset);context.lineTo(50,50+yOffset ...
Read now
Unlock full access