
lib.viewport.js Maven / Gradle / Ivy
The newest version!
// shim layer with setTimeout fallback
// TODO: remove this, replace as chrome extension
window.requestAnimFrame = (function () {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(callback) {
window.setTimeout(callback, 1000 / 60);
};
}());
function Viewport(canvasId) {
var canvas = document.getElementById(canvasId),
context = canvas.getContext('2d'),
originalWidth = canvas.width,
originalHeight = canvas.height,
running = false,
viewport = this;
this.getWidth = function() { return originalWidth; };
this.getHeight = function() { return originalHeight; };
this.getCanvas = function() { return canvas; };
this.getContext = function() { return context; };
this.withCanvas = function(callback) { callback(canvas); };
this.withContext = function(callback) { callback(context); };
this.withCanvasAndContext = function(callback) {
callback(canvas, context);
};
this.isRunning = function() { return running; };
this.start = function() {
running = true;
viewport.animate();
};
this.stop = function() {
running = false;
};
this.animate = function() {
if (viewport.isRunning()) {
$(viewport).trigger('reload');
window.requestAnimFrame(viewport.animate);
}
};
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy