Created
December 27, 2013 01:03
-
-
Save frostney/8141002 to your computer and use it in GitHub Desktop.
TextureManager proof-of-concept with Q written in CoffeeScript
This file contains hidden or 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
class TextureManager | |
constructor: -> | |
@textures = {} | |
load: (texture, callback) -> | |
texture = [texture] unless Array.isArray texture | |
textures = @textures | |
promises = for t in texture | |
(cb) -> | |
image = new Image() | |
image.src = t | |
image.onload = -> | |
# TODO: Normalize path | |
textures[t] = new Texture image | |
cb() | |
Q.allSettled(promises).then(callback) | |
class Texture | |
constructor: (image, pixelData = null) -> | |
{@width, @height} = image | |
@name = image.src | |
if pixelData? | |
@pixels = pixelData | |
else | |
canvas = document.createElement 'canvas' | |
context = canvas.getContext '2d' | |
canvas.width = @width | |
canvas.height = @height | |
context.drawImage context, 0, 0 | |
imageData = canvas.getImageData 0, 0, @width, @height | |
@pixels = [] | |
for y in [0..width] | |
for x in [0..height] | |
@pixels[x] or= [] | |
@pixels[x][y] = | |
r: imageData.data[x * y] | |
g: imageData.data[x * y + 1] | |
b: imageData.data[x * y + 2] | |
a: imageData.data[x * y + 3] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment