SlideShare une entreprise Scribd logo
1  sur  61
Télécharger pour lire hors ligne
High Performance Mobile
Web Game Development
in HTML5

Sangmin, Shim
I work at NHN
See the demo first

http://goo.gl/JJbaQ
You can easily build games on desktops
Mobile’s different
Many OS’s




  4/5/6




 2.1 / 2.2 / 2.3 / 2.3.2 / 2.3.3 / 2.3.7 / 3.1 / 3.2 / 4.0.1 / 4.0.3 / 4.0.4 / 4.1
                                        ....
Various Devices
Slower
Limited Sound Support

• Cannot play multiple sounds simultaneously
• iOS requires user input to play sound
• Slow downs the performance


Background music



                   Effect
Two worst enemies in mobile game development




         Performance & Sound
Mobile is evolving rapidly
Web Audio API




Background music


                   Effect
Better performance in iOS6
FPS (Frame per Second)                             FPS

60
                                                   60

                           iOS6                                  iOS6



                   353%↑                                        68%↑
50
                                                   50


40
                                                   40


                                                         iOS5
30
                                                   30


20
                                                   20


10                iOS4                             10


 0
                                                    0

     100 objects were animated using Collie on iPhone4
Let’s start to build your own mobile game!
1. 2. 3. 4. 5. 6.
Both Canvas And CSS3 3D Transforms have to be
                  supported
Both Canvas And CSS3 3D Transforms have to be supported




         Need to use different rendering methods depending
         on the platform to get hardware acceleration


         Hardware Accelerated Canvas Animation in iOS5
Both Canvas And CSS3 3D Transforms have to be supported




         iOS4 did not have hardware acceleration support


         Performance comparison between iOS4 and iOS5
Both Canvas And CSS3 3D Transforms have to be supported




         iOS4 supports hardware acceleration when using
         CSS 3D Transforms


         Performance comparison between iOS4 and iOS5 with different rendering methods
Both Canvas And CSS3 3D Transforms have to be supported




            "O, iOS4 is such an old platform, maybe my game
                       does not need to support it"
Both Canvas And CSS3 3D Transforms have to be supported




         Renderings Used



                               iPhone                     iPhone   Android    Android
                              3GS or 4                      4S       2.x     3 or higher




                              CSS 3D
                         (less than iOS5)
                                                                   Canvas
Rendering                                                 Canvas     or       CSS 3D
                                                                    DOM
                              Canvas
                            (iOS5 over)
Both Canvas And CSS3 3D Transforms have to be supported




                  "Well, most mobile devices support
           CSS3 3D transforms, so I’ll just ditch Canvas totally
                          and go with DOM"
Both Canvas And CSS3 3D Transforms have to be supported




         Performance Comparison between Canvas and
         DOM in iOS5
          FPS

          60



          50



          40                                                        Canvas
          30


                                                               CSS3 3D Transforms
          20



          10



           0
                      10                  20              30   50       100     Objects
2. 3. 4. 5. 6.
Need to find out the exact extent of an object
Need to find out the exact extent of an object




          Must find out the exact extent of an object
Need to find out the exact extent of an object




          How to find the exact extent of an object using
          getImageData method in Canvas

             var el = document.createElement("canvas");
             el.width = 200;
             el.height = 200;
             var ctx = el.getContext("2d");
             ctx.fillStyle = "red";
             ctx.fillRect(10, 10, 100, 100);

             var imageData = ctx.getImageData(20, 20, 1, 1);

             if (
                           imageData.data[0]     !==   0 ||
                           imageData.data[1]     !==   0 ||
                           imageData.data[2]     !==   0 ||
                           imageData.data[3]     !==   0
             ) {
                           console.log("here it is!");
             }
Need to find out the exact extent of an object




          Security Issue with Canvas Element

          • Information leakage can occur if scripts from
            one origin can access information (e.g. read
            pixels) from images from another origin (one that
            isn't the same)
          • The toDataURL(), toBlob(), and getImageData()
            methods check the flag and will throw a
            SecurityError exception rather than leak cross-
            origin data
Need to find out the exact extent of an object




          Predefining the area



                                                 [[126, 285],[104, 286],[101, 267],[105,
                                                 221],[101, 213],[88, 188],[85, 168],[81,
                                                 153],[94, 133],[97, 103],[94, 60],[95,
                                                 29],[103, 32],[109, 97],[143, 93],[147,
                                                 33],[151, 31],[152, 97],[161, 137],[166,
                                                 167],[165, 188],[169, 200],[165,
                                                 208],[160, 209],[159, 235],[159,
                                                 256],[159, 269],[162, 275],[162,
                                                 286],[138, 288],[130, 275],[132, 254]]
Need to find out the exact extent of an object




          Performance comparison between
          getImageData and PNPOLY
         Operations
          70000


          60000


          50000


          40000
                                                          9,930%↑
                                                                                         operations/sec
          30000


          20000


          10000


              0

                             getImageData()                           PNPOLY Algorithm


         http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
Need to find out the exact extent of an object




          Graphic Designer



        I'm a Graphic Designer
3. 4. 5. 6.
Use a Bitmap Image Instead of Drawing
Use a Bitmap Image Instead of Drawing




         Drawing Vectors is Slow


          Performance comparison between vector drawing and using images
Use a Bitmap Image Instead of Drawing




         Object Caching

                                        Non Object Caching
                                             Draw a vector




                             Use Object Caching


                                   Draw a vector



                                                   Draw a bitmap


                            hidden canvas
                                                             visible canvas
Use a Bitmap Image Instead of Drawing




         It’s better to use already pre-rotated images
         rather than rotating them on the fly
Use a Bitmap Image Instead of Drawing




         Shadow, filter and gradation are expensive


          Performance enhanced by using prepared images
4. 5. 6.
Do Not Use Too Many Canvases
Do Not Use Too Many Canvases




         Do Not Use TOO MANY Canvases

         FPS
          60



          50



          40



          30



          20
                                                           FPS
          10



           0
                 1        2    3   4   5   6   7   8   9     10   Canvas
5. 6.
Android...
Android...




             What happens when one side of an image is larger
             than 2048px



             Large Image on Android 4.1.1   Same Image on Android 4.0.1
Android...




             Arrangement of Images in a Spritesheet
Android...




             Rotate3d bug in Android

 Android 4.X                           iPhone 4 iOS6
Android...




             Declare Every Element In Animation As 3D

       DIV -webkit-                         DIV -webkit-
       transform:translate3d(0, 0, 0); -    transform:translate3d(0, 0, 0); -
       webkit-transform-style:preserve-3d   webkit-transform-style:preserve-3d

         ㄴ IMG no styles                     ㄴ IMG -webkit-
                                            transform:translateZ(0)
Android...




             A bug found while using CSS3 3D Transforms
             and form element in Android

             The Image Will Become Larger When Keyboard Shows Up
6.
More Technical Points
More Technical Points




          Tile Caching
          Useful when you are dealing with many objects
          simultaneously

          Tile Caching enhances the performance
More Technical Points




          Dirty Rectangle
          Redraw only where there are changes.

                        redraw   redraw   skip   redraw
More Technical Points




          Tile Rendering




                           redraw
More Technical Points




          Polishing logics inside a tick is very important

          • 60 times in a second is 3600 times in a minute
          • Even a simple “if statement” can be problem




                                 Tick
                               16.667ms
Too Many To Consider?
There’s a library trying to solve all these problems.
Collie
Collie
Collie




         Tested on over 18 OS’s and devices
Collie




         Automatically selects the fastest drawing
         method for the device.
Collie




         What you can do with Collie
Collie




         What you can do with Collie
Collie




         What you can do with Collie
         Catch me if you can - realtime chess in Windows 8 Store
Collie




         20   kb
Collie




         Open Source


         LGPL v2.1
Collie




         http://jindo.dev.naver.com/collie
Questions?




             @davidshimjs
             ssm0123@gmail.com
Thank you

Contenu connexe

En vedette

Jessica cox 雙語
Jessica cox 雙語Jessica cox 雙語
Jessica cox 雙語
psjlew
 
敦煌藝術
敦煌藝術敦煌藝術
敦煌藝術
psjlew
 
人民大會堂吃家宴
人民大會堂吃家宴人民大會堂吃家宴
人民大會堂吃家宴
psjlew
 
劉兆玄的幽默(大家都適合看)
劉兆玄的幽默(大家都適合看)劉兆玄的幽默(大家都適合看)
劉兆玄的幽默(大家都適合看)
psjlew
 
油畫解析 伏爾加河上的縴夫
油畫解析 伏爾加河上的縴夫油畫解析 伏爾加河上的縴夫
油畫解析 伏爾加河上的縴夫
psjlew
 
二代健保101-04-01
二代健保101-04-01二代健保101-04-01
二代健保101-04-01
psjlew
 
Monitoring with Icinga @ SF Bay Area LSPE meetup
Monitoring with Icinga @ SF Bay Area LSPE meetupMonitoring with Icinga @ SF Bay Area LSPE meetup
Monitoring with Icinga @ SF Bay Area LSPE meetup
mdxp
 
民谚集成
民谚集成民谚集成
民谚集成
psjlew
 
心臟血管通暢的自然療法
心臟血管通暢的自然療法心臟血管通暢的自然療法
心臟血管通暢的自然療法
psjlew
 
咖啡與肝病
咖啡與肝病咖啡與肝病
咖啡與肝病
psjlew
 
壺口的瀑布~肖鐵
壺口的瀑布~肖鐵壺口的瀑布~肖鐵
壺口的瀑布~肖鐵
psjlew
 
蒋介石与毛泽东的面相
蒋介石与毛泽东的面相蒋介石与毛泽东的面相
蒋介石与毛泽东的面相
psjlew
 
六種眼疾原因
六種眼疾原因六種眼疾原因
六種眼疾原因
psjlew
 
一群自稱是中國人的日耳曼土著
一群自稱是中國人的日耳曼土著一群自稱是中國人的日耳曼土著
一群自稱是中國人的日耳曼土著
psjlew
 

En vedette (20)

Jessica cox 雙語
Jessica cox 雙語Jessica cox 雙語
Jessica cox 雙語
 
敦煌藝術
敦煌藝術敦煌藝術
敦煌藝術
 
人民大會堂吃家宴
人民大會堂吃家宴人民大會堂吃家宴
人民大會堂吃家宴
 
Rubyのオブジェクト図をもう一度
Rubyのオブジェクト図をもう一度Rubyのオブジェクト図をもう一度
Rubyのオブジェクト図をもう一度
 
劉兆玄的幽默(大家都適合看)
劉兆玄的幽默(大家都適合看)劉兆玄的幽默(大家都適合看)
劉兆玄的幽默(大家都適合看)
 
油畫解析 伏爾加河上的縴夫
油畫解析 伏爾加河上的縴夫油畫解析 伏爾加河上的縴夫
油畫解析 伏爾加河上的縴夫
 
二代健保101-04-01
二代健保101-04-01二代健保101-04-01
二代健保101-04-01
 
2010游戏设计
2010游戏设计2010游戏设计
2010游戏设计
 
Monitoring with Icinga @ SF Bay Area LSPE meetup
Monitoring with Icinga @ SF Bay Area LSPE meetupMonitoring with Icinga @ SF Bay Area LSPE meetup
Monitoring with Icinga @ SF Bay Area LSPE meetup
 
民谚集成
民谚集成民谚集成
民谚集成
 
心臟血管通暢的自然療法
心臟血管通暢的自然療法心臟血管通暢的自然療法
心臟血管通暢的自然療法
 
咖啡與肝病
咖啡與肝病咖啡與肝病
咖啡與肝病
 
壺口的瀑布~肖鐵
壺口的瀑布~肖鐵壺口的瀑布~肖鐵
壺口的瀑布~肖鐵
 
Practise fonts
Practise fontsPractise fonts
Practise fonts
 
蒋介石与毛泽东的面相
蒋介石与毛泽东的面相蒋介石与毛泽东的面相
蒋介石与毛泽东的面相
 
六種眼疾原因
六種眼疾原因六種眼疾原因
六種眼疾原因
 
Marutisuzuki Ltd
Marutisuzuki LtdMarutisuzuki Ltd
Marutisuzuki Ltd
 
Google Analytics IQ Lesson 2: Interpreting Results
Google Analytics IQ Lesson 2: Interpreting ResultsGoogle Analytics IQ Lesson 2: Interpreting Results
Google Analytics IQ Lesson 2: Interpreting Results
 
手机城市
手机城市手机城市
手机城市
 
一群自稱是中國人的日耳曼土著
一群自稱是中國人的日耳曼土著一群自稱是中國人的日耳曼土著
一群自稱是中國人的日耳曼土著
 

Similaire à High Performance Mobile Web Game Development in HTML5

Building mobile apps with the ArcGIS api for Javascript, Esri, Andy Gup and A...
Building mobile apps with the ArcGIS api for Javascript, Esri, Andy Gup and A...Building mobile apps with the ArcGIS api for Javascript, Esri, Andy Gup and A...
Building mobile apps with the ArcGIS api for Javascript, Esri, Andy Gup and A...
Esri Nederland
 

Similaire à High Performance Mobile Web Game Development in HTML5 (20)

HTML5 Animation in Mobile Web Games
HTML5 Animation in Mobile Web GamesHTML5 Animation in Mobile Web Games
HTML5 Animation in Mobile Web Games
 
Whats new in Feathers 3.0?
Whats new in Feathers 3.0?Whats new in Feathers 3.0?
Whats new in Feathers 3.0?
 
Mapping the world with Twitter
Mapping the world with TwitterMapping the world with Twitter
Mapping the world with Twitter
 
Building a game engine with jQuery
Building a game engine with jQueryBuilding a game engine with jQuery
Building a game engine with jQuery
 
Augmented reality in web rtc browser
Augmented reality in web rtc browserAugmented reality in web rtc browser
Augmented reality in web rtc browser
 
Intro to @viewport & other new Responsive Web Design CSS features
Intro to @viewport & other new Responsive Web Design CSS featuresIntro to @viewport & other new Responsive Web Design CSS features
Intro to @viewport & other new Responsive Web Design CSS features
 
Hi performance table views with QuartzCore and CoreText
Hi performance table views with QuartzCore and CoreTextHi performance table views with QuartzCore and CoreText
Hi performance table views with QuartzCore and CoreText
 
Picture Perfect: Images for Coders
Picture Perfect: Images for CodersPicture Perfect: Images for Coders
Picture Perfect: Images for Coders
 
Building mobile apps with the ArcGIS api for Javascript, Esri, Andy Gup and A...
Building mobile apps with the ArcGIS api for Javascript, Esri, Andy Gup and A...Building mobile apps with the ArcGIS api for Javascript, Esri, Andy Gup and A...
Building mobile apps with the ArcGIS api for Javascript, Esri, Andy Gup and A...
 
Before Going Vector
Before Going VectorBefore Going Vector
Before Going Vector
 
Practical tipsmakemobilefaster oscon2016
Practical tipsmakemobilefaster oscon2016Practical tipsmakemobilefaster oscon2016
Practical tipsmakemobilefaster oscon2016
 
Web mapping with vector data. Is it the future ? 2012
Web mapping with vector data. Is it the future ? 2012Web mapping with vector data. Is it the future ? 2012
Web mapping with vector data. Is it the future ? 2012
 
Responsive Web Design: Clever Tips and Techniques
Responsive Web Design: Clever Tips and TechniquesResponsive Web Design: Clever Tips and Techniques
Responsive Web Design: Clever Tips and Techniques
 
Building VR Applications For Google Cardboard
Building VR Applications For Google CardboardBuilding VR Applications For Google Cardboard
Building VR Applications For Google Cardboard
 
Work With Images
Work With ImagesWork With Images
Work With Images
 
Better DITA Graphics for a Multi-Screen World
Better DITA Graphics for a Multi-Screen WorldBetter DITA Graphics for a Multi-Screen World
Better DITA Graphics for a Multi-Screen World
 
Ember.js Tokyo event 2014/09/22 (English)
Ember.js Tokyo event 2014/09/22 (English)Ember.js Tokyo event 2014/09/22 (English)
Ember.js Tokyo event 2014/09/22 (English)
 
Migrating your Web app to Virtual Reality
Migrating your Web app to Virtual RealityMigrating your Web app to Virtual Reality
Migrating your Web app to Virtual Reality
 
Implementing CATiledLayer
Implementing CATiledLayerImplementing CATiledLayer
Implementing CATiledLayer
 
CATiled Layer - Melbourne Cocoheads February 2012
CATiled Layer - Melbourne Cocoheads February 2012CATiled Layer - Melbourne Cocoheads February 2012
CATiled Layer - Melbourne Cocoheads February 2012
 

High Performance Mobile Web Game Development in HTML5

  • 1. High Performance Mobile Web Game Development in HTML5 Sangmin, Shim
  • 2. I work at NHN
  • 3. See the demo first http://goo.gl/JJbaQ
  • 4. You can easily build games on desktops
  • 6. Many OS’s 4/5/6 2.1 / 2.2 / 2.3 / 2.3.2 / 2.3.3 / 2.3.7 / 3.1 / 3.2 / 4.0.1 / 4.0.3 / 4.0.4 / 4.1 ....
  • 9. Limited Sound Support • Cannot play multiple sounds simultaneously • iOS requires user input to play sound • Slow downs the performance Background music Effect
  • 10. Two worst enemies in mobile game development Performance & Sound
  • 12. Web Audio API Background music Effect
  • 13. Better performance in iOS6 FPS (Frame per Second) FPS 60 60 iOS6 iOS6 353%↑ 68%↑ 50 50 40 40 iOS5 30 30 20 20 10 iOS4 10 0 0 100 objects were animated using Collie on iPhone4
  • 14. Let’s start to build your own mobile game!
  • 15. 1. 2. 3. 4. 5. 6. Both Canvas And CSS3 3D Transforms have to be supported
  • 16. Both Canvas And CSS3 3D Transforms have to be supported Need to use different rendering methods depending on the platform to get hardware acceleration Hardware Accelerated Canvas Animation in iOS5
  • 17. Both Canvas And CSS3 3D Transforms have to be supported iOS4 did not have hardware acceleration support Performance comparison between iOS4 and iOS5
  • 18. Both Canvas And CSS3 3D Transforms have to be supported iOS4 supports hardware acceleration when using CSS 3D Transforms Performance comparison between iOS4 and iOS5 with different rendering methods
  • 19. Both Canvas And CSS3 3D Transforms have to be supported "O, iOS4 is such an old platform, maybe my game does not need to support it"
  • 20. Both Canvas And CSS3 3D Transforms have to be supported Renderings Used iPhone iPhone Android Android 3GS or 4 4S 2.x 3 or higher CSS 3D (less than iOS5) Canvas Rendering Canvas or CSS 3D DOM Canvas (iOS5 over)
  • 21. Both Canvas And CSS3 3D Transforms have to be supported "Well, most mobile devices support CSS3 3D transforms, so I’ll just ditch Canvas totally and go with DOM"
  • 22. Both Canvas And CSS3 3D Transforms have to be supported Performance Comparison between Canvas and DOM in iOS5 FPS 60 50 40 Canvas 30 CSS3 3D Transforms 20 10 0 10 20 30 50 100 Objects
  • 23. 2. 3. 4. 5. 6. Need to find out the exact extent of an object
  • 24. Need to find out the exact extent of an object Must find out the exact extent of an object
  • 25. Need to find out the exact extent of an object How to find the exact extent of an object using getImageData method in Canvas var el = document.createElement("canvas"); el.width = 200; el.height = 200; var ctx = el.getContext("2d"); ctx.fillStyle = "red"; ctx.fillRect(10, 10, 100, 100); var imageData = ctx.getImageData(20, 20, 1, 1); if ( imageData.data[0] !== 0 || imageData.data[1] !== 0 || imageData.data[2] !== 0 || imageData.data[3] !== 0 ) { console.log("here it is!"); }
  • 26. Need to find out the exact extent of an object Security Issue with Canvas Element • Information leakage can occur if scripts from one origin can access information (e.g. read pixels) from images from another origin (one that isn't the same) • The toDataURL(), toBlob(), and getImageData() methods check the flag and will throw a SecurityError exception rather than leak cross- origin data
  • 27. Need to find out the exact extent of an object Predefining the area [[126, 285],[104, 286],[101, 267],[105, 221],[101, 213],[88, 188],[85, 168],[81, 153],[94, 133],[97, 103],[94, 60],[95, 29],[103, 32],[109, 97],[143, 93],[147, 33],[151, 31],[152, 97],[161, 137],[166, 167],[165, 188],[169, 200],[165, 208],[160, 209],[159, 235],[159, 256],[159, 269],[162, 275],[162, 286],[138, 288],[130, 275],[132, 254]]
  • 28. Need to find out the exact extent of an object Performance comparison between getImageData and PNPOLY Operations 70000 60000 50000 40000 9,930%↑ operations/sec 30000 20000 10000 0 getImageData() PNPOLY Algorithm http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
  • 29. Need to find out the exact extent of an object Graphic Designer I'm a Graphic Designer
  • 30. 3. 4. 5. 6. Use a Bitmap Image Instead of Drawing
  • 31. Use a Bitmap Image Instead of Drawing Drawing Vectors is Slow Performance comparison between vector drawing and using images
  • 32. Use a Bitmap Image Instead of Drawing Object Caching Non Object Caching Draw a vector Use Object Caching Draw a vector Draw a bitmap hidden canvas visible canvas
  • 33. Use a Bitmap Image Instead of Drawing It’s better to use already pre-rotated images rather than rotating them on the fly
  • 34. Use a Bitmap Image Instead of Drawing Shadow, filter and gradation are expensive Performance enhanced by using prepared images
  • 35. 4. 5. 6. Do Not Use Too Many Canvases
  • 36. Do Not Use Too Many Canvases Do Not Use TOO MANY Canvases FPS 60 50 40 30 20 FPS 10 0 1 2 3 4 5 6 7 8 9 10 Canvas
  • 38. Android... What happens when one side of an image is larger than 2048px Large Image on Android 4.1.1 Same Image on Android 4.0.1
  • 39. Android... Arrangement of Images in a Spritesheet
  • 40. Android... Rotate3d bug in Android Android 4.X iPhone 4 iOS6
  • 41. Android... Declare Every Element In Animation As 3D DIV -webkit- DIV -webkit- transform:translate3d(0, 0, 0); - transform:translate3d(0, 0, 0); - webkit-transform-style:preserve-3d webkit-transform-style:preserve-3d ㄴ IMG no styles ㄴ IMG -webkit- transform:translateZ(0)
  • 42. Android... A bug found while using CSS3 3D Transforms and form element in Android The Image Will Become Larger When Keyboard Shows Up
  • 44. More Technical Points Tile Caching Useful when you are dealing with many objects simultaneously Tile Caching enhances the performance
  • 45. More Technical Points Dirty Rectangle Redraw only where there are changes. redraw redraw skip redraw
  • 46. More Technical Points Tile Rendering redraw
  • 47. More Technical Points Polishing logics inside a tick is very important • 60 times in a second is 3600 times in a minute • Even a simple “if statement” can be problem Tick 16.667ms
  • 48. Too Many To Consider?
  • 49. There’s a library trying to solve all these problems.
  • 52. Collie Tested on over 18 OS’s and devices
  • 53. Collie Automatically selects the fastest drawing method for the device.
  • 54. Collie What you can do with Collie
  • 55. Collie What you can do with Collie
  • 56. Collie What you can do with Collie Catch me if you can - realtime chess in Windows 8 Store
  • 57. Collie 20 kb
  • 58. Collie Open Source LGPL v2.1
  • 59. Collie http://jindo.dev.naver.com/collie
  • 60. Questions? @davidshimjs ssm0123@gmail.com