Skip to content

Commit

Permalink
Fix toPng so that it's no longer asynchronous meltingice#19
Browse files Browse the repository at this point in the history
  • Loading branch information
meltingice committed Feb 26, 2015
1 parent 243439e commit b56085b
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 36 deletions.
18 changes: 7 additions & 11 deletions dist/psd.js
Original file line number Diff line number Diff line change
Expand Up @@ -16778,17 +16778,13 @@ module.exports = {
return canvas.toDataURL('image/png');
},
toPng: function() {
return new RSVP.Promise((function(_this) {
return function(resolve, reject) {
var dataUrl, image;
dataUrl = _this.toBase64();
image = new Image();
image.width = _this.width();
image.height = _this.height();
image.src = dataUrl;
return resolve(image);
};
})(this));
var dataUrl, image;
dataUrl = this.toBase64();
image = new Image();
image.width = this.width();
image.height = this.height();
image.src = dataUrl;
return image;
},
saveAsPng: function() {
throw "Not available in the browser. Use toPng() instead.";
Expand Down
6 changes: 2 additions & 4 deletions examples/browser/image.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,10 @@
PSD.fromEvent(e).then(function (psd) {
var data = JSON.stringify(psd.tree().export(), undefined, 2);
document.getElementById('data').innerHTML = data;
psd.image.toPng().then(function (image) {
document.getElementById('image').appendChild(image);
});
document.getElementById('image').appendChild(psd.image.toPng());
});
}
}());
</script>
</body>
</html>
</html>
17 changes: 7 additions & 10 deletions lib/psd/image_exports/png.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@ RSVP = require 'rsvp'

module.exports =
toPng: ->
new RSVP.Promise (resolve, reject) =>
png = new PNG(filterType: 4, width: @width(), height: @height())

png.data = @pixelData
resolve(png)
png = new PNG(filterType: 4, width: @width(), height: @height())
png.data = @pixelData
png

saveAsPng: (output) ->
new RSVP.Promise (resolve, reject) =>
@toPng().then (image) ->
image
.pack()
.pipe(fs.createWriteStream(output))
.on 'finish', resolve
@toPng()
.pack()
.pipe(fs.createWriteStream(output))
.on 'finish', resolve
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "psd",
"description": "A general purpose Photoshop file parser.",
"version": "1.1.0",
"version": "2.0.0",
"main": "./index.js",
"keywords": ["psd", "parser", "photoshop", "adobe", "reader"],
"repository": {
Expand Down
19 changes: 9 additions & 10 deletions shims/png.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,15 @@ module.exports =
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 = dataUrl
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 = dataUrl

resolve(image)
image

saveAsPng: ->
throw "Not available in the browser. Use toPng() instead."
throw "Not available in the browser. Use toPng() instead."

0 comments on commit b56085b

Please sign in to comment.