Skip to content

Instantly share code, notes, and snippets.

@frostney
Created December 27, 2013 01:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frostney/8141002 to your computer and use it in GitHub Desktop.
Save frostney/8141002 to your computer and use it in GitHub Desktop.
TextureManager proof-of-concept with Q written in CoffeeScript
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