SlideShare a Scribd company logo
HTML5 Canvas
HTML5 Canvas - Let's Draw!
HTML5 Canvas - Let's Draw!
HTML5 Canvas - Let's Draw!
The canvas is
a 2d bitmap.
No   layers.
No   undo.
No   objects.
No   moving stuff.
http://rawkes.com/lab/google-balls-logo
http://blog.nihilogic.dk/2008/04/javascript-wolfenstein-3d.html
How  can I
 create a
 c anvas?
HTML
<canvas id="a">fallback txt</canvas>




JavaScript
var canvas =
  document.getElementsById('a');

var ctx = canvas.getContext('2d');
I draw
s quares.
con text
  ctx.fillStyle = 'blue';  he ight
  ctx.fillRect(0, 0, 150, 50);
               x y width




  ctx.strokeStyle = '#c66';
  ctx.lineWidth = 1;
  ctx.strokeRect(0.5, 60.5, 150, 50);
Iwant to
 d raw an
    image!
create image
var img = new Image();
img.onload = function() { w hen loaded
   ctx.drawImage(img, 0, 0);
};                     x  y
img.src = 'nakedrobot.png';
                          image url
how can you
  draw text?
css p roperties
ctx.font = 'bold 40px sans-serif';
ctx.fillText('hello kitty', 5, 40);
                            x  y

hello kitty
bring me
the pixels!
var w = canvas.width;
var h = canvas.height;
                                 new image array
var img = ctx.createImageData(w, h);
for (var x = 0; x < w; x++) {
    for (var y = 0; y < h; y++) {

        // each pixel has four values
        var idx = (x + y * w) * 4;

        // 0 red, 1 green, 2 blue, 3 alpha
        img.data[idx + 0] = x;
        img.data[idx + 1] = 255-x;
        img.data[idx + 2] = 0;
        img.data[idx + 3] = 255;
    }
}                                          result

ctx.putImageData(img, 0, 0);   write to canv
                                             as
http://media.chikuyonok.ru/ambilight/
close pixealte
http://desandro.com/resources/close-pixelate/
A nimation


 function ti
             ck() {
    doKeyboard(
                );
   updatePlaye
               r();
   doAI();
   draw();
  audio(); }
var fps = 3
            0;
setInterval
            (tick, 1000
                        /fps)   ;
get
       input




draw           update




every 30ms
function tick() {
	 doKeyboard();
	 updatePlayer();
	 doAI();
	 draw();
  audio();
}

var fps = 30;
setInterval(tick, 1000/fps);
How can
i Save an
Image?
var img = document.
  getElementsByTagName('img')[0];

var canvasImg =
  canvas.toDataURL("image/png");
                       returns “data:ima
                                         ge/png;base64,
img.src = canvasImg;           iVBORw0KGgoAAAA
                                                 NS
                               MgAAADICAYAAACt...” UhEU




  canvas      image
Browser
Support   67%
HTML5 Canvas - Let's Draw!
http://weavesilk.com/
Summary Canvas
<canvas> is a new 2d bitmap html element. you can draw primitives, images
and pixels. There are no layers or object. If a pixel gets a new color, the
old color is overwritten and lost.

A canvas is not “sandboxed” like flash and can be accessed and drawn on
with javascript.
Material Used
            Banksy “Have a Break” http://www.flickr.com/photos/loungerie/4109421950/

            Banksy “Flowing off” http://www.flickr.com/photos/loungerie/4109420324/

         Banksy “They’re Coming” http://www.flickr.com/photos/97041449@N00/4115205218/

      Bansky “Tourqay Robot” http://en.wikipedia.org/wiki/File:Banksy_Torquay_robot_crop.jpg

                  Banksy “You have got to be kidding me” http://www.banksy.co.uk/

        Banksy “follow your Dream” http://www.flickr.com/photos/thomashawk/6343586111/

          Banksy “nola” http://www.flickr.com/photos/eddiedangerous/3045255243/

 Banksy “Flower Pollard Street” http://www.flickr.com/photos/eddiedangerous/1800573742/

Banksy “what are you looking at?” http://www.flickr.com/photos/nolifebeforecoffee/124659356/

   Banksy “Man and Picture of a dog” http://www.flickr.com/photos/atomicshed/141462789/

   Banksy “Anti-Capitalism for sale” http://www.flickr.com/photos/jcodysimms/246024687/
Material Used
       3d teapot model http://resumbrae.com/ub/dms423_f08/10/

 Metal Teapot http://www.flickr.com/photos/11273631@N08/2867342497/

 furry teapot http://www.flickr.com/photos/11273631@N08/2867342461/

 Television Icon http://thenounproject.com/noun/television/#icon-No416

 Graphics Card http://www.flickr.com/photos/43779660@N00/218093887/

 Banksy “under the Carpet” http://www.flickr.com/photos/acb/147223287/

  Boxing http://www.flickr.com/photos/51035655711@N01/2826228569/

More Related Content

What's hot (20)

KEY
5 tips for your HTML5 games
Ernesto Jiménez
 
KEY
Leaving Flatland: getting started with WebGL
gerbille
 
PPTX
Drawing with the HTML5 Canvas
Henry Osborne
 
PDF
A More Flash Like Web?
Murat Can ALPAY
 
PPTX
YUI for your Hacks
Subramanyan Murali
 
PPT
HTML5 Canvas
Robyn Overstreet
 
PDF
HTML5 - Daha Flash bir web?
Ankara JUG
 
PDF
Html5 canvas
Gary Yeh
 
PDF
Interactive web with Fabric.js @ meet.js
Juriy Zaytsev
 
PPTX
How to make a video game
dandylion13
 
PDF
Test driven game development silly, stupid or inspired?
Eric Smith
 
PDF
Test Driven Cocos2d
Eric Smith
 
PPTX
Making Games in JavaScript
Sam Cartwright
 
PDF
Pixelplant - WebDev Meetup Salzburg
wolframkriesing
 
PPTX
HTML 5 Canvas & SVG
Ofir's Fridman
 
PDF
Зависимые типы в GHC 8. Максим Талдыкин
Юрий Сыровецкий
 
PDF
Three.js basics
Vasilika Klimova
 
PPT
Machine Tags
hchen1
 
PDF
3D everywhere
Vasilika Klimova
 
PPTX
HTML 5_Canvas
Vishakha Vaidya
 
5 tips for your HTML5 games
Ernesto Jiménez
 
Leaving Flatland: getting started with WebGL
gerbille
 
Drawing with the HTML5 Canvas
Henry Osborne
 
A More Flash Like Web?
Murat Can ALPAY
 
YUI for your Hacks
Subramanyan Murali
 
HTML5 Canvas
Robyn Overstreet
 
HTML5 - Daha Flash bir web?
Ankara JUG
 
Html5 canvas
Gary Yeh
 
Interactive web with Fabric.js @ meet.js
Juriy Zaytsev
 
How to make a video game
dandylion13
 
Test driven game development silly, stupid or inspired?
Eric Smith
 
Test Driven Cocos2d
Eric Smith
 
Making Games in JavaScript
Sam Cartwright
 
Pixelplant - WebDev Meetup Salzburg
wolframkriesing
 
HTML 5 Canvas & SVG
Ofir's Fridman
 
Зависимые типы в GHC 8. Максим Талдыкин
Юрий Сыровецкий
 
Three.js basics
Vasilika Klimova
 
Machine Tags
hchen1
 
3D everywhere
Vasilika Klimova
 
HTML 5_Canvas
Vishakha Vaidya
 

Viewers also liked (6)

PDF
An Introduction to Jquery
Phil Reither
 
PDF
WebGL - 3D in your Browser
Phil Reither
 
PDF
Responsive Web Design Patterns
Phil Reither
 
PDF
Docker: Fire your Sysadmin and use Docker to build, ship and run any app, any...
Phil Reither
 
PDF
Web Design Trends 2014
Creative Spark, an M&C Saatchi Company
 
PPTX
Top 10 Web Design Trends for 2015
Accrinet Corporation
 
An Introduction to Jquery
Phil Reither
 
WebGL - 3D in your Browser
Phil Reither
 
Responsive Web Design Patterns
Phil Reither
 
Docker: Fire your Sysadmin and use Docker to build, ship and run any app, any...
Phil Reither
 
Top 10 Web Design Trends for 2015
Accrinet Corporation
 
Ad

Similar to HTML5 Canvas - Let's Draw! (20)

PDF
Is HTML5 Ready? (workshop)
Remy Sharp
 
PDF
Is html5-ready-workshop-110727181512-phpapp02
PL dream
 
PPT
Game Development With HTML5
Gil Megidish
 
PDF
Canvas - The Cure
Robert Nyman
 
PDF
Mapping the world with Twitter
carlo zapponi
 
PDF
HTML5 Canvas - The Future of Graphics on the Web
Robin Hawkes
 
PDF
How to build a html5 websites.v1
Bitla Software
 
KEY
Canvas: we must go deeper
ardcore
 
PPT
Canvas in html5 - TungVD
Framgia Vietnam
 
PPT
Standards.Next: Canvas
Martin Kliehm
 
PDF
Writing a Space Shooter with HTML5 Canvas
Steve Purkis
 
PDF
Google's HTML5 Work: what's next?
Patrick Chanezon
 
KEY
Exploring Canvas
Kevin Hoyt
 
PPTX
Introduction to Canvas - Toronto HTML5 User Group
dreambreeze
 
PDF
Introduction to Canvas - Toronto HTML5 User Group
bernice-chan
 
ODP
Working With Canvas
Diogo Antunes
 
PPTX
introduction of HTML canvas and styles .pptx
hannahroseline2
 
PPTX
Intro to Canva
dreambreeze
 
PDF
Exploring Canvas
Kevin Hoyt
 
Is HTML5 Ready? (workshop)
Remy Sharp
 
Is html5-ready-workshop-110727181512-phpapp02
PL dream
 
Game Development With HTML5
Gil Megidish
 
Canvas - The Cure
Robert Nyman
 
Mapping the world with Twitter
carlo zapponi
 
HTML5 Canvas - The Future of Graphics on the Web
Robin Hawkes
 
How to build a html5 websites.v1
Bitla Software
 
Canvas: we must go deeper
ardcore
 
Canvas in html5 - TungVD
Framgia Vietnam
 
Standards.Next: Canvas
Martin Kliehm
 
Writing a Space Shooter with HTML5 Canvas
Steve Purkis
 
Google's HTML5 Work: what's next?
Patrick Chanezon
 
Exploring Canvas
Kevin Hoyt
 
Introduction to Canvas - Toronto HTML5 User Group
dreambreeze
 
Introduction to Canvas - Toronto HTML5 User Group
bernice-chan
 
Working With Canvas
Diogo Antunes
 
introduction of HTML canvas and styles .pptx
hannahroseline2
 
Intro to Canva
dreambreeze
 
Exploring Canvas
Kevin Hoyt
 
Ad

Recently uploaded (20)

PDF
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
PPTX
Simple and concise overview about Quantum computing..pptx
mughal641
 
PDF
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
PDF
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
PPTX
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
PDF
Per Axbom: The spectacular lies of maps
Nexer Digital
 
PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PPTX
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PPTX
Agentic AI in Healthcare Driving the Next Wave of Digital Transformation
danielle hunter
 
PDF
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PPTX
Farrell_Programming Logic and Design slides_10e_ch02_PowerPoint.pptx
bashnahara11
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PPTX
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
PDF
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
PDF
Build with AI and GDG Cloud Bydgoszcz- ADK .pdf
jaroslawgajewski1
 
PPTX
AI Code Generation Risks (Ramkumar Dilli, CIO, Myridius)
Priyanka Aash
 
PDF
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
Simple and concise overview about Quantum computing..pptx
mughal641
 
GDG Cloud Munich - Intro - Luiz Carneiro - #BuildWithAI - July - Abdel.pdf
Luiz Carneiro
 
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
Per Axbom: The spectacular lies of maps
Nexer Digital
 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
Agentic AI in Healthcare Driving the Next Wave of Digital Transformation
danielle hunter
 
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
Farrell_Programming Logic and Design slides_10e_ch02_PowerPoint.pptx
bashnahara11
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
Build with AI and GDG Cloud Bydgoszcz- ADK .pdf
jaroslawgajewski1
 
AI Code Generation Risks (Ramkumar Dilli, CIO, Myridius)
Priyanka Aash
 
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 

HTML5 Canvas - Let's Draw!

  • 5. The canvas is a 2d bitmap.
  • 6. No layers. No undo. No objects. No moving stuff.
  • 9. How can I create a c anvas?
  • 10. HTML <canvas id="a">fallback txt</canvas> JavaScript var canvas = document.getElementsById('a'); var ctx = canvas.getContext('2d');
  • 12. con text ctx.fillStyle = 'blue'; he ight ctx.fillRect(0, 0, 150, 50); x y width ctx.strokeStyle = '#c66'; ctx.lineWidth = 1; ctx.strokeRect(0.5, 60.5, 150, 50);
  • 13. Iwant to d raw an image!
  • 14. create image var img = new Image(); img.onload = function() { w hen loaded ctx.drawImage(img, 0, 0); }; x y img.src = 'nakedrobot.png'; image url
  • 15. how can you draw text?
  • 16. css p roperties ctx.font = 'bold 40px sans-serif'; ctx.fillText('hello kitty', 5, 40); x y hello kitty
  • 18. var w = canvas.width; var h = canvas.height; new image array var img = ctx.createImageData(w, h); for (var x = 0; x < w; x++) { for (var y = 0; y < h; y++) { // each pixel has four values var idx = (x + y * w) * 4; // 0 red, 1 green, 2 blue, 3 alpha img.data[idx + 0] = x; img.data[idx + 1] = 255-x; img.data[idx + 2] = 0; img.data[idx + 3] = 255; } } result ctx.putImageData(img, 0, 0); write to canv as
  • 21. A nimation function ti ck() { doKeyboard( ); updatePlaye r(); doAI(); draw(); audio(); } var fps = 3 0; setInterval (tick, 1000 /fps) ;
  • 22. get input draw update every 30ms
  • 23. function tick() { doKeyboard(); updatePlayer(); doAI(); draw(); audio(); } var fps = 30; setInterval(tick, 1000/fps);
  • 24. How can i Save an Image?
  • 25. var img = document. getElementsByTagName('img')[0]; var canvasImg = canvas.toDataURL("image/png"); returns “data:ima ge/png;base64, img.src = canvasImg; iVBORw0KGgoAAAA NS MgAAADICAYAAACt...” UhEU canvas image
  • 29. Summary Canvas <canvas> is a new 2d bitmap html element. you can draw primitives, images and pixels. There are no layers or object. If a pixel gets a new color, the old color is overwritten and lost. A canvas is not “sandboxed” like flash and can be accessed and drawn on with javascript.
  • 30. Material Used Banksy “Have a Break” http://www.flickr.com/photos/loungerie/4109421950/ Banksy “Flowing off” http://www.flickr.com/photos/loungerie/4109420324/ Banksy “They’re Coming” http://www.flickr.com/photos/97041449@N00/4115205218/ Bansky “Tourqay Robot” http://en.wikipedia.org/wiki/File:Banksy_Torquay_robot_crop.jpg Banksy “You have got to be kidding me” http://www.banksy.co.uk/ Banksy “follow your Dream” http://www.flickr.com/photos/thomashawk/6343586111/ Banksy “nola” http://www.flickr.com/photos/eddiedangerous/3045255243/ Banksy “Flower Pollard Street” http://www.flickr.com/photos/eddiedangerous/1800573742/ Banksy “what are you looking at?” http://www.flickr.com/photos/nolifebeforecoffee/124659356/ Banksy “Man and Picture of a dog” http://www.flickr.com/photos/atomicshed/141462789/ Banksy “Anti-Capitalism for sale” http://www.flickr.com/photos/jcodysimms/246024687/
  • 31. Material Used 3d teapot model http://resumbrae.com/ub/dms423_f08/10/ Metal Teapot http://www.flickr.com/photos/11273631@N08/2867342497/ furry teapot http://www.flickr.com/photos/11273631@N08/2867342461/ Television Icon http://thenounproject.com/noun/television/#icon-No416 Graphics Card http://www.flickr.com/photos/43779660@N00/218093887/ Banksy “under the Carpet” http://www.flickr.com/photos/acb/147223287/ Boxing http://www.flickr.com/photos/51035655711@N01/2826228569/