Skip to content

Latest commit

 

History

History
31 lines (22 loc) · 766 Bytes

loader.coffee.md

File metadata and controls

31 lines (22 loc) · 766 Bytes

Loader

Loader = (I={}, self=Core(I)) ->
  self.extend
    load: (url) ->
      deferred = Deferred()

      canvas = document.createElement('canvas')
      context = canvas.getContext('2d')
      image = document.createElement("img")
      image.crossOrigin = true

      image.onload = ->
        {width, height} = image

        canvas.width = width
        canvas.height = height
        context.drawImage(image, 0, 0)
        imageData = context.getImageData(0, 0, width, height)

        deferred.resolve imageData

      image.onerror = ->
        deferred.reject "Error loading image data"

      image.src = url

      return deferred.promise()

module.exports = Loader