29
HTML5 Graphics Drawing shapes with the <canvas> tag

04 html5 graphics

Embed Size (px)

Citation preview

Page 1: 04 html5 graphics

HTML5 Graphics Drawing shapes with the <canvas> tag

Page 2: 04 html5 graphics

Graphics were always limited to external images �  jpgs, gifs, pngs <img src="VaderAttacks.jpg" />!

�  There was no way to create dynamic or original graphics in HTML

Page 3: 04 html5 graphics

But HTML5 introduced the <canvas>

� Canvas allows

drawing just about

anything

<canvas id="

cv"

width="100"

height="100"

/>!

Page 4: 04 html5 graphics

The canvas is laid out with coordinates

� The origin is in the upper-left � Units are pixels

Page 5: 04 html5 graphics

We need some JavaScript to draw on the canvas function drawOnCanvas() {!

var canvas = document.getElementById("cv");!

var ctx = canvas.getContext("2d");!

ctx.fillRect(50, 25, 150, 100);!

}!

Page 6: 04 html5 graphics

fillRect(x, y, width, height) draws a rectangle

�  It uses the current fill style

Page 7: 04 html5 graphics

fillStyle and strokeStyle tells us what kind of paint we're using when we draw

� CSS color � A pattern � A gradient

Page 8: 04 html5 graphics

strokeRect(x, y, width, height) draws a rectangle with the current stroke style

�  fillRect draws it and fills it. strokeRect only draws the lines

Page 9: 04 html5 graphics

clearRect(x, y, width, height) un-draws the pixels in the

specified rectangle.

Page 10: 04 html5 graphics

Hands-on drawing rectangles

Page 11: 04 html5 graphics

You can erase the canvas by setting its width or height � You don’t even need to change the width  var cv = document.getElementById("cv");!

cv.width = cv.width; !

Page 12: 04 html5 graphics

Hands-on clearing the canvas

Page 13: 04 html5 graphics

We draw paths with moveTo(), lineTo() and stroke() � Merely moves the pen ctx.moveTo(100, 10);!

� Draws a line ctx.lineTo(200, 10);!

ctx.lineTo(200, 110);!

Page 14: 04 html5 graphics

But nothing actually gets drawn until you tell it to with stroke()

ctx.stroke();!

Page 15: 04 html5 graphics

Demo: Drawing lines Hands-on drawing lines

Page 16: 04 html5 graphics

You can draw curves with arc()

� An arc is a portion of a circle � This is how we'll draw ◦  circles ◦  arc segments

Page 17: 04 html5 graphics

Arc isn't easy to work with

arc(x, y, radius, startAngle, endAngle, direction); � Where: ◦  x, y: the center of the circle ◦  startAngle, endAngle: The angle in radians that the

arc starts and ends. 0=straight left ◦  direction: true=counterclockwise & false=clockwise

� Example: circle ctx.arc(x, y, radius, 0, Math.PI * 2, true); !

Page 18: 04 html5 graphics

Demo: Drawing arcs Hands-on drawing arcs

Page 19: 04 html5 graphics

There are some other ways to draw curves �  ctx.quadraticCurveTo(cpx,cpy,x,y); �  ctx.bezierCurveTo(cp1x,cp1y,cp2x,cp2y,x,y); �  ctx.arcTo(x1,y1,x2,y2,radius);

Page 20: 04 html5 graphics

We add text to our drawings with fillText()

�  context.fillText(textstring, x, y); context.font = "bold 12px arial";!context.fillText("Liz", 200, 40);!context.fillText("Lemon", 200, 60);!

Page 21: 04 html5 graphics

CSS styling is not available to the text on canvases � We can set a font,

color and size � But we cannot do it

with styles � No: ◦  padding ◦ margins ◦ wrapping

Page 22: 04 html5 graphics

Font can be anything you would put in a CSS font rule �  font style �  font variant �  font weight �  font size �  line height �  font family

Page 23: 04 html5 graphics

Demo: Adding text to drawings Hands-on adding text to drawings

Page 24: 04 html5 graphics

Gradients can add realism to drawings

var grad = !context.createLinearGradient(0,0,30,25);!grad.addColorStop(0, #0f0);!grad.addColorStop(1, #e49);!context.fillStyle = grad;!context.fillRect(0, 0, 300, 225);!

Page 25: 04 html5 graphics

Hands-on gradients

Page 26: 04 html5 graphics

Sometimes, though, we just need to put an image in our canvas

Page 27: 04 html5 graphics

drawImage() allows us to include an existing jpg, gif, or png

� Get the drawing first var vader = new Image();!

vader.src = "images/vaderAttacks.jpg";!

� Draw image starting at (x, y) ctx.drawImage(vader, x, y);!

� Draw image starting at (x, y) and scale it to (w, h)

drawImage(vader, x, y, w, h);!

Page 28: 04 html5 graphics

Demo: Adding images to your canvas Hands-on adding images

Page 29: 04 html5 graphics

Conclusion

� HTML5 canvas suddenly makes it possible to create drawings, games and other RIA without Flash, Silverlight or other plug-ins

� The low-level access to the canvas's context allows you to draw just about anything

� We can include text and images in addition to rectangles, lines, and arcs