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

Simplify sprite image loading #3514

Merged
merged 2 commits into from
Nov 3, 2016
Merged

Simplify sprite image loading #3514

merged 2 commits into from
Nov 3, 2016

Conversation

mourner
Copy link
Member

@mourner mourner commented Nov 2, 2016

👀 @lucaswoj

const canvas = window.document.createElement('canvas');
const context = canvas.getContext('2d');
canvas.width = img.width;
canvas.height = img.height;
context.drawImage(img, 0, 0);
return context.getImageData(0, 0, img.width, img.height).data;
img.data = context.getImageData(0, 0, img.width, img.height).data;
Copy link
Contributor

Choose a reason for hiding this comment

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

img is an instance of HTMLImageElement which does not have a data property. It might be better to return a vanilla object with width, height, data, and complete properties.

Copy link
Member Author

Choose a reason for hiding this comment

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

Not does it have getData() method it was augmented with earlier. There are some places in the code that expect getImage to return the image object rather than underlying data, such as ImageSource. On the other hand, moving canvas-based getData code to ImageSprite will make stubbing in Node more difficult because you have to stub its methods rather than ajax.getImage. So I suggest leaving this as is for now.

Copy link
Member Author

Choose a reason for hiding this comment

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

On a second thought, we can make this much cleaner by introducing a separate browser.getImageData utility function and stubbing it out.

};
if (!sameOrigin(url)) {
img.crossOrigin = "Anonymous";
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

On the other hand, this example https://developer.mozilla.org/ru/docs/Web/HTML/CORS_enabled_image uses capitalized version. Video code below (unchanged) used capitalized version too.

@mourner
Copy link
Member Author

mourner commented Nov 2, 2016

@lucaswoj added another commit that makes handling/stubbing of image data much cleaner.

Copy link
Contributor

@lucaswoj lucaswoj 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! Sorry about the merge conflict. :shipit:

@mourner mourner merged commit 19cd42f into master Nov 3, 2016
@mourner mourner deleted the cleanup-sprite branch November 3, 2016 19:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants