high performance mobile web game development in html5

61
High Performance Mobile Web Game Development in HTML5 Sangmin, Shim

Upload: sangmin-shim

Post on 22-Jun-2015

4.289 views

Category:

Documents


0 download

DESCRIPTION

Attached Movies p.16) https://vimeo.com/47706328 p.17) https://vimeo.com/47706331 p.18) https://vimeo.com/47706329 p.31) https://vimeo.com/54573920 p.34) https://vimeo.com/54573922 p.41) https://vimeo.com/54573923 p.42) https://vimeo.com/54573924 p.44) https://vimeo.com/54573925 p.45) https://vimeo.com/54574009

TRANSCRIPT

Page 1: High Performance Mobile Web Game Development in HTML5

High Performance Mobile

Web Game Development

in HTML5

Sangmin, Shim

Page 2: High Performance Mobile Web Game Development in HTML5

I work at NHN

Page 3: High Performance Mobile Web Game Development in HTML5

See the demo first

http://goo.gl/JJbaQ

Page 4: High Performance Mobile Web Game Development in HTML5

You can easily build games on desktops

Page 5: High Performance Mobile Web Game Development in HTML5

Mobile’s different

Page 6: High Performance Mobile Web Game Development in HTML5

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

....

Page 7: High Performance Mobile Web Game Development in HTML5

Various Devices

Page 8: High Performance Mobile Web Game Development in HTML5

Slower

Page 9: High Performance Mobile Web Game Development in HTML5

Limited Sound Support

• Cannot play multiple sounds simultaneously

• iOS requires user input to play sound

• Slow downs the performance

Background music

Effect

Page 10: High Performance Mobile Web Game Development in HTML5

Two worst enemies in mobile game development

Performance & Sound

Page 11: High Performance Mobile Web Game Development in HTML5

Mobile is evolving rapidly

Page 12: High Performance Mobile Web Game Development in HTML5

Web Audio API

Background music

Effect

Page 13: High Performance Mobile Web Game Development in HTML5

0

10

20

30

40

50

60

Better performance in iOS6

0

10

20

30

40

50

60

68%↑ 353%↑ iOS6 iOS6

FPS (Frame per Second)

iOS4

iOS5

100 objects were animated using Collie on iPhone4

FPS

Page 14: High Performance Mobile Web Game Development in HTML5

Let’s start to build your own mobile game!

Page 15: High Performance Mobile Web Game Development in HTML5

Both Canvas And CSS3 3D Transforms have to be

supported

1. 2. 3. 4. 5. 6.

Page 16: High Performance Mobile Web Game Development in HTML5

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

Page 17: High Performance Mobile Web Game Development in HTML5

iOS4 did not have hardware acceleration support

Performance comparison between iOS4 and iOS5

Both Canvas And CSS3 3D Transforms have to be supported

Page 18: High Performance Mobile Web Game Development in HTML5

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

Page 19: High Performance Mobile Web Game Development in HTML5

"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

Page 20: High Performance Mobile Web Game Development in HTML5

Renderings Used

iPhone

3GS or 4

iPhone

4S

Android

2.x

Android

3 or higher

Rendering

CSS 3D

(less than iOS5)

Canvas

Canvas

or

DOM

CSS 3D

Canvas

(iOS5 over)

Both Canvas And CSS3 3D Transforms have to be supported

Page 21: High Performance Mobile Web Game Development in HTML5

"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

Page 22: High Performance Mobile Web Game Development in HTML5

Performance Comparison between Canvas and

DOM in iOS5

0

10

20

30

40

50

60

10 20 30 50 100

FPS

Objects

Canvas

CSS3 3D Transforms

Both Canvas And CSS3 3D Transforms have to be supported

Page 23: High Performance Mobile Web Game Development in HTML5

Need to find out the exact extent of an object

2. 3. 4. 5. 6.

Page 24: High Performance Mobile Web Game Development in HTML5

Must find out the exact extent of an object

Need to find out the exact extent of an object

Page 25: High Performance Mobile Web Game Development in HTML5

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

Page 26: High Performance Mobile Web Game Development in HTML5

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

Page 27: High Performance Mobile Web Game Development in HTML5

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

Page 28: High Performance Mobile Web Game Development in HTML5

Performance comparison between

getImageData and PNPOLY

0

10000

20000

30000

40000

50000

60000

70000

getImageData() PNPOLY Algorithm

operations/sec

9,930%↑

Operations

Need to find out the exact extent of an object

http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html

Page 29: High Performance Mobile Web Game Development in HTML5

Graphic Designer

Need to find out the exact extent of an object

I'm a Graphic Designer

Page 30: High Performance Mobile Web Game Development in HTML5

Use a Bitmap Image Instead of Drawing

3. 4. 5. 6.

Page 31: High Performance Mobile Web Game Development in HTML5

Drawing Vectors is Slow

Performance comparison between vector drawing and using images

Use a Bitmap Image Instead of Drawing

Page 32: High Performance Mobile Web Game Development in HTML5

Object Caching

hidden canvas

visible canvas

Draw a vector

Draw a bitmap

Use a Bitmap Image Instead of Drawing

Draw a vector

Use Object Caching

Non Object Caching

Page 33: High Performance Mobile Web Game Development in HTML5

It’s better to use already pre-rotated images

rather than rotating them on the fly

Use a Bitmap Image Instead of Drawing

Page 34: High Performance Mobile Web Game Development in HTML5

Shadow, filter and gradation are expensive

Use a Bitmap Image Instead of Drawing

Performance enhanced by using prepared images

Page 35: High Performance Mobile Web Game Development in HTML5

Do Not Use Too Many Canvases

4. 5. 6.

Page 36: High Performance Mobile Web Game Development in HTML5

Do Not Use TOO MANY Canvases

0

10

20

30

40

50

60

1 2 3 4 5 6 7 8 9 10

FPS

Canvas

FPS

Do Not Use Too Many Canvases

Page 37: High Performance Mobile Web Game Development in HTML5

Android...

5. 6.

Page 38: High Performance Mobile Web Game Development in HTML5

What happens when one side of an image is larger

than 2048px

Android...

Large Image on Android 4.1.1 Same Image on Android 4.0.1

Page 39: High Performance Mobile Web Game Development in HTML5

Arrangement of Images in a Spritesheet

Android...

Page 40: High Performance Mobile Web Game Development in HTML5

Android 4.X

Rotate3d bug in Android

Android...

iPhone 4 iOS6

Page 41: High Performance Mobile Web Game Development in HTML5

Declare Every Element In Animation As 3D

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

ㄴ IMG no styles

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

ㄴ IMG -webkit-transform:translateZ(0)

Android...

Page 42: High Performance Mobile Web Game Development in HTML5

A bug found while using CSS3 3D Transforms

and form element in Android

Android...

The Image Will Become Larger When Keyboard Shows Up

Page 43: High Performance Mobile Web Game Development in HTML5

More Technical Points

6.

Page 44: High Performance Mobile Web Game Development in HTML5

Tile Caching

Useful when you are dealing with many objects

simultaneously

More Technical Points

Tile Caching enhances the performance

Page 45: High Performance Mobile Web Game Development in HTML5

Dirty Rectangle

Redraw only where there are changes.

skip redraw redraw redraw

More Technical Points

Page 46: High Performance Mobile Web Game Development in HTML5

Tile Rendering

More Technical Points

redraw

Page 47: High Performance Mobile Web Game Development in HTML5

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

More Technical Points

Page 48: High Performance Mobile Web Game Development in HTML5

Too Many To Consider?

Page 49: High Performance Mobile Web Game Development in HTML5

There’s a library trying to solve all these problems .

Page 50: High Performance Mobile Web Game Development in HTML5

Collie

Page 51: High Performance Mobile Web Game Development in HTML5

Collie

Page 52: High Performance Mobile Web Game Development in HTML5

Tested on over 18 OS’s and devices

Collie

Page 53: High Performance Mobile Web Game Development in HTML5

Automatically selects the fastest drawing

method for the device.

Collie

Page 54: High Performance Mobile Web Game Development in HTML5

What you can do with Collie

Collie

Page 55: High Performance Mobile Web Game Development in HTML5

What you can do with Collie

Collie

Page 56: High Performance Mobile Web Game Development in HTML5

What you can do with Collie

Catch me if you can - realtime chess in Windows 8 Store

Collie

Page 57: High Performance Mobile Web Game Development in HTML5

20kb

Collie

Page 58: High Performance Mobile Web Game Development in HTML5

Open Source

LGPL v2.1

Collie

Page 59: High Performance Mobile Web Game Development in HTML5

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

Collie

Page 60: High Performance Mobile Web Game Development in HTML5

Questions?

@davidshimjs [email protected]

Page 61: High Performance Mobile Web Game Development in HTML5

Thank you