Hello! This is the official page of Wayne Sebastian Pereanu.

Software > Cheat Sheets > JavaScript


Pre-load images on canvas
var curr_page, img, imgToLoad, imgLoaded
function onImageLoad () {
	imgLoaded++;
	if (imgLoaded == imgToLoad) {
		all_images_have_loaded();
	}
}
function load_images () {
	imgLoaded = 0;          //reset this as we are loading a new batch of images
	//what needs to be loaded for the current page
	switch (curr_page) {
		case "title":
			imgToLoad = 1;
			var img_name = "title.jpg";
			break;
	}
	//background image
	img = new Image();
	img.onload = function() { onImageLoad(); }
	img.src = img_name;
}

Draw an image on canvas
var c = document.getElementById("myCanvas");
var context = c.getContext("2d");
context.drawImage( img_back, 0, 0);

Switch
switch (num) {
	case 1:
		#do something if num=1
		break;
	case 2:
		#do something if num=2
		break;
	default:
		#do something if num is anything else
}

Arrays
var this_x = new Array();
this_x.push( ev.touches[i].pageX );

For Loop
for (i=0; i<5; i++) {
	console.log( i );
}