SlideShare una empresa de Scribd logo
1 de 20
Canvas: we must go deeper

                Szymon Piłkowski
   (freelancer, previously crytek & bigpoint)

            http://twitter.com/ard
Canvas?!




The canvas element provides scripts with a resolution-
dependent bitmap canvas, which can be used for
rendering graphs, game graphics, or other visual images
on the fly.
Canvas is a bitmap

•   one-dimensional array

•   [R,G,B,A,// pixel 0,0
     R,G,B,A,// pixel 1,0
     R,G,B,A,// pixel 2,0 (...)
    ]

•   point 0,0 placed in top left corner
Canvas basics
•   Immediate access (almost)

•   Low-level API




•   No scene graph, no direct access to drawn elements

•   Basic animation = redrawing everything each frame
Drawing on canvas




    (example from MDN)
Simple Game Loop
Pre-rendering / Buffering

•   Buffer is a copy of bitmap state (single frame) which is
    kept in memory

•   Buffers are known in Computer Graphics for 40 years

•   Using buffers you’re able to save and restore drawn
    states

•   ... and you can use them in JavaScript, too
Buffers in JS
• Wrong way:


• Slow, slow, slow
• still popular
Buffers in JS
• Good way
    context.drawImage: “can take either an HTMLImageElement,
    an HTMLCanvasElement, or an HTMLVideoElement for the image
    argument.”




    (another way: use toDataURL method)

•   As fast as drawing normal images
Buffers: performance
    optimisation
Buffers in games




(image from isocity by Rob Evans)
Render cycle
   Background layer (rendered once, never
   cleared)



    Static layer (rendered anew when
    invalidated)


    Dynamic layer (rendered anew each
    frame)
Render cycle (code)



 (needs to be tweaked per game)
Pre-processing sprites
• Saving disk space
• Saving designers time
• Improving the pipeline


 (from onslaught! arena case study on html5rocks.com)
Pre-processing sprites
Collision detection

• Problem: canvas is just a bitmap.
• You can’t attach events to drawn objects
• Detecting collision between a point (mouse
  pointer) and any rendered shape (image or
  primitive) is not straight-forward
Hitmap / Collision map

  1. map objects to unique colors
Hitmap / Collision map
  2. store in memory a copy of each
  object drawn in single color (using
        composite operations)
Hitmap / Collision map
3. when checking collisions, do a single render
          of candidates to a buffer



4. grab color from point and compare with
                 colorMap
end()

•   questions?

•   http://twitter.com/ard

•   szymon.pilkowski@gmail.com

•   Thanks!

Más contenido relacionado

La actualidad más candente

Animation software by Er. Suvisha Gupta
Animation software by Er. Suvisha GuptaAnimation software by Er. Suvisha Gupta
Animation software by Er. Suvisha Gupta
Suvisha Gupta
 
How Facebook cut image load times in half
How Facebook cut image load times in halfHow Facebook cut image load times in half
How Facebook cut image load times in half
Tyrone Nicholas
 
Smedberg niklas bringing_aaa_graphics
Smedberg niklas bringing_aaa_graphicsSmedberg niklas bringing_aaa_graphics
Smedberg niklas bringing_aaa_graphics
changehee lee
 

La actualidad más candente (20)

Overview of graphics systems
Overview of  graphics systemsOverview of  graphics systems
Overview of graphics systems
 
Deepak
DeepakDeepak
Deepak
 
Animation software by Er. Suvisha Gupta
Animation software by Er. Suvisha GuptaAnimation software by Er. Suvisha Gupta
Animation software by Er. Suvisha Gupta
 
Computer animation Computer Graphics
Computer animation Computer Graphics Computer animation Computer Graphics
Computer animation Computer Graphics
 
ANIMATION SEQUENCE
ANIMATION SEQUENCEANIMATION SEQUENCE
ANIMATION SEQUENCE
 
【Unite 2017 Tokyo】スクリプタブル・レンダーパイプラインのカスタマイズと拡張
【Unite 2017 Tokyo】スクリプタブル・レンダーパイプラインのカスタマイズと拡張【Unite 2017 Tokyo】スクリプタブル・レンダーパイプラインのカスタマイズと拡張
【Unite 2017 Tokyo】スクリプタブル・レンダーパイプラインのカスタマイズと拡張
 
TMD2063 | Digital Animation - Chapter 2
TMD2063 | Digital Animation - Chapter 2TMD2063 | Digital Animation - Chapter 2
TMD2063 | Digital Animation - Chapter 2
 
Computer Graphics
Computer GraphicsComputer Graphics
Computer Graphics
 
Animation
AnimationAnimation
Animation
 
How Facebook cut image load times in half
How Facebook cut image load times in halfHow Facebook cut image load times in half
How Facebook cut image load times in half
 
Computer Graphics
Computer GraphicsComputer Graphics
Computer Graphics
 
Facebook tricks for image handling in Android
Facebook tricks for image handling in AndroidFacebook tricks for image handling in Android
Facebook tricks for image handling in Android
 
Ch06
Ch06Ch06
Ch06
 
Animation
AnimationAnimation
Animation
 
Computer_Graphics_basic_definitions_summary
Computer_Graphics_basic_definitions_summaryComputer_Graphics_basic_definitions_summary
Computer_Graphics_basic_definitions_summary
 
Animations
AnimationsAnimations
Animations
 
Armand Rousso describes about the Computer graphic
Armand Rousso describes about the Computer graphicArmand Rousso describes about the Computer graphic
Armand Rousso describes about the Computer graphic
 
Animation techniques for CG students
Animation techniques for CG studentsAnimation techniques for CG students
Animation techniques for CG students
 
VFX
VFXVFX
VFX
 
Smedberg niklas bringing_aaa_graphics
Smedberg niklas bringing_aaa_graphicsSmedberg niklas bringing_aaa_graphics
Smedberg niklas bringing_aaa_graphics
 

Destacado

Malta Ltd Company Formation - A Walk Through - 2014 - Acumum Legal & Advisory
Malta Ltd Company Formation - A Walk Through - 2014 - Acumum Legal & AdvisoryMalta Ltd Company Formation - A Walk Through - 2014 - Acumum Legal & Advisory
Malta Ltd Company Formation - A Walk Through - 2014 - Acumum Legal & Advisory
Acumum - Legal & Advisory
 
fanscala1 1 インストールとreplまで
fanscala1 1 インストールとreplまでfanscala1 1 インストールとreplまで
fanscala1 1 インストールとreplまで
Toshiki Shinozaki
 

Destacado (18)

Inleiding social media
Inleiding social media Inleiding social media
Inleiding social media
 
fanscala1 3 sbt
fanscala1 3 sbtfanscala1 3 sbt
fanscala1 3 sbt
 
篠崎Lt20160909
篠崎Lt20160909篠崎Lt20160909
篠崎Lt20160909
 
Protest 簡介
Protest 簡介Protest 簡介
Protest 簡介
 
Malta Ltd Company Formation - A Walk Through - 2014 - Acumum Legal & Advisory
Malta Ltd Company Formation - A Walk Through - 2014 - Acumum Legal & AdvisoryMalta Ltd Company Formation - A Walk Through - 2014 - Acumum Legal & Advisory
Malta Ltd Company Formation - A Walk Through - 2014 - Acumum Legal & Advisory
 
fanscala1 2 scalaの基本
fanscala1 2 scalaの基本fanscala1 2 scalaの基本
fanscala1 2 scalaの基本
 
Malta’s Remittance System
Malta’s Remittance SystemMalta’s Remittance System
Malta’s Remittance System
 
fanscala1 1 インストールとreplまで
fanscala1 1 インストールとreplまでfanscala1 1 インストールとreplまで
fanscala1 1 インストールとreplまで
 
fanscala1 4 その他資料
fanscala1 4 その他資料fanscala1 4 その他資料
fanscala1 4 その他資料
 
Summary Bisnis Plan Mikro Financing
Summary Bisnis Plan Mikro FinancingSummary Bisnis Plan Mikro Financing
Summary Bisnis Plan Mikro Financing
 
Haskell vs. F# vs. Scala
Haskell vs. F# vs. ScalaHaskell vs. F# vs. Scala
Haskell vs. F# vs. Scala
 
History of photography
History of photographyHistory of photography
History of photography
 
Maritime by Acumum
Maritime by AcumumMaritime by Acumum
Maritime by Acumum
 
Закон об Aвиации Регистрация & Сервис - Мальтийская Международная Правовая фи...
Закон об Aвиации Регистрация & Сервис - Мальтийская Международная Правовая фи...Закон об Aвиации Регистрация & Сервис - Мальтийская Международная Правовая фи...
Закон об Aвиации Регистрация & Сервис - Мальтийская Международная Правовая фи...
 
Malta Investor Funds - A Walk Through - By Acumum; Legal & Advisory
Malta Investor Funds - A Walk Through - By Acumum; Legal & AdvisoryMalta Investor Funds - A Walk Through - By Acumum; Legal & Advisory
Malta Investor Funds - A Walk Through - By Acumum; Legal & Advisory
 
篠崎Lt20141215
篠崎Lt20141215篠崎Lt20141215
篠崎Lt20141215
 
Malta - Tax Efficient Asset Structuring by Acumum
Malta - Tax Efficient Asset Structuring by AcumumMalta - Tax Efficient Asset Structuring by Acumum
Malta - Tax Efficient Asset Structuring by Acumum
 
Malta Gaming Memo - Acumum Legal & Advisory
Malta Gaming Memo - Acumum Legal & AdvisoryMalta Gaming Memo - Acumum Legal & Advisory
Malta Gaming Memo - Acumum Legal & Advisory
 

Similar a Canvas: we must go deeper

Graphicsand animations devoxx2010 (1)
Graphicsand animations devoxx2010 (1)Graphicsand animations devoxx2010 (1)
Graphicsand animations devoxx2010 (1)
Marakana Inc.
 
Making a game with Molehill: Zombie Tycoon
Making a game with Molehill: Zombie TycoonMaking a game with Molehill: Zombie Tycoon
Making a game with Molehill: Zombie Tycoon
Jean-Philippe Doiron
 
Mo devtablet johncromartie-graphicsperformance
Mo devtablet johncromartie-graphicsperformanceMo devtablet johncromartie-graphicsperformance
Mo devtablet johncromartie-graphicsperformance
johncromartie
 
Image processing tool box.pptx
Image processing tool box.pptxImage processing tool box.pptx
Image processing tool box.pptx
AvinashJain66
 
OpenLayers 3.0
OpenLayers 3.0OpenLayers 3.0
OpenLayers 3.0
Camptocamp
 

Similar a Canvas: we must go deeper (20)

Graphicsand animations devoxx2010 (1)
Graphicsand animations devoxx2010 (1)Graphicsand animations devoxx2010 (1)
Graphicsand animations devoxx2010 (1)
 
Benoit fouletier guillaume martin unity day- modern 2 d techniques-gce2014
Benoit fouletier guillaume martin   unity day- modern 2 d techniques-gce2014Benoit fouletier guillaume martin   unity day- modern 2 d techniques-gce2014
Benoit fouletier guillaume martin unity day- modern 2 d techniques-gce2014
 
Данило Ульянич “C89 OpenGL for ARM microcontrollers on Cortex-M. Basic functi...
Данило Ульянич “C89 OpenGL for ARM microcontrollers on Cortex-M. Basic functi...Данило Ульянич “C89 OpenGL for ARM microcontrollers on Cortex-M. Basic functi...
Данило Ульянич “C89 OpenGL for ARM microcontrollers on Cortex-M. Basic functi...
 
Overview of graphics systems.ppt
Overview of graphics systems.pptOverview of graphics systems.ppt
Overview of graphics systems.ppt
 
Making a game with Molehill: Zombie Tycoon
Making a game with Molehill: Zombie TycoonMaking a game with Molehill: Zombie Tycoon
Making a game with Molehill: Zombie Tycoon
 
Introduction to Computer graphics
Introduction to Computer graphicsIntroduction to Computer graphics
Introduction to Computer graphics
 
Unit 11. Graphics
Unit 11. GraphicsUnit 11. Graphics
Unit 11. Graphics
 
Mo devtablet johncromartie-graphicsperformance
Mo devtablet johncromartie-graphicsperformanceMo devtablet johncromartie-graphicsperformance
Mo devtablet johncromartie-graphicsperformance
 
Chapter 3 : IMAGE
Chapter 3 : IMAGEChapter 3 : IMAGE
Chapter 3 : IMAGE
 
Chapter 3
Chapter 3Chapter 3
Chapter 3
 
Game development terminologies
Game development terminologiesGame development terminologies
Game development terminologies
 
Prinsip gambar digital
Prinsip gambar digitalPrinsip gambar digital
Prinsip gambar digital
 
Cocos2d programming
Cocos2d programmingCocos2d programming
Cocos2d programming
 
GJU MM Unit 3.pdf
GJU MM Unit 3.pdfGJU MM Unit 3.pdf
GJU MM Unit 3.pdf
 
K2P workshop 3-23-13
K2P workshop 3-23-13K2P workshop 3-23-13
K2P workshop 3-23-13
 
Image processing tool box.pptx
Image processing tool box.pptxImage processing tool box.pptx
Image processing tool box.pptx
 
Stupid Canvas Tricks
Stupid Canvas TricksStupid Canvas Tricks
Stupid Canvas Tricks
 
OpenLayers 3.0
OpenLayers 3.0OpenLayers 3.0
OpenLayers 3.0
 
West Coast DevCon 2014: The Slate UI Framework (Part 2) - Game UI & Unreal Mo...
West Coast DevCon 2014: The Slate UI Framework (Part 2) - Game UI & Unreal Mo...West Coast DevCon 2014: The Slate UI Framework (Part 2) - Game UI & Unreal Mo...
West Coast DevCon 2014: The Slate UI Framework (Part 2) - Game UI & Unreal Mo...
 
TMD2063 | Digital Animation - Chapter 3
TMD2063 | Digital Animation - Chapter 3TMD2063 | Digital Animation - Chapter 3
TMD2063 | Digital Animation - Chapter 3
 

Último

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Último (20)

Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
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
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
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...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 

Canvas: we must go deeper

  • 1. Canvas: we must go deeper Szymon Piłkowski (freelancer, previously crytek & bigpoint) http://twitter.com/ard
  • 2. Canvas?! The canvas element provides scripts with a resolution- dependent bitmap canvas, which can be used for rendering graphs, game graphics, or other visual images on the fly.
  • 3. Canvas is a bitmap • one-dimensional array • [R,G,B,A,// pixel 0,0 R,G,B,A,// pixel 1,0 R,G,B,A,// pixel 2,0 (...) ] • point 0,0 placed in top left corner
  • 4. Canvas basics • Immediate access (almost) • Low-level API • No scene graph, no direct access to drawn elements • Basic animation = redrawing everything each frame
  • 5. Drawing on canvas (example from MDN)
  • 7. Pre-rendering / Buffering • Buffer is a copy of bitmap state (single frame) which is kept in memory • Buffers are known in Computer Graphics for 40 years • Using buffers you’re able to save and restore drawn states • ... and you can use them in JavaScript, too
  • 8. Buffers in JS • Wrong way: • Slow, slow, slow • still popular
  • 9. Buffers in JS • Good way context.drawImage: “can take either an HTMLImageElement, an HTMLCanvasElement, or an HTMLVideoElement for the image argument.” (another way: use toDataURL method) • As fast as drawing normal images
  • 10. Buffers: performance optimisation
  • 11. Buffers in games (image from isocity by Rob Evans)
  • 12. Render cycle Background layer (rendered once, never cleared) Static layer (rendered anew when invalidated) Dynamic layer (rendered anew each frame)
  • 13. Render cycle (code) (needs to be tweaked per game)
  • 14. Pre-processing sprites • Saving disk space • Saving designers time • Improving the pipeline (from onslaught! arena case study on html5rocks.com)
  • 16. Collision detection • Problem: canvas is just a bitmap. • You can’t attach events to drawn objects • Detecting collision between a point (mouse pointer) and any rendered shape (image or primitive) is not straight-forward
  • 17. Hitmap / Collision map 1. map objects to unique colors
  • 18. Hitmap / Collision map 2. store in memory a copy of each object drawn in single color (using composite operations)
  • 19. Hitmap / Collision map 3. when checking collisions, do a single render of candidates to a buffer 4. grab color from point and compare with colorMap
  • 20. end() • questions? • http://twitter.com/ard • szymon.pilkowski@gmail.com • Thanks!

Notas del editor

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n