Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support canvas v2 #81

Merged
merged 4 commits into from
Mar 16, 2020
Merged

Conversation

tjenkinson
Copy link
Contributor

In canvas v2 Image is not on Canvas.
See https://github.com/Automattic/node-canvas/blob/master/CHANGELOG.md#200

This adds a Image to the options, and also throws a specific error if Image should probably be provided.

@tjenkinson tjenkinson marked this pull request as ready for review January 13, 2020 15:09
Comment on lines +108 to +109
Canvas: Canvas,
Image: Image

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since this limits the usability to only canvas v2 (since it's a dev, not full dependency, and the consumer would have to provide it, i think it'd be better to document how canvas v2 could be used on the consumer's end instead of forcing it through this route.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not quite following. merge-images would still work with canvas v1. In the v1 case the Image property would just be omitted.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, you're right, I was misreading this as the source, not the markdown. I was thinking something similar but just doing doc for it. Sorry for my misread

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah cool :)

@lukechilds
Copy link
Owner

@tjenkinson thank you so much for this! 🙏

A very simple change but I just haven't had the chance to take a look and see what needed changing since v2 was released and #23 was broken.

Copy link
Owner

@lukechilds lukechilds left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

We can drop support for canvas@1 to simplify the code a bit, then this is good to be merged.

src/index.js Outdated Show resolved Hide resolved
src/index.js Outdated Show resolved Hide resolved
src/index.js Outdated Show resolved Hide resolved
canvas.toDataURL(options.format, {
quality: options.quality,
progressive: false
}, (err, jpeg) => {
if (err) {
throw err;
reject(err);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How come this is changed to reject instead of throw?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the error was being thrown from an asynchronous function, meaning it would not result in the promise being rejected.

The function provided to Promise will reject automatically if it catches an exception, but it won't catch it from something asynchronous.

@@ -9,5 +9,5 @@ test('mergeImages rejects Promise if node-canvas instance isn\'t passed in', asy

test('mergeImages rejects Promise if image load errors', async t => {
t.plan(1);
await t.throws(mergeImages([1], { Canvas }));
await t.throws(mergeImages(['nothing-here.jpg'], { Canvas, Image }));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How come this is changed to 'nothing-here.jpg' instead of 1?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this it now fails with

  errors › mergeImages rejects Promise if image load errors

  /Users/tomjenkinson/Documents/GitHub/merge-images/node_modules/nyc/node_modules/signal-exit/index.js:155

  Promise returned by test never resolved

I investigated a bit further and the reason is that Image doesn't call onerror (or onload) when it's given a number. Chrome does call onerror, so this probably should be a bug report on Canvas

@lukechilds lukechilds merged commit 23c1a80 into lukechilds:master Mar 16, 2020
lukechilds added a commit that referenced this pull request Mar 16, 2020
@lukechilds
Copy link
Owner

Thanks for this @tjenkinson!

Published as [email protected] because it's a breaking change for Node.js users.

Browser users are unaffected though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants