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

Fixes CanvasPixelArray set polyfill (#4974) #4997

Merged
merged 1 commit into from
Jun 24, 2014

Conversation

CodingFabian
Copy link
Contributor

what this PR does to fix #4974 is to inspect the actual image data returned from the canvas context.
If the browser supports Uint8ClampedArray and the image data is of that type we are all set and can return.

if not, we need to check if there might be a set function available. if that is not the case, we wrap the createImageData function and add a set function to it.
As far as I could figure out it is not possible to add it to the CanvasPixelArray prototype used by Chrome 17.

if (window.CanvasPixelArray) {
if (typeof window.CanvasPixelArray.prototype.set !== 'function') {
window.CanvasPixelArray.prototype.set = function(arr) {
var ctx = document.createElement('canvas').getContext('2d');
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm just wondering if we can avoid to creating a canvas element for browsers that has no such problem to reduce startup time

Copy link
Contributor Author

Choose a reason for hiding this comment

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

unfortunately there is no easy way to know what type the data array will have without creating it. What we could do is assume that browsers who support Uint8ClampedArray will use that for backing the canvas. But I do not know if that holds true.

Copy link
Contributor

Choose a reason for hiding this comment

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

Let use userAgent string to not bother to check for Firefox, Opera, Safari and newer versions of Chrome and IE

Copy link
Contributor

Choose a reason for hiding this comment

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

Could you also time it? Just for our information

Copy link
Contributor Author

Choose a reason for hiding this comment

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

would be possible, but for which versions?
basically all which not use Uint8ClampedArray right?
http://caniuse.com/typedarrays doesnt really help.

Copy link
Contributor

Choose a reason for hiding this comment

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

good question, so far we had problem with ie and chrome (probably androids browser) only

Copy link
Contributor Author

Choose a reason for hiding this comment

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

just tested IE. <11 doesn't have set method.
i tested a few chrome verions (glad there is chromium portable(, and support for the set method was added in chrome 21.
Ee can do a user agent check and polyfill it with like i proposed.

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 am downloading a few old chrome versions to test. Maybe you could check firefox?
document.createElement('canvas').getContext('2d').createImageData(1,1).data.set
should not return undefined :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

its undefined in firefox < 4 - I will add a polyfill for that as well, tell me if I do not need to do that :)

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think pdf.js works or tested for firefox < ~17

@CodingFabian
Copy link
Contributor Author

@yurydelendik i added now the polyfill based on user agents for IE < 11 and chrom(e|ium) < 21. Not really beautiful but the only way to avoid creating an instance.

// feature-detectable, we have to rely on user agents:
// IE<11 contains MSIE - starting from 11 it no longer does
var polyfill = false;
if (navigator.userAgent.indexOf('MSIE')) {
Copy link
Contributor

Choose a reason for hiding this comment

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

did you mean .indexOf(...) >= 0 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

well result is the same, but i can add it.

@yurydelendik
Copy link
Contributor

Look fine to me after comments address. Can somebody else review this as well?

@CodingFabian
Copy link
Contributor Author

ouch - i just thought: hey why is it working in IE10 with the old polyfill?
I realized that the old polyfill (on window.CanvasPixelArray) works in IE - I am going to update the PR and provide both polyfills

@@ -492,6 +492,25 @@ if (typeof PDFJS === 'undefined') {
}
};
}
} else {
// Chrome < 21 uses an inaccessible CanvasPixelArray prototype.
// be cause we cannot feature detect it, we rely on user agent.
Copy link
Collaborator

Choose a reason for hiding this comment

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

A small nit: Please use upper-case letters at the beginning of sentences; it makes the text in the comment much easier to read! :-)

Edit: Also be cause should be one word -> Because.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this PR is horrible, so many amends, and I cannot hide them :-(

@yurydelendik
Copy link
Contributor

/botio-linux preview

@pdfjsbot
Copy link

From: Bot.io (Linux)


Received

Command cmd_preview from @yurydelendik received. Current queue size: 1

Live output at: http://107.21.233.14:8877/c1e36e06a178552/output.txt

@yurydelendik
Copy link
Contributor

/botio-linux preview

@pdfjsbot
Copy link

From: Bot.io (Linux)


Received

Command cmd_preview from @yurydelendik received. Current queue size: 0

Live output at: http://107.21.233.14:8877/0d3850a29c5408c/output.txt

yurydelendik added a commit that referenced this pull request Jun 24, 2014
Fixes CanvasPixelArray set polyfill (#4974)
@yurydelendik yurydelendik merged commit b482393 into mozilla:master Jun 24, 2014
@yurydelendik
Copy link
Contributor

Thank you

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.

Chrome v17 CanvasPixelArray issue
4 participants