SlideShare a Scribd company logo
HTML-5    Canvas
Agenda Where it has started What change can it bring? Understanding the lingo Basic usage Drawing shapes Using images Applying styles and colors Transformations Compositing Basic animations
History
Canvas vs SVG  High performance  Everything is a pixel.  Save the resulting image as a .png or .jpg. For generating raster graphics where pixel-level manipulation is needed. Resolution independence Elements can be animated using a declarative syntax, or via JavaScript. Full control over each element
Lingo
What it is not
First kicks <canvas id=&quot;stockGraph&quot; width=&quot;150&quot; height=&quot;150&quot;> You browser doesn't support canvas. </canvas> var canvas = document.getElementById('tutorial'); if (canvas.getContext){   var ctx = canvas.getContext('2d'); }
Basic shapes Rectangle : fillRect(x,y,width,height)  strokeRect(x,y,width,height)  clearRect(x,y,width,height) Circle: arc(x, y, radius, startAngle,  endAngle, anticlockwise)
Paths beginPath() closePath() stroke() Fill() moveTo(x, y) lineTo(x, y)
Exercise - 1
Solution ctx.beginPath(); ctx.arc(75,75,50,0,Math.PI*2,true);  // Outer circle ctx.moveTo(110,75); ctx.arc(75,75,35,0,Math.PI,false);  // Mouth(clockwise) ctx.moveTo(40,75); ctx.lineTo(110,75); ctx.moveTo(65,65); ctx.arc(60,65,5,0,Math.PI*2,true);  // Left eye ctx.moveTo(95,65); ctx.arc(90,65,5,0,Math.PI*2,true);  // Right eye ctx.stroke();
Beziere curves quadraticCurveTo(cp1x, cp1y, x, y)  bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y)
Styling lineWidth = value lineCap = type lineJoin = type miterLimit = value fillStyle = color strokeStyle = color ctx.globalAlpha = 0...1; createLinearGradient(x1,y1,x2,y2) createRadialGradient(x1,y1,r1,x2,y2,r2) addColorStop(position, color) var ptrn =ctx.createPattern(img,'repeat'); ctx.fillStyle = ptrn;
Images var img = new Image();  img.src = 'myImage.png'; // Set source path OR Img = 'data:image/gif;base64,R0lGO......' drawImage(image, sx, sy, sWidth, sHeight,  dx, dy, dWidth, dHeight)
Transformations translate(x, y) rotate(alpha)
Compositing  globalCompositeOperation = [type] ctx.beginPath(); drawAPath(); ctx.clip();
Animate Clear the canvas Save the canvas state Draw animated shapes Restore the canvas state setInterval(animateShape,500); setTimeout(animateShape,500);
Canvas vs Flash

More Related Content

PPT
Future of SaaS with HTML-5
Ganesh Gembali
 
PPT
Canvas
Ganesh Gembali
 
PPT
Agile for fixed price projects
Ganesh Gembali
 
PPT
Agile Myths agilencr2010
Ganesh Gembali
 
PPTX
HTML 5_Canvas
Vishakha Vaidya
 
PDF
HTML5 Canvas - The Future of Graphics on the Web
Robin Hawkes
 
PPT
HTML5 Canvas
Robyn Overstreet
 
PDF
Exploring Canvas
Kevin Hoyt
 
Future of SaaS with HTML-5
Ganesh Gembali
 
Agile for fixed price projects
Ganesh Gembali
 
Agile Myths agilencr2010
Ganesh Gembali
 
HTML 5_Canvas
Vishakha Vaidya
 
HTML5 Canvas - The Future of Graphics on the Web
Robin Hawkes
 
HTML5 Canvas
Robyn Overstreet
 
Exploring Canvas
Kevin Hoyt
 

Similar to Canvas (20)

KEY
Exploring Canvas
Kevin Hoyt
 
PDF
Html5 canvas
Gary Yeh
 
PPTX
canvas_1.pptx
RutujRunwal1
 
PPTX
Intro to Canva
dreambreeze
 
PPTX
Introduction to Canvas - Toronto HTML5 User Group
dreambreeze
 
PDF
Introduction to Canvas - Toronto HTML5 User Group
bernice-chan
 
PPTX
HTML 5 Canvas & SVG
Ofir's Fridman
 
PPTX
Css5 canvas
Vadim Spiridenko
 
PDF
Intro to HTML5 Canvas
Juho Vepsäläinen
 
PPTX
canvas.pptx
VeenaNaik23
 
PPTX
HTML5 Canvas - Basics.pptx
AhmadAbba6
 
PDF
JavaOne 2009 - 2d Vector Graphics in the browser with Canvas and SVG
Patrick Chanezon
 
PDF
Plunge into HTML5 Canvas – Let’s begin
Azilen Technologies Pvt. Ltd.
 
PPTX
Javascript Canvas API
Samuel Santos
 
PPT
Html5 Canvas Drawing and Animation
Mindfire Solutions
 
ODP
Working With Canvas
Diogo Antunes
 
PDF
How to build a html5 websites.v1
Bitla Software
 
PPT
Rotoscope inthebrowserppt billy
nimbleltd
 
KEY
Web Grafik Technologien
n3xd software studios ag
 
Exploring Canvas
Kevin Hoyt
 
Html5 canvas
Gary Yeh
 
canvas_1.pptx
RutujRunwal1
 
Intro to Canva
dreambreeze
 
Introduction to Canvas - Toronto HTML5 User Group
dreambreeze
 
Introduction to Canvas - Toronto HTML5 User Group
bernice-chan
 
HTML 5 Canvas & SVG
Ofir's Fridman
 
Css5 canvas
Vadim Spiridenko
 
Intro to HTML5 Canvas
Juho Vepsäläinen
 
canvas.pptx
VeenaNaik23
 
HTML5 Canvas - Basics.pptx
AhmadAbba6
 
JavaOne 2009 - 2d Vector Graphics in the browser with Canvas and SVG
Patrick Chanezon
 
Plunge into HTML5 Canvas – Let’s begin
Azilen Technologies Pvt. Ltd.
 
Javascript Canvas API
Samuel Santos
 
Html5 Canvas Drawing and Animation
Mindfire Solutions
 
Working With Canvas
Diogo Antunes
 
How to build a html5 websites.v1
Bitla Software
 
Rotoscope inthebrowserppt billy
nimbleltd
 
Web Grafik Technologien
n3xd software studios ag
 
Ad

Canvas

  • 1. HTML-5 Canvas
  • 2. Agenda Where it has started What change can it bring? Understanding the lingo Basic usage Drawing shapes Using images Applying styles and colors Transformations Compositing Basic animations
  • 4. Canvas vs SVG High performance Everything is a pixel. Save the resulting image as a .png or .jpg. For generating raster graphics where pixel-level manipulation is needed. Resolution independence Elements can be animated using a declarative syntax, or via JavaScript. Full control over each element
  • 7. First kicks <canvas id=&quot;stockGraph&quot; width=&quot;150&quot; height=&quot;150&quot;> You browser doesn't support canvas. </canvas> var canvas = document.getElementById('tutorial'); if (canvas.getContext){ var ctx = canvas.getContext('2d'); }
  • 8. Basic shapes Rectangle : fillRect(x,y,width,height) strokeRect(x,y,width,height) clearRect(x,y,width,height) Circle: arc(x, y, radius, startAngle, endAngle, anticlockwise)
  • 9. Paths beginPath() closePath() stroke() Fill() moveTo(x, y) lineTo(x, y)
  • 11. Solution ctx.beginPath(); ctx.arc(75,75,50,0,Math.PI*2,true); // Outer circle ctx.moveTo(110,75); ctx.arc(75,75,35,0,Math.PI,false); // Mouth(clockwise) ctx.moveTo(40,75); ctx.lineTo(110,75); ctx.moveTo(65,65); ctx.arc(60,65,5,0,Math.PI*2,true); // Left eye ctx.moveTo(95,65); ctx.arc(90,65,5,0,Math.PI*2,true); // Right eye ctx.stroke();
  • 12. Beziere curves quadraticCurveTo(cp1x, cp1y, x, y) bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y)
  • 13. Styling lineWidth = value lineCap = type lineJoin = type miterLimit = value fillStyle = color strokeStyle = color ctx.globalAlpha = 0...1; createLinearGradient(x1,y1,x2,y2) createRadialGradient(x1,y1,r1,x2,y2,r2) addColorStop(position, color) var ptrn =ctx.createPattern(img,'repeat'); ctx.fillStyle = ptrn;
  • 14. Images var img = new Image(); img.src = 'myImage.png'; // Set source path OR Img = 'data:image/gif;base64,R0lGO......' drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight)
  • 16. Compositing globalCompositeOperation = [type] ctx.beginPath(); drawAPath(); ctx.clip();
  • 17. Animate Clear the canvas Save the canvas state Draw animated shapes Restore the canvas state setInterval(animateShape,500); setTimeout(animateShape,500);