1.07k likes | 1.39k Views
HTML5 Canvas API. Dr. Raymond Bond. API Definition: “a bitmapped area of the screen that can be manipulated with JavaScript ”. Oreilly , HTML5 Canvas. Element Definition: “The HTML5 <canvas> element is used to draw graphics, on the fly, via scripting (usually JavaScript) .”.
E N D
HTML5 Canvas API Dr. Raymond Bond
API Definition: “a bitmapped area of the screen that can be manipulated with JavaScript” Oreilly, HTML5 Canvas
Element Definition: “The HTML5 <canvas> element is used to draw graphics, on the fly, via scripting (usually JavaScript).” http://www.w3schools.com/html/html5_canvas.asp
“HTML5 Canvas is the technology that has the best capability of replacing Flash functionality on the web and mobile web.” Oreilly, HTML5 Canvas
Canvas Applications • http://atari.com/arcade#!/arcade/atari-promo • http://www.createjs.com/#!/EaselJS/demos • http://mudcu.be/sketchpad/ • http://raphaeljs.com/ • http://alteredqualia.com/canvasmol/ • http://thewildernessdowntown.com/
Testing for Support <scriptsrc="http://modernizr.com/downloads/modernizr-latest.js"></script> if(Modernizr.canvas == true){ startCanvasApp(); }else{ //browser does not support canvas }
Canvas Element <canvas id="myCanvas" width="200" height="100"> </canvas> NOTE: Need id for script access.
Canvas Element • The <canvas> element is only a container for the graphics. • A canvas can only be rectangular • You can have multiple <canvas> elements on one HTML page • By default, the <canvas> element has no border and no content.
Adding a Border <canvas id="myCanvas" width="200" height="100" style="border:1px solid #000000;"> </canvas>
Inline Canvas Scripting <body> <canvas id="myCanvas" width="200" height="100” > Your browser does not support the HTML5 canvas tag. </canvas> <script> var c = document.getElementById("myCanvas"); varctx = c.getContext("2d");ctx.fillStyle = "#FF0000"; ctx.fillRect(0,0,150,75); </script> </body> Returns the CanvasRenderingContext2D object
Or use the onload event <head> <script> function onload(){ varc=document.getElementById("myCanvas"); varctx=c.getContext("2d"); ctx.fillStyle="#FF0000"; ctx.fillRect(0,0,150,75); } </script> </head> <body onLoad="onload()"> <canvas id="myCanvas" width="200" height="100" style="border:1px solid #c3c3c3;">Your browser does not support the HTML5 canvas tag.</canvas> </body>
Or link to external JS file <head> <script type="text/javascript" src="canvasapp.js"></script> </head> <body onLoad="onload()"> <canvas id="myCanvas" width="200" height="100" style="border:1px solid #c3c3c3;">Your browser does not support the HTML5 canvas tag.</canvas> </body> NOTE: In HTML5, you no longer have to specify the script type attribute. Do we need this?
No!! window.onload = function(){ canvasApp(); } function canvasApp(){ varc=document.getElementById("myCanvas"); varctx=c.getContext("2d"); ctx.fillStyle="#FF0000"; ctx.fillRect(0,0,150,75); }
AddEventListener window.addEventListener("load", canvasApp, false); function canvasApp(){ varc=document.getElementById("myCanvas"); varctx=c.getContext("2d"); ctx.fillStyle="#FF0000"; ctx.fillRect(0,0,150,75); }
Jqueryonload event $(function() { // insert code });
What makes canvas different from other graphics technologies such as Adobe Flash?
Immediate mode “Canvas is an immediate mode bitmapped area of the screen... Immediate mode refers to the way the canvas renders pixels… Canvas completely redraws the bitmapped screen on every frame...” “which means everything needs to be redrawn every time something changes.”
Retained mode “This makes Canvas very different from Flash, Silverlight, or SVG, which operate in retained mode. In this mode, a display list of objects is kept by the graphics renderer…” Oreilly, HTML5 Canvas
Pros and cons Immediate mode: low level operations more control Responsive mode: high level operations less control
Text on the Canvas var c = document.getElementById("myCanvas"); var ctx = c.getContext("2d"); ctx.font = "30px Arial"; ctx.fillText("Hello World", 10, 50);
Text on the Canvas var c=document.getElementById("myCanvas"); var ctx=c.getContext("2d"); ctx.font="30px Arial"; ctx.strokeText("Hello World",10,50);
Drawing functions var c=document.getElementById("myCanvas"); varctx=c.getContext("2d"); ctx.beginPath(); ctx.moveTo(20,20); ctx.lineTo(20,100); ctx.lineTo(70,100); ctx.closePath(); ctx.stroke(); Note: no primitives other than fillRect()
Drawing a circle var c=document.getElementById("myCanvas"); var ctx=c.getContext("2d"); ctx.beginPath(); ctx.arc(95, 50, 40, 0, 2*Math.PI); ctx.stroke(); Why 2*PI?
The arc function arc(x, y, r, start, stop, antic);
Combination ctx.moveTo(0,0); ctx.lineTo(100, 200); ctx.arcTo(350,350,100,100,20);
BezierCurves ctx.moveTo(0,0); ctx.quadraticCurveTo(100, 25, 0, 50); // context.quadraticCurveTo(cpx, cpy, x, y)
clearRectcommand varc=document.getElementById("myCanvas"); varctx=c.getContext("2d"); ctx.fillStyle="red"; ctx.fillRect(0,0,300,150); ctx.clearRect(20, 20, 100, 50);
Dynamically load images var c=document.getElementById("myCanvas"); varctx=c.getContext("2d"); varhelloWorldImage = new Image(); helloWorldImage.src = "helloworld.gif”; helloWorldImage.onload= function () { ctx.drawImage(helloWorldImage, 160, 130); } What is this?
Dynamically load images 1. ctx.drawImage(img, dx, dy); 2. ctx.drawImage(img, dx, dy, dw, dh); 3. ctx.drawImage(img, sx, sy, sw, sh, dx, dy, dw, dh); What is this?
Using HTML img tags var c=document.getElementById("myCanvas"); varctx=c.getContext("2d"); varimg=document.getElementById("scream"); ctx.drawImage(img,10,10); … <img id="scream" src=”my_scream_image.jpg” /> <canvas> </canvas>
Capturing pixel data var c=document.getElementById("myCanvas"); var ctx=c.getContext("2d"); varformElement = document.getElementById("createImageData"); formElement.addEventListener('click', createImageDataPressed, false); function createImageDataPressed(e) { window.open( c.toDataURL() ); } <input type="button" id="createImageData" value="Export Image" />
Window parameters function createImageDataPressed(e) { window.open(c.toDataURL(),"canvasImage","left=0,top=0,width=" + c.width+ ",height=" + c.height +",toolbar=0,resizable=0"); }
The Canvas State • Each canvas context can save and restore multiple states • A state can be pushed onto a stack using the save() command • A state can be restored using the restore() command • A state has several properties: • Values of lineWIdth, fillStyle, strokeStyle… etc • The transformation matrix • Current clipping region
The Canvas State … ctx.strokeStyle= "red"; ctx.fillStyle= "yellow"; ctx.lineWidth= 10; ctx.fillRect(25,25,100,125); ctx.strokeRect(25,25,100,125); ctx.save(); ctx.strokeStyle= "green"; ctx.fillStyle= "blue"; ctx.lineWidth= 5; ctx.fillRect(175, 25, 100, 125); ctx.strokeRect(175, 25, 100, 125); ctx.restore(); ctx.fillRect(325, 25, 100, 125); ctx.strokeRect(325,25,100,125); 1. How many rectangles will there be? 2. What fill colour will the third rectangles have?
Shadows var c=document.getElementById("myCanvas"); varctx=c.getContext("2d"); ctx.shadowBlur=10; ctx.shadowOffsetX=20; ctx.shadowColor="black"; ctx.fillStyle="red"; ctx.fillRect(20,20,100,80);
Patterns var c=document.getElementById("myCanvas"); var ctx=c.getContext("2d"); var img=document.getElementById("lamp"); var pat=ctx.createPattern(img,"repeat"); ctx.rect(0,0,150,100); ctx.fillStyle=pat; ctx.fill();
Patterns var pat = ctx.createPattern(img,"repeat");
Gradients var c=document.getElementById("myCanvas"); varctx=c.getContext("2d"); vargrd = ctx.createLinearGradient(0,0,170,0); grd.addColorStop(0,"black"); grd.addColorStop(1,"white"); ctx.fillStyle = grd; ctx.fillRect(20,20,150,100);
Clipping paths ctx.rect(0, 0, 50, 50); ctx.clip(); // What happens here? // only pixel data will be rendered within the area set by the rect function
Canvas Transformations • Three basic transformation features • Translate • Scale • Rotate • NOTE WORTHY: • Transforms affect any subsequent drawing commands. • Transforms are additive (What does this mean?)
Translate translate(x, y); This function moves the 0,0 origin of the canvas.
Translate ctx.fillStyle = "blue"; ctx.fillRect(0,0,100,50); // translate origin to center of the canvas ctx.translate(ctx.canvas.width/2, ctx.canvas.height/2); ctx.fillRect(0,0,100,50);
Scale scale(x, y); Scale subsequent drawing commands by factors of x and y.
Scale ctx.fillRect(0, 0, 100, 50); ctx.scale(1.5, 2); ctx.fillRect(125, 50, 100, 50); // What will the height of the 2nd rectangle actually be?