Html canvas

RaoMubashar 224 views 14 slides Feb 09, 2018
Slide 1
Slide 1 of 14
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14

About This Presentation

Presentation of HTML Canvas


Slide Content

Topic HTML Canvas Members Mubashar

What is HTML5 Canvas Browser Support To draw something Canvas Examples Events Fillstyle G aming Outlines

What is HTML5 Canvas The HTML5 C anvas element is used to draw graphics via JavaScript. The Canvas element is only a container for graphics. You must use JavaScript to actually draw the graphics. Canvas used for drawing boxes, circles, text, and adding images etc.

Browser Support The latest versions of Firefox, Safari, Chrome and Opera all support for HTML5 Canvas but IE8 does not support canvas.

To draw something you need 4 basic components A Bitmap to hold the pixels . A Canvas to writing into the bitmap A drawing like rectangle, arc, line etc A paint to describe the colors and styles for the drawing

<!DOCTYPE html> <html> <head> <title>A Simple Canvas Example</title> <style> body { background:#CCC; } #canvas { margin: 10px; padding: 10px; background:#FFF; </style> </head> <body> <canvas id='canvas' width='900' height='100'> </canvas> <script src=" Untitled-1.js "></script> </body> </html> A basic Example var canvas = document.getElementById ('canvas'); context = canvas.getContext ('2d'); context.font = '38pt times of new roman'; context.fillStyle = ' cornflowerblue '; context.strokeStyle = 'blue'; context.fillText ('Hey I am Javed Akhtar', canvas.width /4 - 150, canvas.height /2 + 14); context.strokeText ('Hey I am Javed Akhtar', canvas.width /4 - 150, canvas.height /2 + 16 ); Java script code HTML Code

Events Mouse mousedown , mouseup , mousemove , mouseout and mouseover Keyboard keydown keypress keyup

Canvas default setting By default, the browser creates canvas elements with a width of 300 pixels and a height of 150 pixels. You can change the size of a canvas element by specifying the width and height attributes

Canvas Examples Canvas Animation Canvas Rotation drawImage (image , dx, dy ) Text and Fonts Create Gradients

A fillStyle Example <!DOCTYPE HTML><html> <head > <style> #test { width: 100px; height:100px; margin: 0px auto; } </style> <script type="text/ javascript "> function drawShape (){ var canvas = document.getElementById (' mycanvas '); if ( canvas.getContext ) { var ctx = canvas.getContext ('2d '); for ( var i =0;i<7;i++){ for ( var j=0;j<7;j ++){ ctx.fillStyle =' rgb (' + Math.floor (255-20.5* i )+ ','+ Math.floor (255 - 42.5*j) + ',255)'; ctx.fillRect ( j*25, i * 25, 55, 55 ); } } else { alert('You need Safari or Firefox 1.5+ to see this demo .');} } </script> </head> <body id="test" onload =" drawShape ();"> <canvas id=" mycanvas "></canvas> </body></html>

Gaming

Bounce ball game Create the Canvas and draw on it Move the ball Bounce off the walls Paddle and keyboard controls Game over Build the brick field Collision detection Track the score and win Mouse controls Finishing up

Create the Canvas and draw on it <!DOCTYPE html> <html> < head>< title> Gamedev Canvas Workshop</title > <style >* { padding: 0; margin: 0; } canvas { background: # eee ; display: block; margin: 0 auto; } </style ></ head> <body > <canvas id=" myCanvas " width="480" height="320"></canvas > < script > // JavaScript code goes here </script > </body> </html>
Tags