Created
April 5, 2011 10:16
-
-
Save mrdoob/903379 to your computer and use it in GitHub Desktop.
Sprite sheet script.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var canvas = document.createElement( 'canvas' ); | |
canvas.width = 2048; | |
canvas.height = 2048; | |
canvas.addEventListener( 'click', function() { | |
window.open( canvas.toDataURL( 'image/png' ), 'mywindow' ); | |
}, false ); | |
var context = canvas.getContext( '2d' ); | |
/* | |
context.fillStyle = 'rgb(255,0,0)'; | |
context.fillRect( 0, 0, canvas.width, canvas.height ); | |
*/ | |
document.body.appendChild( canvas ); | |
for ( var i = 0, j = 0; i <= 62; i++, j += 2) { | |
loadBitmap( i + 0, 'tree_front/tree_front_lighter' + pad( j, 4 ) + '.png' ); | |
} | |
function pad( value, amount ) { | |
var string = value.toString(); | |
while ( string.length < amount ) { | |
string = '0' + string; | |
} | |
return string; | |
} | |
function loadBitmap( id, path ) { | |
var bitmap = document.createElement( 'img' ); | |
bitmap.addEventListener( 'load', function () { | |
var x = ( id * 256 ) % canvas.width; | |
var y = Math.floor( ( id * 256 ) / canvas.width ) * 256; | |
context.drawImage( this, x, y ); | |
}, false ); | |
bitmap.src = path; | |
return bitmap; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment