SlideShare a Scribd company logo
10 things I’ve learned
when working with
HTML5 canvas
by François CREVOLA
Sept. 2016
Goals of this presentation
- Share experience acquired
- Give tips you may not already know
- Reafirming important facts to not forget about
canvas
1, 2, 3, 4, 5…
1. Vector graphics are powerfull (even on a
bitmap canvas)
2. IE has the poorest canvas performance
3. Canvas is a transparent plane !
4. Optimize / minimize area to repaint
5. Browsers use GPU
… 6, 7, 8, 9, 10.
6. How to add a “Save image as …”
7. How to draw a colored polygon with
transparent hole.
8. Do not reinvent wheel, use the right library
9. Javascript + canvas is more portable than
java applet
10. Decimal coordinates give nicer results
1. Vector graphics are powerfull
- Vector graphics are easy to resize (to any
level), rotate (any angle) and without any
loss of quality
- Vector graphics are low-sized
- Can be generated by algorithm
2. IE has the poorest canvas performance
- Benchmarks show that for the same
drawning task, IE is slower than other
browser.
- If you game / app / demo is running
smoothly in IE, you can be sure that it will
run nicely with Safari, Firefox and Chrome.
3. Canvas is a transparent plane !
- Don’t erase your canvas by drawning a big
colored rectangle
- Canvas can be on top of other elements of
your Html page (for exemple you can
animate needles on top of a watch
background)
- Canvas can even be on top of other canvas
(can be a way to optimize by separating
4. Optimize / minimize area to repaint
- don’t redraw all canvas just to ‘move’ one
(little) element
- use clearRect() on the minimum possible
area (instead of the whole canvas), then
redraw only that part.
- exemple : move a sprite by erasing rect of
old sprite position, then redraw at new
position (handle sprite background in
5. Browsers use GPU
- Optimize so that work is done by your
browser (instead of manually doing it by your
own javascript code), which in turn will use
GPU for better performance.
- Use canvas built-in function for zoom,
clipping, transform, rotate, ...
6. How to add a “Save image as …”
// save canvas image as data url (png format by default)
and set this data avec src of a classic img html element
var data = canvas.toDataURL();
document.getElementById('pict').src = data;
NOTE : Today, major browsers have a context
menu “Save image as …” on canvas.
7. How to draw a colored polygon with
transparent hole.
- Draw the outer border clock-wise (CW)
- and draw the inner border
counter-clock-wise (CCW)
8. Do not reinvent wheel, use the right library
- many javascript library exists : choose the
one that do the job (example: clipper.js
helped me working on polygons)
- many framework designed specifically for
canvas
9. Javascript + canvas is more portable than
java applet
- Java applet are not compatible with Android
browser or IOS browser
- Java applet are considered obsolete
- Javascript and canvas don’t need any
browser plugin
10. Decimal coordinates give nicer results
- unless you use imageSmoothingEnabled =
false you don’t need to use integer
coordinates
- browsers will nicely smooth pixel for the
given coordinates : it’s perfectly ok to draw a
lineTo a floating point coordinate !

More Related Content

PDF
The Power of WebGL - Hackeando sua GPU com JavaScript
PPTX
Wp7 performance challenges
PPTX
Wide Screen Presentation16by901024x576 instructions
PDF
Пиксельные шейдеры для Web-разработчиков. Программируем GPU / Денис Радин (Li...
PDF
Advanced Web Graphics with Canvas
PPTX
Android performance
PPTX
Looking glassrenderinglt en
PDF
Thinking cpu & memory - DroidCon Paris 18 june 2013
The Power of WebGL - Hackeando sua GPU com JavaScript
Wp7 performance challenges
Wide Screen Presentation16by901024x576 instructions
Пиксельные шейдеры для Web-разработчиков. Программируем GPU / Денис Радин (Li...
Advanced Web Graphics with Canvas
Android performance
Looking glassrenderinglt en
Thinking cpu & memory - DroidCon Paris 18 june 2013

Similar to 10 things I've learned when working with html5 canvas (20)

PDF
HTML5 Canvas - The Future of Graphics on the Web
PPTX
HTML 5_Canvas
PDF
Intro to HTML5 Canvas
PDF
How to build a html5 websites.v1
KEY
Stupid Canvas Tricks
KEY
5 tips for your HTML5 games
PDF
Google's HTML5 Work: what's next?
PPTX
introduction of HTML canvas and styles .pptx
ODP
Working With Canvas
PDF
Plunge into HTML5 Canvas – Let’s begin
PPT
HTML5 Canvas
PDF
JavaOne 2009 - 2d Vector Graphics in the browser with Canvas and SVG
PDF
Mapping the world with Twitter
PPTX
Intro to Canva
PPTX
Introduction to HTML5 Canvas
PPTX
HTML5 Canvas - Basics.pptx
PPTX
Introduction to Canvas - Toronto HTML5 User Group
PDF
Introduction to Canvas - Toronto HTML5 User Group
PDF
HTML5 Canvas - Let's Draw!
PDF
Html5 canvas
HTML5 Canvas - The Future of Graphics on the Web
HTML 5_Canvas
Intro to HTML5 Canvas
How to build a html5 websites.v1
Stupid Canvas Tricks
5 tips for your HTML5 games
Google's HTML5 Work: what's next?
introduction of HTML canvas and styles .pptx
Working With Canvas
Plunge into HTML5 Canvas – Let’s begin
HTML5 Canvas
JavaOne 2009 - 2d Vector Graphics in the browser with Canvas and SVG
Mapping the world with Twitter
Intro to Canva
Introduction to HTML5 Canvas
HTML5 Canvas - Basics.pptx
Introduction to Canvas - Toronto HTML5 User Group
Introduction to Canvas - Toronto HTML5 User Group
HTML5 Canvas - Let's Draw!
Html5 canvas
Ad

Recently uploaded (20)

PDF
Approach and Philosophy of On baking technology
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
A Presentation on Artificial Intelligence
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Heart disease approach using modified random forest and particle swarm optimi...
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Hindi spoken digit analysis for native and non-native speakers
PPTX
A Presentation on Touch Screen Technology
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PPTX
Chapter 5: Probability Theory and Statistics
PPTX
SOPHOS-XG Firewall Administrator PPT.pptx
PPTX
1. Introduction to Computer Programming.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Hybrid model detection and classification of lung cancer
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
Approach and Philosophy of On baking technology
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
A Presentation on Artificial Intelligence
Digital-Transformation-Roadmap-for-Companies.pptx
Encapsulation_ Review paper, used for researhc scholars
Heart disease approach using modified random forest and particle swarm optimi...
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
Assigned Numbers - 2025 - Bluetooth® Document
Programs and apps: productivity, graphics, security and other tools
Hindi spoken digit analysis for native and non-native speakers
A Presentation on Touch Screen Technology
Univ-Connecticut-ChatGPT-Presentaion.pdf
Chapter 5: Probability Theory and Statistics
SOPHOS-XG Firewall Administrator PPT.pptx
1. Introduction to Computer Programming.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Hybrid model detection and classification of lung cancer
NewMind AI Weekly Chronicles - August'25-Week II
Accuracy of neural networks in brain wave diagnosis of schizophrenia
Ad

10 things I've learned when working with html5 canvas

  • 1. 10 things I’ve learned when working with HTML5 canvas by François CREVOLA Sept. 2016
  • 2. Goals of this presentation - Share experience acquired - Give tips you may not already know - Reafirming important facts to not forget about canvas
  • 3. 1, 2, 3, 4, 5… 1. Vector graphics are powerfull (even on a bitmap canvas) 2. IE has the poorest canvas performance 3. Canvas is a transparent plane ! 4. Optimize / minimize area to repaint 5. Browsers use GPU
  • 4. … 6, 7, 8, 9, 10. 6. How to add a “Save image as …” 7. How to draw a colored polygon with transparent hole. 8. Do not reinvent wheel, use the right library 9. Javascript + canvas is more portable than java applet 10. Decimal coordinates give nicer results
  • 5. 1. Vector graphics are powerfull - Vector graphics are easy to resize (to any level), rotate (any angle) and without any loss of quality - Vector graphics are low-sized - Can be generated by algorithm
  • 6. 2. IE has the poorest canvas performance - Benchmarks show that for the same drawning task, IE is slower than other browser. - If you game / app / demo is running smoothly in IE, you can be sure that it will run nicely with Safari, Firefox and Chrome.
  • 7. 3. Canvas is a transparent plane ! - Don’t erase your canvas by drawning a big colored rectangle - Canvas can be on top of other elements of your Html page (for exemple you can animate needles on top of a watch background) - Canvas can even be on top of other canvas (can be a way to optimize by separating
  • 8. 4. Optimize / minimize area to repaint - don’t redraw all canvas just to ‘move’ one (little) element - use clearRect() on the minimum possible area (instead of the whole canvas), then redraw only that part. - exemple : move a sprite by erasing rect of old sprite position, then redraw at new position (handle sprite background in
  • 9. 5. Browsers use GPU - Optimize so that work is done by your browser (instead of manually doing it by your own javascript code), which in turn will use GPU for better performance. - Use canvas built-in function for zoom, clipping, transform, rotate, ...
  • 10. 6. How to add a “Save image as …” // save canvas image as data url (png format by default) and set this data avec src of a classic img html element var data = canvas.toDataURL(); document.getElementById('pict').src = data; NOTE : Today, major browsers have a context menu “Save image as …” on canvas.
  • 11. 7. How to draw a colored polygon with transparent hole. - Draw the outer border clock-wise (CW) - and draw the inner border counter-clock-wise (CCW)
  • 12. 8. Do not reinvent wheel, use the right library - many javascript library exists : choose the one that do the job (example: clipper.js helped me working on polygons) - many framework designed specifically for canvas
  • 13. 9. Javascript + canvas is more portable than java applet - Java applet are not compatible with Android browser or IOS browser - Java applet are considered obsolete - Javascript and canvas don’t need any browser plugin
  • 14. 10. Decimal coordinates give nicer results - unless you use imageSmoothingEnabled = false you don’t need to use integer coordinates - browsers will nicely smooth pixel for the given coordinates : it’s perfectly ok to draw a lineTo a floating point coordinate !