Skip to content

Instantly share code, notes, and snippets.

@zeropaper
Last active January 12, 2017 16:10
Show Gist options
  • Save zeropaper/3837007cc5d7b9ee8091639bb277a318 to your computer and use it in GitHub Desktop.
Save zeropaper/3837007cc5d7b9ee8091639bb277a318 to your computer and use it in GitHub Desktop.
Visual Fiha canvas layer boilerplate
<canvas id="canvas" width="400" height="300"></canvas>
function drawCtx(ctx) {
var cache = this.cache || {};
cache.frame = cache.frame || 0;
cache.frame++;
var cw = ctx.canvas.width * 0.5;
var ch = ctx.canvas.height * 0.5;
var txt = cache.frame + ' ' + this.screenState.frametime + ' ' + this.screenState.firstframetime + ' ';
ctx.fillText(txt, cw, ch);
};
// vvvvvvvvv the rest is just here to emulate vvvvvvvvv
var canvas = document.getElementById('canvas');
canvas.parentNode.style.backgroundColor = '#eee';
canvas.width = canvas.parentNode.clientWidth;
canvas.height = canvas.parentNode.clientHeight;
var context = canvas.getContext('2d');
var layer = {
cache: {},
screenState: {
frametime: 0,
firstframetime: performance.now()
}
};
function animate() {
context.clearRect(0, 0, context.canvas.width, context.canvas.height);
drawCtx.call(layer, context);
window.requestAnimationFrame(animate);
}
animate();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment