Commit cb61b9ee authored by Dan Hough's avatar Dan Hough

Added #toBase64 and #fromDroppedFile:

- #toBase64 is for situations where you want to control what happens to the URL. Maybe you want to upload it, rather than put it straight into an image - or maybe you want to use it as an image in a CSS background. At any rate, the code is exactly the same for toPng, but I put a lot of it into the toBase64 function
- #fromDroppedFile is very similar to fromEvent, only it assumes that you already have the file extracted from the drop event. This is useful e.g. with the Dropzone JS library, which exposes the file in an 'addedFile' event.
parent 7ec70e92
......@@ -28,3 +28,15 @@ module.exports =
reader.onerror = reject
reader.readAsArrayBuffer(file)
@fromDroppedFile = (file) ->
new RSVP.Promise (resolve, reject) ->
reader = new FileReader()
reader.onload = (e) ->
psd = new PSD(new Uint8Array(e.target.result))
psd.parse()
resolve(psd)
reader.onerror = reject
reader.readAsArrayBuffer(file)
RSVP = require 'rsvp'
module.exports =
toPng: ->
new RSVP.Promise (resolve, reject) =>
toBase64: ->
# Draw the pixels to the canvas
canvas = document.createElement('canvas')
canvas.width = @width()
......@@ -16,12 +15,17 @@ module.exports =
context.putImageData(imageData, 0, 0)
canvas.toDataURL 'image/png'
toPng: ->
new RSVP.Promise (resolve, reject) =>
dataUrl = @toBase64()
# Create the image and set the source to the
# canvas data URL.
image = new Image()
image.width = @width()
image.height = @height()
image.src = canvas.toDataURL 'image/png'
image.src = dataUrl
resolve(image)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment