javascript - HTML5 canvas performance enhancements -


i working on javascript canvas game , improve performance of game. reading articles how achieve better performance - 1 technique being pre-rendering.

does make sense render every object, each of has texture, it's own separate canvas element? here example of entity rendering:

    fruitless.ctx.save();         fruitless.ctx.translate(this.body.getposition().x,this.body.getposition().y);         fruitless.ctx.rotate(this.body.getangle());         fruitless.ctx.scale(this.scale.x, this.scale.y);          fruitless.ctx.drawimage(this.texture, ... )         this.face.draw();     fruitless.ctx.restore(); 

so running drawimage() function each iteration... pre-rendering suggests drawimage() should done in initialisation (just once) - right?

hard give specific recommendations without knowing more...but here's start:

  1. put static background elements in html image , lay image down first. scroll background image if static larger game viewport.

  2. sort animated elements when need animate several groups. sun , cloud elements animate on frame#5 1 group. grape-man , raison-man animate every frame in different group. create canvas each of these several groups.

  3. put infrequently animated elements on sprite-sheet.

  4. put animated elements in own image object.

  5. put re-textured elements in own offscreen canvas , re-texture there. here's trade: canvas's operate poorly on mobile, don't want lot of canvases on mobile. pre-rendering variations of textures image objects takes lot of memory.

bottom line:

pre-rendering undoubtedly give performance boost.

but need test various pre-rendering strategies see works best on device


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -