SlideShare a Scribd company logo
Topic
ʕ What is HTML5 Canvas
ʕ Browser Support
ʕ To draw something
ʕ Canvas Examples
ʕ Events
ʕ Fillstyle
ʕ Gaming
Outlines
What is HTML5 Canvas
The HTML5 Canvas 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>
Html canvas

More Related Content

PDF
Canvas - HTML 5
PPTX
HTML CANVAS
PPTX
HTML 5 Canvas & SVG
PPTX
HTML 5_Canvas
PPT
Canvas in html5 - TungVD
PPTX
Introduction to HTML5 Canvas
PPT
HTML5 Canvas
KEY
Interactive Graphics using Javascript, HTML5 and CSS3
Canvas - HTML 5
HTML CANVAS
HTML 5 Canvas & SVG
HTML 5_Canvas
Canvas in html5 - TungVD
Introduction to HTML5 Canvas
HTML5 Canvas
Interactive Graphics using Javascript, HTML5 and CSS3

What's hot (7)

PPTX
Advanced html5 diving into the canvas tag
PDF
Simplify your CSS with Stylus and Nib
PDF
Android Vector Drawable
PPTX
Canvas & Charts
KEY
Stupid Canvas Tricks
PDF
Responsive layouts by Maqbool
KEY
Responsive Web Design
Advanced html5 diving into the canvas tag
Simplify your CSS with Stylus and Nib
Android Vector Drawable
Canvas & Charts
Stupid Canvas Tricks
Responsive layouts by Maqbool
Responsive Web Design
Ad

Similar to Html canvas (20)

PPTX
Graphics on the Go
PPTX
Html5 canvas
PDF
Javascript #10 : canvas
PPTX
Canvas in html5
PPTX
introduction of HTML canvas and styles .pptx
PDF
Intro to HTML5
PPTX
HTML5 Canvas
PDF
Plunge into HTML5 Canvas – Let’s begin
PDF
Hello, Canvas.
PPTX
HTML5 Canvas - Basics.pptx
PPTX
JavaScript Canvas
PPT
Html5workshop
ODP
Graphics & Animation with HTML5
PPTX
Drawing with the HTML5 Canvas
PPTX
canvas.pptx
PPT
Game Development With HTML5
PPTX
Html5 Game Development with Canvas
ODP
Working With Canvas
PDF
Advanced Web Graphics with Canvas
PDF
HTML5 Canvas - The Future of Graphics on the Web
Graphics on the Go
Html5 canvas
Javascript #10 : canvas
Canvas in html5
introduction of HTML canvas and styles .pptx
Intro to HTML5
HTML5 Canvas
Plunge into HTML5 Canvas – Let’s begin
Hello, Canvas.
HTML5 Canvas - Basics.pptx
JavaScript Canvas
Html5workshop
Graphics & Animation with HTML5
Drawing with the HTML5 Canvas
canvas.pptx
Game Development With HTML5
Html5 Game Development with Canvas
Working With Canvas
Advanced Web Graphics with Canvas
HTML5 Canvas - The Future of Graphics on the Web
Ad

Recently uploaded (20)

PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Presentation on HIE in infants and its manifestations
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
Cell Types and Its function , kingdom of life
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Cell Structure & Organelles in detailed.
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Complications of Minimal Access Surgery at WLH
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
RMMM.pdf make it easy to upload and study
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Computing-Curriculum for Schools in Ghana
2.FourierTransform-ShortQuestionswithAnswers.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Presentation on HIE in infants and its manifestations
102 student loan defaulters named and shamed – Is someone you know on the list?
Cell Types and Its function , kingdom of life
Microbial disease of the cardiovascular and lymphatic systems
Cell Structure & Organelles in detailed.
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Complications of Minimal Access Surgery at WLH
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
GDM (1) (1).pptx small presentation for students
Supply Chain Operations Speaking Notes -ICLT Program
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
RMMM.pdf make it easy to upload and study
Abdominal Access Techniques with Prof. Dr. R K Mishra
Computing-Curriculum for Schools in Ghana

Html canvas

  • 2. ʕ What is HTML5 Canvas ʕ Browser Support ʕ To draw something ʕ Canvas Examples ʕ Events ʕ Fillstyle ʕ Gaming Outlines
  • 3. What is HTML5 Canvas The HTML5 Canvas 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.
  • 4. Browser Support The latest versions of Firefox, Safari, Chrome and Opera all support for HTML5 Canvas but IE8 does not support canvas.
  • 5. 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
  • 6. <!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
  • 7. Events  Mouse mousedown, mouseup, mousemove, mouseout and mouseover  Keyboard keydown keypress keyup
  • 8. 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
  • 9. Canvas Examples  Canvas Animation  Canvas Rotation  drawImage(image, dx, dy)  Text and Fonts  Create Gradients
  • 10. 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>
  • 12. 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
  • 13. 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>