Skip to content

Commit

Permalink
improve performance by prerendering background
Browse files Browse the repository at this point in the history
  • Loading branch information
master-lincoln committed Feb 16, 2014
1 parent 58cb59a commit 8e5b43f
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ define(['bird', 'pipe', 'score', '../lib/promise-0.1.1.min'], function(bird, Pip

var flash = false;

var bgCanvas = document.createElement('canvas');
bgCanvas.width = 640;
bgCanvas.height = 480;
var bgCtx = bgCanvas.getContext('2d');

function drawImage(name, x, y, ctx) {
var el = atlasMap[name];
if (el) {
Expand Down Expand Up @@ -60,7 +65,13 @@ define(['bird', 'pipe', 'score', '../lib/promise-0.1.1.min'], function(bird, Pip
pipes.push(new Pipe(game));
pipes.push(new Pipe(game));

return Promise.all([promMap, promImg]).then(parseAtlasMap);
return Promise.all([promMap, promImg])
.then(parseAtlasMap)
.then(function() {
drawImage("bg", 0, 0, bgCtx);
drawImage("bg", 288, 0, bgCtx);
drawImage("bg", 2*288, 0, bgCtx);
});
}

function addAndRemovePipes() {
Expand Down Expand Up @@ -125,9 +136,7 @@ define(['bird', 'pipe', 'score', '../lib/promise-0.1.1.min'], function(bird, Pip
}

function drawBG(ctx) {
drawImage("bg", 0, 0, ctx);
drawImage("bg", 288, 0, ctx);
drawImage("bg", 2*288, 0, ctx);
ctx.drawImage(bgCanvas, 0, 0);
}

function drawGround(ctx) {
Expand Down Expand Up @@ -171,7 +180,7 @@ define(['bird', 'pipe', 'score', '../lib/promise-0.1.1.min'], function(bird, Pip
SIZE : [640,480],

// 16 ms (decrease to make game faster)
tickTime: ~~(1/30 *1000),
tickTime: ~~(1/60 *1000),

// last tick UNIX timestamp
lastTickTime : window.performance.now(),
Expand Down

0 comments on commit 8e5b43f

Please sign in to comment.