SlideShare a Scribd company logo
Exploring Canvas


    Kevin Hoyt
    khoyt@adobe.com
    Twitter, AIM,YIM: parkerkrhoyt
    http://blog.kevinhoyt.com
Agenda
  • Getting started
  • Charting
  • Drawing
  • Interaction
  • Painting
  • Animation
Part #1:
Getting Started
Using the Canvas Tag
<canvas id="chart" width="150" height="150">
    Current stock price: $3.15 +0.15
</canvas>

<canvas id="clock" width="150" height="150">
    <img
         src="imagesclock.png"
         width="150"
         height="150"/>
</canvas>
Rendering Context
var canvas = document.getElementById( 'canvas' );
var context = null;

if( canvas.getContext )
{
  context = canvas.getContext( '2d' );
  // Drawing code here
} else {
  // Unsupported code here
}
Coordinate Space
      0
  0                          X

              y
          x




                          height
                  width
  Y
Drawing Paths
context.beginPath();

context.arc( 75, 75, 50, 0, Math.PI * 2, true );
context.moveTo( 110, 75 );
context.arc( 75, 75, 35, 0, Math.PI, false );
context.moveTo( 65, 65 );
context.arc( 60, 65, 5, 0, Math.PI * 2, true );
context.moveTo( 95, 65 );
context.arc( 90, 65, 5, 0, Math.PI * 2, true );

// context.closePath();
context.stroke();
// context.fill();
Understanding Lines
(0, 0)                    (0, 0)
                 (3, 1)                (3.5, 1)




                 (3, 5)                  (3.5, 5)
          1.0 width                1.0 width
Caps, Joints and Miters



   butt   round    width / 2
  round   bevel      x limit
 square   miter   from joint
Styles and Colors
         fillStyle
        strokeStyle




            orange
           #FFA500
     rgb( 255, 165, 0 )
   rgba( 255, 165, 0, 1 )
Gradients

// Start point and stop point
// Controls angle/direction of gradient
var fill = context.createLinearGradient( 0, 0, 0, 50 );

// 0 - 1 relative to gradient range
fill.addColorStop( 0, ‘#FFFFFF’ );
fill.addColorStop( 0.5, ‘#FFA500’ );
fill.addColorStop( 1, ‘#FFFFFF’ );

// Apply the fill style
context.fillStyle = fill;
context.fillRect( 0, 0, 50, 50 );
Gradients
// Inner circle point and radius
// Outer circle point and radius
// Not necessarily a single point
var fill = context.createRadialGradient(
    95, 15, 15,
    102, 20, 40
);

// 0 - 1 relative to gradient range
fill.addColorStop( 0, ‘#FFFFFF’ );
fill.addColorStop( 0.5, ‘#FFA500’ );
fill.addColorStop( 1, ‘#FFFFFF’ );

// Apply the fill style
context.fillStyle = fill;
context.fillRect( 0, 0, 50, 50 );
Text and Shadows
var canvas = document.getElementById( 'canvas' );
var ctx = null;

if( canvas.getContext )
{
    ctx = canvas.getContext( '2d' );

    ctx.shadowOffsetX = 2;
    ctx.shadowOffsetY = 2;
    ctx.shadowBlur = 2;
    ctx.shadowColor = 'rgba( 0, 0, 0, 0.5 )';

    ctx.font = '20px Times New Roman';
    ctx.fillStyle = 'black';
    ctx.fillText( 'Sample String', 5, 30 );
}
Shapes
Part #2:
Charting
PlotKit
jqPlot
RGraph
Part #3:
Drawing
Follow the Mouse
Saving
Saving with Flair
Looking Ahead
Part #4:
Interaction
Tracking Points




    What’s clickable?
   How do you know?
   Canvas vs DOM...
Curves




Quadratic Curve            Cubic Curve
Part #5:
Painting
Load From Server
Load From Server
var canvas = document.getElementById( 'canvas' );
var ctx = canvas.getContext( '2d' );
var img = null;

$( '#make' ).click( function( evt ) {
     img = new Image();
     img.onload = function() {
         ctx.drawImage( img, 0, 0 );
    };
     img.src = 'images/backdrop.png';
} );
Load From Server
Other Canvas
Other Canvas
var canvas = document.getElementById( 'lines' );
var ctx = canvas.getContext( '2d' );

ctx.beginPath();
ctx.moveTo( 30, 96 );
ctx.lineTo( 70, 66 );
ctx.lineTo( 103, 76 );
ctx.lineTo( 170, 15 );
ctx.stroke();

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

$( '#lines' ).click( function( evt ) {
     ctx.drawImage( this, 0, 0 );
} );
Other Canvas
Embedded Data
Embedded Data

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

$( '#embed' ).click( function( evt ) {
     img = new Image();
     img.src = 'data:image/png;base64,iVB...';
     ctx.drawImage( img, 0, 0 );
} );
Pixel Pushing

        context.drawImage(
            myImg,
            frame.x,
            frame.y,
            frame.width,
            frame.height );
Pixel Slicing
        context.drawImage(
            myImg,
            frame.x,
            frame.y,
            frame.width,
            frame.height,
            22,
            21,
            frame.width,
            frame.height );
Pixel Dissecting
Part #6:
Animation
Update on Interval
Tweening
JSTweener.addTween( position, {
    time: 3,
    transition: 'easeOutExpo',
    x: end.x,
    y: end.y,
    onUpdate: function() {
        context.clearRect( 0, 0, 640, 480 );
        // Draw updates to sprites
     },
    onComplete: function() {
        position = null;
        start = null;
        end = null;
     }
} );
Line Interpolation
Physics




 Box2D JS
Questions?

  Kevin Hoyt
  khoyt@adobe.com
  Twitter, AIM,YIM: parkerkrhoyt
  http://blog.kevinhoyt.com

More Related Content

What's hot (20)

PDF
UIWebViewでつくるUI
cocopon
 
KEY
Proga 090525
Atsushi Tadokoro
 
PPT
HTML5 Canvas
Robyn Overstreet
 
PPTX
Introduction to HTML5 Canvas
Mindy McAdams
 
PDF
Exploring fractals in CSS, @fronttrends, Warsaw, 2015
pixelass
 
PPT
Mobile Game and Application with J2ME - Collision Detection
Jenchoke Tachagomain
 
PPT
Mobile Game and Application with J2ME
Jenchoke Tachagomain
 
KEY
cocos2d 事例編 HungryMasterの実装から
Yuichi Higuchi
 
KEY
Proga 0629
Atsushi Tadokoro
 
PPTX
How to make a video game
dandylion13
 
PPTX
HTML 5 Canvas & SVG
Ofir's Fridman
 
PDF
Html5 game programming overview
민태 김
 
PDF
Swift, via "swift-2048"
Austin Zheng
 
PPTX
My favorite slides
Mitchell Wand
 
KEY
Proga 0518
Atsushi Tadokoro
 
KEY
Proga 0706
Atsushi Tadokoro
 
PPTX
HTML 5_Canvas
Vishakha Vaidya
 
PDF
Canvas - HTML 5
Jaeni Sahuri
 
PPTX
HTML CANVAS
Light Salt
 
PPTX
CSS Transitions, Transforms, Animations
Rob LaPlaca
 
UIWebViewでつくるUI
cocopon
 
Proga 090525
Atsushi Tadokoro
 
HTML5 Canvas
Robyn Overstreet
 
Introduction to HTML5 Canvas
Mindy McAdams
 
Exploring fractals in CSS, @fronttrends, Warsaw, 2015
pixelass
 
Mobile Game and Application with J2ME - Collision Detection
Jenchoke Tachagomain
 
Mobile Game and Application with J2ME
Jenchoke Tachagomain
 
cocos2d 事例編 HungryMasterの実装から
Yuichi Higuchi
 
Proga 0629
Atsushi Tadokoro
 
How to make a video game
dandylion13
 
HTML 5 Canvas & SVG
Ofir's Fridman
 
Html5 game programming overview
민태 김
 
Swift, via "swift-2048"
Austin Zheng
 
My favorite slides
Mitchell Wand
 
Proga 0518
Atsushi Tadokoro
 
Proga 0706
Atsushi Tadokoro
 
HTML 5_Canvas
Vishakha Vaidya
 
Canvas - HTML 5
Jaeni Sahuri
 
HTML CANVAS
Light Salt
 
CSS Transitions, Transforms, Animations
Rob LaPlaca
 

Similar to Exploring Canvas (20)

PDF
HTML5 Canvas - The Future of Graphics on the Web
Robin Hawkes
 
PPTX
Advanced html5 diving into the canvas tag
David Voyles
 
PDF
How to build a html5 websites.v1
Bitla Software
 
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
 
PDF
Is HTML5 Ready? (workshop)
Remy Sharp
 
PDF
Is html5-ready-workshop-110727181512-phpapp02
PL dream
 
KEY
5 tips for your HTML5 games
Ernesto Jiménez
 
PPT
Rotoscope inthebrowserppt billy
nimbleltd
 
PPT
Canvas
Ganesh Gembali
 
PPT
Canvas
Ganesh Gembali
 
ODP
Working With Canvas
Diogo Antunes
 
PDF
HTML5 Canvas
Ivano Malavolta
 
PDF
Writing a Space Shooter with HTML5 Canvas
Steve Purkis
 
PDF
Mapping the world with Twitter
carlo zapponi
 
PPTX
Javascript Canvas API
Samuel Santos
 
KEY
Stupid Canvas Tricks
deanhudson
 
KEY
The Canvas API for Rubyists
deanhudson
 
HTML5 Canvas - The Future of Graphics on the Web
Robin Hawkes
 
Advanced html5 diving into the canvas tag
David Voyles
 
How to build a html5 websites.v1
Bitla Software
 
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
 
Is HTML5 Ready? (workshop)
Remy Sharp
 
Is html5-ready-workshop-110727181512-phpapp02
PL dream
 
5 tips for your HTML5 games
Ernesto Jiménez
 
Rotoscope inthebrowserppt billy
nimbleltd
 
Working With Canvas
Diogo Antunes
 
HTML5 Canvas
Ivano Malavolta
 
Writing a Space Shooter with HTML5 Canvas
Steve Purkis
 
Mapping the world with Twitter
carlo zapponi
 
Javascript Canvas API
Samuel Santos
 
Stupid Canvas Tricks
deanhudson
 
The Canvas API for Rubyists
deanhudson
 
Ad

Recently uploaded (20)

PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PDF
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
Python basic programing language for automation
DanialHabibi2
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
Python basic programing language for automation
DanialHabibi2
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
Ad

Exploring Canvas

  • 1. Exploring Canvas Kevin Hoyt [email protected] Twitter, AIM,YIM: parkerkrhoyt http://blog.kevinhoyt.com
  • 2. Agenda • Getting started • Charting • Drawing • Interaction • Painting • Animation
  • 4. Using the Canvas Tag <canvas id="chart" width="150" height="150"> Current stock price: $3.15 +0.15 </canvas> <canvas id="clock" width="150" height="150"> <img src="imagesclock.png" width="150" height="150"/> </canvas>
  • 5. Rendering Context var canvas = document.getElementById( 'canvas' ); var context = null; if( canvas.getContext ) { context = canvas.getContext( '2d' ); // Drawing code here } else { // Unsupported code here }
  • 6. Coordinate Space 0 0 X y x height width Y
  • 7. Drawing Paths context.beginPath(); context.arc( 75, 75, 50, 0, Math.PI * 2, true ); context.moveTo( 110, 75 ); context.arc( 75, 75, 35, 0, Math.PI, false ); context.moveTo( 65, 65 ); context.arc( 60, 65, 5, 0, Math.PI * 2, true ); context.moveTo( 95, 65 ); context.arc( 90, 65, 5, 0, Math.PI * 2, true ); // context.closePath(); context.stroke(); // context.fill();
  • 8. Understanding Lines (0, 0) (0, 0) (3, 1) (3.5, 1) (3, 5) (3.5, 5) 1.0 width 1.0 width
  • 9. Caps, Joints and Miters butt round width / 2 round bevel x limit square miter from joint
  • 10. Styles and Colors fillStyle strokeStyle orange #FFA500 rgb( 255, 165, 0 ) rgba( 255, 165, 0, 1 )
  • 11. Gradients // Start point and stop point // Controls angle/direction of gradient var fill = context.createLinearGradient( 0, 0, 0, 50 ); // 0 - 1 relative to gradient range fill.addColorStop( 0, ‘#FFFFFF’ ); fill.addColorStop( 0.5, ‘#FFA500’ ); fill.addColorStop( 1, ‘#FFFFFF’ ); // Apply the fill style context.fillStyle = fill; context.fillRect( 0, 0, 50, 50 );
  • 12. Gradients // Inner circle point and radius // Outer circle point and radius // Not necessarily a single point var fill = context.createRadialGradient( 95, 15, 15, 102, 20, 40 ); // 0 - 1 relative to gradient range fill.addColorStop( 0, ‘#FFFFFF’ ); fill.addColorStop( 0.5, ‘#FFA500’ ); fill.addColorStop( 1, ‘#FFFFFF’ ); // Apply the fill style context.fillStyle = fill; context.fillRect( 0, 0, 50, 50 );
  • 13. Text and Shadows var canvas = document.getElementById( 'canvas' ); var ctx = null; if( canvas.getContext ) { ctx = canvas.getContext( '2d' ); ctx.shadowOffsetX = 2; ctx.shadowOffsetY = 2; ctx.shadowBlur = 2; ctx.shadowColor = 'rgba( 0, 0, 0, 0.5 )'; ctx.font = '20px Times New Roman'; ctx.fillStyle = 'black'; ctx.fillText( 'Sample String', 5, 30 ); }
  • 25. Tracking Points What’s clickable? How do you know? Canvas vs DOM...
  • 26. Curves Quadratic Curve Cubic Curve
  • 29. Load From Server var canvas = document.getElementById( 'canvas' ); var ctx = canvas.getContext( '2d' ); var img = null; $( '#make' ).click( function( evt ) { img = new Image(); img.onload = function() { ctx.drawImage( img, 0, 0 ); }; img.src = 'images/backdrop.png'; } );
  • 32. Other Canvas var canvas = document.getElementById( 'lines' ); var ctx = canvas.getContext( '2d' ); ctx.beginPath(); ctx.moveTo( 30, 96 ); ctx.lineTo( 70, 66 ); ctx.lineTo( 103, 76 ); ctx.lineTo( 170, 15 ); ctx.stroke(); canvas = document.getElementById( 'canvas' ); ctx = canvas.getContext( '2d' ); $( '#lines' ).click( function( evt ) { ctx.drawImage( this, 0, 0 ); } );
  • 35. Embedded Data var canvas = document.getElementById( 'canvas' ); var ctx = canvas.getContext( '2d' ); var img = null; $( '#embed' ).click( function( evt ) { img = new Image(); img.src = 'data:image/png;base64,iVB...'; ctx.drawImage( img, 0, 0 ); } );
  • 36. Pixel Pushing context.drawImage( myImg, frame.x, frame.y, frame.width, frame.height );
  • 37. Pixel Slicing context.drawImage( myImg, frame.x, frame.y, frame.width, frame.height, 22, 21, frame.width, frame.height );
  • 41. Tweening JSTweener.addTween( position, { time: 3, transition: 'easeOutExpo', x: end.x, y: end.y, onUpdate: function() { context.clearRect( 0, 0, 640, 480 ); // Draw updates to sprites }, onComplete: function() { position = null; start = null; end = null; } } );
  • 44. Questions? Kevin Hoyt [email protected] Twitter, AIM,YIM: parkerkrhoyt http://blog.kevinhoyt.com

Editor's Notes