Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- WHAT IS IT
- This is a game library object for helping to create games.
- WHAT DOES IT DO
- It will take the canvas and resize it to the window size even if the window is resized later by the user. It will also add a black background to the canvas. It will create a canvas object from the canvas in 2D context. It can also start a game loop and keep track of FPS.
- Author: Zach Dyer
- Website: ZachDyerDesign.com
- Version: 0.1.0
- */
- function Game(canvasID) {
- //Take out body spacing
- document.body.style.margin = 0;
- document.body.style.padding = 0;
- var canvasElement = document.getElementById(canvasID);
- //Canvas full screen with black background
- canvasElement.width = window.innerWidth;
- canvasElement.height = window.innerHeight;
- canvasElement.style.backgroundColor = "black";
- //Canvas size updates when window size changes
- window.onresize = function() {
- canvasElement.width = window.innerWidth;
- canvasElement.height = window.innerHeight;
- };
- this.canvas = canvasElement.getContext("2d");
- //Total frames per second
- this.fps = 0;
- var currentTime = new Number();
- var lastTime = new Number();
- //Can't use the this keyword in this.functions unless I save it in a variable
- var self = this;
- var interval;
- var getFPS = function(currentTime, lastTime) {
- var fps = 1000 / (currentTime - lastTime);
- return fps.toFixed();
- };
- var step = function(custom){
- currentTime = Date.now();
- self.fps = getFPS(currentTime, lastTime);
- lastTime = currentTime;
- custom();
- };
- this.loop = function (custom) {
- interval = setInterval(function(){
- step(custom);
- },17);
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement