SlideShare une entreprise Scribd logo
1  sur  32
Télécharger pour lire hors ligne
Intro to HTML5 Canvas
       Juho Vepsäläinen
Gonna Talk About These
  Topics You Betcha
Canvas (Overview)

Canvas 2D Context (The Beefcake)

Canvas Tricks (For Fun and Profit)

Canvas Demos (To Recap Concepts Discussed)
Canvas
Overview
http://dev.w3.org/html5/2dcontext/




        Just Google it. :)
Definition

Immediate-mode API and associated utility methods
   for drawing two-dimensional vector graphics
             to a raster drawing area.



  Draw and forget API and utils for 2D drawing.
Markup


                     <canvas></canvas>

       <canvas width="800" height="600"></canvas>

<canvas width="800" height="600">No canvas for you!</canvas>
Element Access


Attributes: width, height

Method: getContext (for drawing), toDataURL (for
saving)
Show some apps and libs now.
Canvas 2D Context
    The Beefcake
Get Some Context


var canvas = document.getElementById("myCanvas");

var ctx = canvas.getContext("2d");
Context Functionality
It's a state machine

Read-only ref back to canvas (attr)

save/restore + ops

Ops: transformations, compositing, colors and styles,
line caps/joins, shadows, rects, paths, text, drawing
images, pixel manipulation + misc. crud I won't cover
Context for Dummies

1. Set some states (transformation, color, ie.)

2. Draw (lines, whatnot)

3. ???

4. Profit
Default Context
    (0, 0)                     x




y
Transformed Context
    (0, 0)                                            x




         ctx.scale(x, y)
         ctx.rotate(rad)
         ctx.translate(x, y)
y        ctx.transform/setTransform(a,b,c,d,e,f) (set resets)
Compositing
ctx.globalAlpha = 0.38;




          ctx.globalCompositeOperation = "source-over";
Colors and Styles - stroke/
           fill
ctx.strokeStyle = "black";




                             ctx.fillStyle = "yellow";
Colors and Styles -
               Gradients
ctx.createLinearGradient(x0,y0,x1,y1)




                ctx.createRadialGradient(x0,y0,r0,x1,y1,r1)
 ctx.addColorStop(offset, color);
Colors and Styles - Patterns
    ctx.createPattern(catImg, 'repeat-x');
Line Caps/Joins

ctx.lineWidth = 12;
ctx.lineCap = "square";
ctx.lineJoin = "miter";
ctx.miterLimit = 10;
Shadows

ctx.shadowOffsetX = 5;
ctx.shadowOffsetY = 5;
ctx.shadowBlur = 3;
ctx.shadowColor = 'grey';
Rectangles
ctx.clearRect/fillRect/strokeRect(x,y,w,h)
Paths
ctx.beginPath();

ctx.moveTo(x, y); // initial pos

// define curve
ctx.lineTo/quadraticCurveTo/bezierCurveTo/
    arcTo/arc/rect
...

ctx.closePath();

ctx.fill/stroke/clip();
Text

ctx.font = "24px sans-serif ";
ctx.textAlign = "center";

ctx.fillText/strokeText(text,x,y,maxWidth);
Drawing Images
 ctx.drawImage(img/canvas/video, lots of alternatives);




Supports compositing! Use this to your advantage.
Pixel Manipulation
  ctx.createImageData/getImageData/putImageData
  var data = ctx.getImageData(0, 0, w, h);

  var realData = data.data;
  for(var y = 0, pos = 0; y < h; y++) {
    for(var x = 0; x < w; x++, pos+=4) {
       realData[pos + 2] *= 0.5; // modify Blue channel
    }
  }
  data.data = realData;

Friggin' slow! Avoid if possible. Optimize usage.
Canvas Tricks
For Fun and Profit
Blurred Lines

Basic idea: Use line shadow, offset actual line
            so that it isn't visible.
Multiple Layers


Basic idea: CSS z-index + absolute positioning.
Erasing


Basic idea: Use destination-out compositing op.
CSS Fun

Basic idea: Play around with CSS opacity
and transformations (incurs perf penalty).
Canvas Demos
To recap concepts discussed
Demo time.

Contenu connexe

Tendances

Exploring Canvas
Exploring CanvasExploring Canvas
Exploring Canvas
Kevin Hoyt
 
5 tips for your HTML5 games
5 tips for your HTML5 games5 tips for your HTML5 games
5 tips for your HTML5 games
Ernesto Jiménez
 
Android 2D Drawing and Animation Framework
Android 2D Drawing and Animation FrameworkAndroid 2D Drawing and Animation Framework
Android 2D Drawing and Animation Framework
Jussi Pohjolainen
 
Interactive Graphics
Interactive GraphicsInteractive Graphics
Interactive Graphics
Blazing Cloud
 
Willustrator
WillustratorWillustrator
Willustrator
2da
 
Interactive Graphics using Javascript, HTML5 and CSS3
Interactive Graphics using Javascript, HTML5 and CSS3Interactive Graphics using Javascript, HTML5 and CSS3
Interactive Graphics using Javascript, HTML5 and CSS3
Lee Lundrigan
 

Tendances (20)

Css5 canvas
Css5 canvasCss5 canvas
Css5 canvas
 
How to make a video game
How to make a video gameHow to make a video game
How to make a video game
 
Exploring Canvas
Exploring CanvasExploring Canvas
Exploring Canvas
 
Stupid Canvas Tricks
Stupid Canvas TricksStupid Canvas Tricks
Stupid Canvas Tricks
 
Exploring Canvas
Exploring CanvasExploring Canvas
Exploring Canvas
 
The Canvas Tag
The Canvas TagThe Canvas Tag
The Canvas Tag
 
Android Vector Drawable
 Android Vector Drawable Android Vector Drawable
Android Vector Drawable
 
SVGo workshop
SVGo workshopSVGo workshop
SVGo workshop
 
5 tips for your HTML5 games
5 tips for your HTML5 games5 tips for your HTML5 games
5 tips for your HTML5 games
 
Android 2D Drawing and Animation Framework
Android 2D Drawing and Animation FrameworkAndroid 2D Drawing and Animation Framework
Android 2D Drawing and Animation Framework
 
HTML5 Canvas - Let's Draw!
HTML5 Canvas - Let's Draw!HTML5 Canvas - Let's Draw!
HTML5 Canvas - Let's Draw!
 
Getting Visual with Ruby Processing
Getting Visual with Ruby ProcessingGetting Visual with Ruby Processing
Getting Visual with Ruby Processing
 
Interactive Graphics
Interactive GraphicsInteractive Graphics
Interactive Graphics
 
Leaving Flatland: getting started with WebGL
Leaving Flatland: getting started with WebGLLeaving Flatland: getting started with WebGL
Leaving Flatland: getting started with WebGL
 
Prototype UI Intro
Prototype UI IntroPrototype UI Intro
Prototype UI Intro
 
Willustrator
WillustratorWillustrator
Willustrator
 
The Web map stack on Django
The Web map stack on DjangoThe Web map stack on Django
The Web map stack on Django
 
Interactive Graphics using Javascript, HTML5 and CSS3
Interactive Graphics using Javascript, HTML5 and CSS3Interactive Graphics using Javascript, HTML5 and CSS3
Interactive Graphics using Javascript, HTML5 and CSS3
 
Kotlin Mullets
Kotlin MulletsKotlin Mullets
Kotlin Mullets
 
UIWebViewでつくるUI
UIWebViewでつくるUIUIWebViewでつくるUI
UIWebViewでつくるUI
 

Similaire à Intro to HTML5 Canvas

Html5 Canvas Drawing and Animation
Html5 Canvas Drawing and AnimationHtml5 Canvas Drawing and Animation
Html5 Canvas Drawing and Animation
Mindfire Solutions
 
How to build a html5 websites.v1
How to build a html5 websites.v1How to build a html5 websites.v1
How to build a html5 websites.v1
Bitla Software
 
Rotoscope inthebrowserppt billy
Rotoscope inthebrowserppt billyRotoscope inthebrowserppt billy
Rotoscope inthebrowserppt billy
nimbleltd
 

Similaire à Intro to HTML5 Canvas (20)

Html5 Canvas Drawing and Animation
Html5 Canvas Drawing and AnimationHtml5 Canvas Drawing and Animation
Html5 Canvas Drawing and Animation
 
SVGo: a Go Library for SVG generation
SVGo: a Go Library for SVG generationSVGo: a Go Library for SVG generation
SVGo: a Go Library for SVG generation
 
Google I/O 2013 - Android Graphics Performance
Google I/O 2013 - Android Graphics PerformanceGoogle I/O 2013 - Android Graphics Performance
Google I/O 2013 - Android Graphics Performance
 
Google I/O 2013 - Android Graphics Performance
Google I/O 2013 - Android Graphics PerformanceGoogle I/O 2013 - Android Graphics Performance
Google I/O 2013 - Android Graphics Performance
 
Introduction to d3js (and SVG)
Introduction to d3js (and SVG)Introduction to d3js (and SVG)
Introduction to d3js (and SVG)
 
How to build a html5 websites.v1
How to build a html5 websites.v1How to build a html5 websites.v1
How to build a html5 websites.v1
 
Canvas
CanvasCanvas
Canvas
 
Writing a Space Shooter with HTML5 Canvas
Writing a Space Shooter with HTML5 CanvasWriting a Space Shooter with HTML5 Canvas
Writing a Space Shooter with HTML5 Canvas
 
Visualization of Big Data in Web Apps
Visualization of Big Data in Web AppsVisualization of Big Data in Web Apps
Visualization of Big Data in Web Apps
 
Advanced html5 diving into the canvas tag
Advanced html5 diving into the canvas tagAdvanced html5 diving into the canvas tag
Advanced html5 diving into the canvas tag
 
HTML5 Canvas - Basics.pptx
HTML5 Canvas - Basics.pptxHTML5 Canvas - Basics.pptx
HTML5 Canvas - Basics.pptx
 
Better d3 charts with tdd
Better d3 charts with tddBetter d3 charts with tdd
Better d3 charts with tdd
 
IE9에서 HTML5 개발하기
IE9에서 HTML5 개발하기IE9에서 HTML5 개발하기
IE9에서 HTML5 개발하기
 
Scmad Chapter06
Scmad Chapter06Scmad Chapter06
Scmad Chapter06
 
Rotoscope inthebrowserppt billy
Rotoscope inthebrowserppt billyRotoscope inthebrowserppt billy
Rotoscope inthebrowserppt billy
 
Massimo Artizzu - The tricks of Houdini: a magic wand for the future of CSS -...
Massimo Artizzu - The tricks of Houdini: a magic wand for the future of CSS -...Massimo Artizzu - The tricks of Houdini: a magic wand for the future of CSS -...
Massimo Artizzu - The tricks of Houdini: a magic wand for the future of CSS -...
 
Canvas
CanvasCanvas
Canvas
 
Canvas
CanvasCanvas
Canvas
 
Professional reports with SVG
Professional reports with SVGProfessional reports with SVG
Professional reports with SVG
 
Google tools for webmasters
Google tools for webmastersGoogle tools for webmasters
Google tools for webmasters
 

Plus de Juho Vepsäläinen

Plus de Juho Vepsäläinen (13)

Web application development - The past, the present, the future
Web application development - The past, the present, the futureWeb application development - The past, the present, the future
Web application development - The past, the present, the future
 
ECMAScript - From an idea to a major standard
ECMAScript - From an idea to a major standardECMAScript - From an idea to a major standard
ECMAScript - From an idea to a major standard
 
Quick introduction to Qwik
Quick introduction to QwikQuick introduction to Qwik
Quick introduction to Qwik
 
fooConf - JavaScript frameworks of tomorrow
fooConf - JavaScript frameworks of tomorrowfooConf - JavaScript frameworks of tomorrow
fooConf - JavaScript frameworks of tomorrow
 
JavaScript frameworks of tomorrow
JavaScript frameworks of tomorrowJavaScript frameworks of tomorrow
JavaScript frameworks of tomorrow
 
The future is mostly static
The future is mostly staticThe future is mostly static
The future is mostly static
 
Web application development - The past, the present, the future
Web application development - The past, the present, the futureWeb application development - The past, the present, the future
Web application development - The past, the present, the future
 
The future is mostly static
The future is mostly staticThe future is mostly static
The future is mostly static
 
Survive JavaScript - Strategies and Tricks
Survive JavaScript - Strategies and TricksSurvive JavaScript - Strategies and Tricks
Survive JavaScript - Strategies and Tricks
 
Speccer
SpeccerSpeccer
Speccer
 
bongaus.fi - Spotting Service Powered by Django
bongaus.fi - Spotting Service Powered by Djangobongaus.fi - Spotting Service Powered by Django
bongaus.fi - Spotting Service Powered by Django
 
Bootstrap vs. Skeleton
Bootstrap vs. SkeletonBootstrap vs. Skeleton
Bootstrap vs. Skeleton
 
Static Websites - The Final Frontier
Static Websites - The Final FrontierStatic Websites - The Final Frontier
Static Websites - The Final Frontier
 

Dernier

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Dernier (20)

Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 

Intro to HTML5 Canvas