Jump to content

[HTML5/Javascript] Couple questions, particularly dealing with Canvas


FalseProphet

Recommended Posts

Hi, I am pretty new to Javascript and would like to play with the Canvas and I have a couple questions.

First off, how do I record frame rate or operations per second properly? In my current code I only get around 170 FPS with nothing on the canvas and the canvas being  default size:

// GetElapsedMilliseconds = Function() { var time = new Date(); return time.getMilliseconds(); }
if (GetElapsedMilliseconds() - oldTime >=1000) {
	OPS = count;
	count = 0;
	oldTime = GetElapsedMilliseconds();
}

++count;
loop = setTimeout('Main()', 1);

 

Another thing, am I doing Javascript's OOP implementation correctly?

myscene = new _Context("thecanvas");
function _Context(id) {

/*

	description:	Retrieve the canvas context by id, if it can't throw 
					an error and reason.

*/

if (document.getElementById(id)) {
		this.canvas = document.getElementById(id).getContext("2d");
} else {
	alert("Error\nUnable to find element ID: " + id);
	return null;
}


this.DrawText = function(x, y, string, size, font, color) { // draw some text inside of the canvas

	/*

		description:	Draw text to the canvas.

	*/

	this.canvas.font = size + "px " + font;
	this.canvas.fillStyle = color;
	this.canvas.fillText(string, x, y);

}
}

  • 2 weeks later...

170FPS is probably right. Browsers throttle timers to a minimum limit, so your setTimeout('Main()', 1) isn't being called every millisecond. Each browser is different, I think for chrome the minimum is 3ms and for FF it's 5ms or something like that, but don't quote me on it.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.