Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // -------------------- Variables -------------------- //
- var canvas = document.getElementById('myCanvas');
- var context = canvas.getContext('2d');
- var requestAnimFrame = (function(){
- return window.requestAnimationFrame ||
- window.webkitRequestAnimationFrame ||
- window.mozRequestAnimationFrame ||
- window.oRequestAnimationFrame ||
- window.msRequestAnimationFrame ||
- function(/* function */ callback, /* DOMElement */ element){
- window.setTimeout(callback, 1000 / fps);
- };
- })();
- animate();
- // FPS timer
- var FpsTimer = new Date().getTime();
- // This is request for update
- var fps = 100;
- // Current count of fps
- var FpsCounter = 0;
- // FPS actual holds the last recorded fps
- var FpsActual = 0;
- // FPS timer
- var TileSet = new Image();
- TileSet.src = 'images/test.tileset.png';
- var TileSetLoaded = false;
- TileSet.onload = function(){
- TileSetLoaded = true;
- }
- var idTimer = new Date().getTime();
- var idUpdate = 500;
- var nextID = 2;
- var getNextId = function(){
- if ( new Date().getTime() - idTimer >= idUpdate){
- idTimer = new Date().getTime();
- nextID++;
- if (nextID > 189)
- nextID = 1;
- }
- return nextID;
- }
- var tileID = 0;
- var tileGetID = [];
- // Load tileGetID for tileid retrieval
- // Tiledisplay for coordinates to each tile image
- for ( var i = 0; i < 19 ; i++)
- {
- tileGetID[i] = {x:0,y:0};
- for ( var j = 0; j < 10 ; j++)
- {
- tileGetID[tileID] = { x : (i * 16), y: (j * 16)};
- tileID++;
- }
- }
- var drawTile = function( id, xC, yC){
- context.drawImage(TileSet,tileGetID[id].x,tileGetID[id].y,16,16,xC,yC,16,16);
- }
- // This helps keep track of actual FPS
- function updateFPS()
- {
- FpsCounter+=1;
- var fpsTime = new Date().getTime() - FpsTimer;
- //reset counter/timer and log fps
- if (fpsTime >= 1000)
- {
- FpsTimer = new Date().getTime();
- FpsActual = FpsCounter;
- FpsCounter = 0;
- }
- }
- function animate() {
- requestAnimFrame( animate );
- // Clear and recenter the screen
- context.clearRect(0, 0, canvas.width, canvas.height);
- draw();
- }
- function draw()
- {
- //updating our fps tracker
- updateFPS();
- context.fillText("FPS :[ "+ FpsActual +" ]",20,15);
- if(TileSetLoaded == true){
- var next = getNextId();
- drawTile(next,50,50);
- context.fillText("Tile :[ "+ next +" ]",50,100);
- context.fillText("Time :[ "+ (new Date().getTime() - idTimer) +" ]",50,115);
- context.fillText("Tile :[ X:"+ tileGetID[next].x +" |Y:" + tileGetID[next].y + " ]",50,130);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement