canvas tag by abhishek parolkar

24
 

Upload: abhishek-parolkar

Post on 25-Jun-2015

2.123 views

Category:

Technology


4 download

DESCRIPTION

A presentation on canvas tag

TRANSCRIPT

Page 1: Canvas Tag By Abhishek Parolkar

   

Page 2: Canvas Tag By Abhishek Parolkar

   

Who am I?

Page 3: Canvas Tag By Abhishek Parolkar

   

Scope of this talk

{    W3C 

   WHATWG      Web2.0

<canvas>What? , Why? , How?//your comments/*my opinions*/./demos

</canvas><canvas> Vs All enemies (Adobe Flash/ Flex, Java Applets, 

Silverlight...) };

Page 4: Canvas Tag By Abhishek Parolkar

   

W3C == WWWC  == World Wide Web Consortium 

The big brother  who  gave us  “HTML”  specification so that we all (web browsers) talk in same manner.

HTML 4 – A stable specification. 

DOM Level 2/3 

XML*

XHTML 

XForms 

Page 5: Canvas Tag By Abhishek Parolkar

   

WHATWG ­ Web Hypertext Application Technology Working Group

“In 2004, after a W3C workshop, Apple, Mozilla and Opera were becoming increasingly concerned about the W3C™s direction with XHTML, lack of interest in HTML and apparent disregard for the needs of real­world authors. So, in response, these organisations set out to with a mission to address these concerns and the Web Hypertext Application Technology Working Group was born.”

­ HTML 5  (Not a complete specification yet )

Page 6: Canvas Tag By Abhishek Parolkar

   

The story begins!

Page 7: Canvas Tag By Abhishek Parolkar

   

...When there was hardly any meaning for “Interactive Media”...

...and internet bandwidth was subject of luxury...

...HTML was just “Hyper Text Markup Language”

we browsers were helpless creatures...

Page 8: Canvas Tag By Abhishek Parolkar

   

..then came JAVA (HOT JAVA) , FLASH. 

and ECMA Script (JavaScript)

..and DHTML heaven

...a powerful Document Object Model Interface

Dynamic content at client­side became reality...

Page 9: Canvas Tag By Abhishek Parolkar

   

Websites evolved to be interactive sites / multimedia websites

 But.... 

Page 10: Canvas Tag By Abhishek Parolkar

   

Limitations

­ Content was mostly plug­in based.­ Non Standard protocols for communication­ Programmers became “Web Developers”­ ...then came many other mark up languages for each purpose­ People were just playing with work­arounds , No clearly thought out solution for serving interactive content.

Web developers could never enjoy freedom that desktop IU hackers have

Page 11: Canvas Tag By Abhishek Parolkar

   

Desktop UI Hacker Web UI Hacker

I am a creative designer

I build new UI controlsI draw directly on graphicsbuffer at pixel levels

u know, Its so much funwhen I can manage and optimise my graphics hardware capabilities

I have choice of libraries 

My app works fine every where

Me too, I like UI hacking 

Oh Wow!, I do it by writing java applet/flash app.

Hmmm.....

:­) hmmmmmm...

 I tell my users to install/update pllug­in , it works fine for them

Page 12: Canvas Tag By Abhishek Parolkar

   

Introducing <Canvas> Tag

Page 13: Canvas Tag By Abhishek Parolkar

   

<canvas id="tutorial" width="150" height="150">Its funny! , your browser doesnt support canvas yet.

</canvas>

<canvas id="stockGraph" width="150" height="150">  current stock price: $3.15 +0.15</canvas>

<canvas id="clock" width="150" height="150">  <img src="images/clock.png" width="150" height="150"/></canvas>

Page 14: Canvas Tag By Abhishek Parolkar

   

 getContext ­Thats all you need to take control

var canvas = document.getElementById('canvas');var ctx = canvas.getContext('2d');

Page 15: Canvas Tag By Abhishek Parolkar

   

Document structure<html>  <head>    <title>Canvas tutorial</title>    <script type="text/javascript">      function draw(){        var canvas = document.getElementById('mycanvas');        if (canvas.getContext){          var ctx = canvas.getContext('2d');        }      }    </script>    <style type="text/css">      canvas { border: 1px solid black; }    </style>  </head>  <body onload="draw();">    <canvas id="mycanvas" width="150" height="150"></canvas>  </body></html>

Page 16: Canvas Tag By Abhishek Parolkar

   

Basics:

 The grid:top left corner (coordinate (0,0)) Unlike SVG, canvas only supports one primitive shape ­ rectangles. 

fillRect(x,y,width,height) : Draws a filled rectanglestrokeRect(x,y,width,height) : Draws a rectangular outlineclearRect(x,y,width,height) : Clears the specified area and makes it fully transparent

Page 17: Canvas Tag By Abhishek Parolkar

   

Free Hand Drawing

ctx.beginPath();ctx.moveTo(75,50);ctx.lineTo(100,75);ctx.lineTo(100,25);ctx.fill();

arc(x, y, radius, startAngle, endAngle, anticlockwise)bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y)

Page 18: Canvas Tag By Abhishek Parolkar

   

Drawing/ Scaling /Import Graphics

drawImage(image, x, y, width, height);drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight)

Page 19: Canvas Tag By Abhishek Parolkar

   

... Coloring my world ...ctx.fillStyle = "orange";ctx.fillStyle = "#FFA500";ctx.fillStyle = "rgb(255,165,0)";ctx.fillStyle = "rgba(255,165,0,1)";

...Transparency...globalAlpha = transparency value

...Gradients...createLinearGradient(x1,y1,x2,y2)createRadialGradient(x1,y1,r1,x2,y2,r2)

...Pattern...createPattern(image,type)

Page 20: Canvas Tag By Abhishek Parolkar

   

 Saving and restoring state

save()restore()

Page 21: Canvas Tag By Abhishek Parolkar

   

Transformationstranslate(x, y)rotate(angle)scale(x, y)

CompositingglobalCompositeOperation = type[source­over (default),source­in,source­out,source­atop,lighter,copy,xor,darker]

clip()

Page 22: Canvas Tag By Abhishek Parolkar

   

 AnimationSteps:   1. Clear the canvas       2. Save the canvas state        3. Draw animated shapes       4. Restore the canvas state        5. Repeat

Page 23: Canvas Tag By Abhishek Parolkar

   

 </canvas>

Thank You

Page 24: Canvas Tag By Abhishek Parolkar

   

I am just a click away [email protected]

#(freenode)IRC: parolkar