fregante/image-promise

Support CORS enabled images

Closed this issue · 4 comments

Hey! Great library! One thing I noticed is missing is support for CORS enabled images, basically if you load and use an external image in a canvas, you can't use .getImageData() on the canvas anymore, to resolve this you have to do img.crossOrigin = 'Anonymous' before attaching the src attribute to the image.

Here is an example of how another library does it.

There are two ways of approaching it:

  1. we put image.crossOrigin = 'Anonymous' directly into the code and the user doesn't have to worry about it
  2. we provide the options as a second argument, so it's up to the user to enable it

I would prefer option 1. Let me know what you think! So I can make a PR of it

As much as I'd like it to be a default, this might not be desirable. Can you add it as a second attributes object parameter?

Then in the code something like:

  const src = image;
  image = new Image();
+ Object.keys(attributes || {}).forEach(
+ 	name => image.setAttribute(name, attributes[name])
+ );
  image.src = src;

Also the TypeScript definitions and tests would have to be updated.

Great idea! So it is completely configurable.

Alright done!