Commit b56085b6 authored by Ryan LeFevre's avatar Ryan LeFevre

Fix toPng so that it's no longer asynchronous #19

parent 243439e9
...@@ -16778,17 +16778,13 @@ module.exports = { ...@@ -16778,17 +16778,13 @@ module.exports = {
return canvas.toDataURL('image/png'); return canvas.toDataURL('image/png');
}, },
toPng: function() { toPng: function() {
return new RSVP.Promise((function(_this) { var dataUrl, image;
return function(resolve, reject) { dataUrl = this.toBase64();
var dataUrl, image; image = new Image();
dataUrl = _this.toBase64(); image.width = this.width();
image = new Image(); image.height = this.height();
image.width = _this.width(); image.src = dataUrl;
image.height = _this.height(); return image;
image.src = dataUrl;
return resolve(image);
};
})(this));
}, },
saveAsPng: function() { saveAsPng: function() {
throw "Not available in the browser. Use toPng() instead."; throw "Not available in the browser. Use toPng() instead.";
......
...@@ -58,12 +58,10 @@ ...@@ -58,12 +58,10 @@
PSD.fromEvent(e).then(function (psd) { PSD.fromEvent(e).then(function (psd) {
var data = JSON.stringify(psd.tree().export(), undefined, 2); var data = JSON.stringify(psd.tree().export(), undefined, 2);
document.getElementById('data').innerHTML = data; document.getElementById('data').innerHTML = data;
psd.image.toPng().then(function (image) { document.getElementById('image').appendChild(psd.image.toPng());
document.getElementById('image').appendChild(image);
});
}); });
} }
}()); }());
</script> </script>
</body> </body>
</html> </html>
\ No newline at end of file
...@@ -4,16 +4,13 @@ RSVP = require 'rsvp' ...@@ -4,16 +4,13 @@ RSVP = require 'rsvp'
module.exports = module.exports =
toPng: -> toPng: ->
new RSVP.Promise (resolve, reject) => png = new PNG(filterType: 4, width: @width(), height: @height())
png = new PNG(filterType: 4, width: @width(), height: @height()) png.data = @pixelData
png
png.data = @pixelData
resolve(png)
saveAsPng: (output) -> saveAsPng: (output) ->
new RSVP.Promise (resolve, reject) => new RSVP.Promise (resolve, reject) =>
@toPng().then (image) -> @toPng()
image .pack()
.pack() .pipe(fs.createWriteStream(output))
.pipe(fs.createWriteStream(output)) .on 'finish', resolve
.on 'finish', resolve
{ {
"name": "psd", "name": "psd",
"description": "A general purpose Photoshop file parser.", "description": "A general purpose Photoshop file parser.",
"version": "1.1.0", "version": "2.0.0",
"main": "./index.js", "main": "./index.js",
"keywords": ["psd", "parser", "photoshop", "adobe", "reader"], "keywords": ["psd", "parser", "photoshop", "adobe", "reader"],
"repository": { "repository": {
......
...@@ -18,16 +18,15 @@ module.exports = ...@@ -18,16 +18,15 @@ module.exports =
canvas.toDataURL 'image/png' canvas.toDataURL 'image/png'
toPng: -> toPng: ->
new RSVP.Promise (resolve, reject) => dataUrl = @toBase64()
dataUrl = @toBase64() # Create the image and set the source to the
# Create the image and set the source to the # canvas data URL.
# canvas data URL. image = new Image()
image = new Image() image.width = @width()
image.width = @width() image.height = @height()
image.height = @height() image.src = dataUrl
image.src = dataUrl
resolve(image) image
saveAsPng: -> saveAsPng: ->
throw "Not available in the browser. Use toPng() instead." throw "Not available in the browser. Use toPng() instead."
\ No newline at end of file
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