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

Do we really want to clip the sourceRect of createImageBitmap to the input's dimensions? #6306

Closed
Kaiido opened this issue Jan 18, 2021 · 10 comments
Labels
interop Implementations are not interoperable with each other topic: canvas

Comments

@Kaiido
Copy link
Member

Kaiido commented Jan 18, 2021

#1546 introduced a normative change that made the source rectangle clipped to the image's dimensions (#1546 (comment)).

When this change was introduced it was described as being "consistent with drawImage", however as a web-author I would like to dispute this claim.

If I try to make a parallel between the use of drawImage and the clipping of createImageBitmap I would use the sx, sy, sw, sh parameters of createImageBitmap to produce the clipped source like the same parameters are being used in the 9-length version of drawImage, then use the 5-length version to resize this cropped ImageBitmap.

// using the 9-length version directly
imgCanvas.getContext( "2d" ).drawImage( imgEl, sx, sy, sw, sh, dx, dy, dw, dh );
// using createImageBitmap's cropping + 5-length drawImage
const bmp = await createImageBitmap( imgEl, sx, sy, sw, sh );
bmpCanvas.getContext( "2d" ).drawImage( bmp, dx, dy, dw, dh );
// [Blink only] use resizeWidth and resizeHeight
const bmpR = await createImageBitmap( imgEl, sx, sy, sw, sh, { resizeWidth: dw, resizeHeight: dh } );
bmpResizeCanvas.getContext( "2d" ).drawImage( bmpR, dx, dy );

And doing so, we end up with very different results, as demonstrated in this fiddle:

  • If ran in Blink, which does implement the current specified behavior, both results using createImageBitmap are stretched on the x-axis because both the call to drawImage(bmp, dx, dy, dw, dh) and the resizeWidth and resizeHeight options assumed the input would keep the square aspect-ratio defined by sw and sh.
  • In Firefox, where this change hasn't made it yet, (I opened https://bugzilla.mozilla.org/show_bug.cgi?id=1687216), both working results are the same, which is more like what I'd call a "consistent" result. (The third result is different because this browser doesn't support the resizeWidth and resizeHeight options).

So I'd like to gather thoughts on this behavior, which in my own opinion is particularly surprising.

Also, I'll take the chance to note that this "Clip x to y's dimensions" doesn't seem to be linked to any definition an implementer should follow. I am myself in the process of writing a js monkey-patch for this method and I have to assume that for instance when the sourceRectangle is completely outside the input's bounds we have to return a transparent ImageBitmap with the dimensions from sourceRectangle, only because I saw it's what Blink does when I was writing a (now aborted) patch there.

Ps: Related WPT tests PR.

cc @jakearchibald

@jakearchibald
Copy link
Contributor

I think the confusing detail is from https://html.spec.whatwg.org/multipage/canvas.html#the-canvas-element

When the source rectangle is outside the source image, the source rectangle must be clipped to the source image and the destination rectangle must be clipped in the same proportion.

It seems like createImageBitmap doesn't apply the second part of this, where the destination rectangle is clipped in the same proportion.

Feels like the drawImage line would be less confusing if it was just "pixels outside the source image bounds are transparent black", but I can't figure out if that might produce an observable difference.

@Kaiido
Copy link
Member Author

Kaiido commented Jan 19, 2021

Regarding drawImage's clipping, (may be worth its own ticket?)

The only place I could find where changing the drawImage clipping behavior would make a difference is when using the "clear" composite mode, currently only available in Blink: https://jsfiddle.net/m9zL17gq/

There, even transparent pixels are used for the compositing (at least in current implementation, I'm not a specialist of compositing and I did not try to understand the specs there).

I am not sure what would be web-authors' expectations here though.

And while maybe less important, one benefit of clipping here is that implementations can early exit in case of empty sourceRect (e.g in Blink), but I'm not sure the specs should enforce the clipping just for that, maybe simply allowing it would be enough, but forcing transparent pixels may also cause some useless overhead.


Back to createImageBitmap

So we agree the current behavior is unexpected? I believe removing this step is quite straightforward, only Blink has implemented it and I already have a patch ready for them if needed.

@annevk annevk added the interop Implementations are not interoperable with each other label Jan 19, 2021
@annevk
Copy link
Member

annevk commented Jan 19, 2021

cc @whatwg/canvas

@jakearchibald
Copy link
Contributor

So we agree the current behavior is unexpected?

I think so, yeah.

I believe removing this step is quite straightforward, only Blink has implemented it and I already have a patch ready for them if needed.

Seems fair. The test should be changed too.

@junov
Copy link
Member

junov commented Jan 19, 2021

Feels like the drawImage line would be less confusing if it was just "pixels outside the source image bounds are transparent black", but I can't figure out if that might produce an observable difference.

The reason that the rectangles are clipped rather than filling with transparent black is so that drawImage will render a rectangle with sharp edges. Filling with transparent black would result, in some cases, in an edge that is blurry (when imageSmoothingEnabled is set to true).

I know this because I've had to fix that bug a long time ago. If I recall correctly, a web developer who was rendering content tile by tile reported a bug that there were seams in the rendered output. So this little detail actually matters.

@kdashg
Copy link

kdashg commented Jan 20, 2021

For e.g. createImageBitmap([3x3 image], 2, 2, 2, 1) I expect it to give me a 2x1 ImageBitmap with the left pixel being the src's bottom-right pixel, and right pixel transparent black.
I think it's important for devs to explicitly opt-in to clipping/content-dependent-resizing, and that it's a good invariant that you get a result of the same size that you requested.

There's some divergence here between partial-pixel-targetable drawImage and working with whole-integer-pixels like for ImageBitmap, which is that drawImage expects to be able to filter non-integer-scale resizes smoothly, and there's no equivalent solution for ImageBitmap except rounding, which I think is fraught to do behind the dev's back.

Ideally we spec the most "obvious" default behavior, and enable users to do more sophisticated things themselves.

Kaiido added a commit to Kaiido/wpt that referenced this issue Jan 31, 2021
As per whatwg/html#6306 it is finally the expected behavior.
Kaiido added a commit to Kaiido/html that referenced this issue Jan 31, 2021
It has been found in whatwg#6306 that this was an oversight at the time of its introduction.
Current behavior goes against web-authors expectations and no implementers has opposed against the change to "no-clip".
Kaiido added a commit to Kaiido/html that referenced this issue Jan 31, 2021
It has been found in whatwg#6306 that this was an oversight at the time of its introduction.
Current behavior goes against web-authors expectations and no implementers has opposed against the change to "no-clip".
@fserb
Copy link
Contributor

fserb commented Feb 2, 2021

Was there some discussion regarding if this breaks current content?

@kdashg
Copy link

kdashg commented Feb 2, 2021

When content is currently broken in at least one browser, we have pretty strong latitude to standardize it however we think is best. Put another way, content will be broken whatever direction we choose.

@fserb
Copy link
Contributor

fserb commented Feb 2, 2021

ahn, okey. I didn't notice that. My bad. Yeah, if that's the case, I agree with you.

@junov
Copy link
Member

junov commented Feb 2, 2021 via email

@annevk annevk closed this as completed in b43cdf4 Feb 4, 2021
imhele added a commit to imhele/html that referenced this issue Feb 18, 2021
* Editorial: remove redundant "the"

* Meta: default branch rename

Also correct a broken link. Not even w3.org URLs are that cool.

Helps with whatwg/meta#174.

* Editorial: clean up calls to "parse a URL"

It actually takes a string, so calls should be clear about that.

* Review Draft Publication: January 2021

* Simplify <link>s

In particular, remove their activation behavior, stop them from matching
:link and :visited, and stop suggesting that they be focusable areas.

This also includes a slight expansion and rearrangement of the link
element's section to make it clearer what hyperlinks created by <link>
are meant for, contrasting them to <a> and <area> hyperlinks.

Closes whatwg#4831. Closes whatwg#2617. Helps with whatwg#5490.

* Meta: remove demos/offline/* (whatwg#6307)

These are no longer needed as of e4330d5.

* Meta: minor references cleanup

Use more HTTPS and drop obsolete HTML Differences reference.

* Editorial: anticlockwise → counterclockwise

We use en-US these days. Spotted in https://twitter.com/iso2022jp/status/1352601086519955456.

* Use :focus-visible in the UA stylesheet

See w3c/csswg-drafts#4278.

* Editorial: align with WebIDL and Infra

* Fix "update a style block" early return

The new version matches implementation reality and CSSWG resolution.

The algorithm was also inconsistent, as it looked at whether
the element was in a shadow tree or in the document tree, but it was
only specified to be re-run if the element becomes connected or
disconnected.

The CSSWG discussed this in
w3c/csswg-drafts#3096 (comment)
and http://wpt.live/shadow-dom/ShadowRoot-interface.html tests this.

This also matches closer the definition of <link rel="stylesheet">,
which does use connectedness (though it uses "browsing-context
connected", which is a bit different):
https://html.spec.whatwg.org/#link-type-stylesheet

* Modernize and refactor simple dialogs

This contains a small bug fix, in that confirm() and prompt() said
"return" in some cases instead of "return false" or "return null" as
appropriate.

Other notable changes, all editorial, are:

* Factoring out repeated "cannot show simple dialogs" steps, which will
  likely expand over time (see e.g. whatwg#6297).
* Separating out and explaining the no-argument overload of alert().
* Passing the document through to the "printing steps", instead of just
  having them talk about "this Window object".

* Meta: add definition markup for MessageEvent

* Remove <marquee> events

They are only supported by one engine (Gecko).

Closes whatwg#2957.

* Clarify when microtasks happen

* Ignore COEP on non-secure contexts

Fixes whatwg#6328.

* Editorial: update URL Standard integration

* Editorial: only invoke response's location URL once

Complements whatwg/fetch#1149.

* Track the incumbent settings and active script in Promise callbacks

Closes whatwg#5213.

* createImageBitmap(): stop clipping sourceRect to source's dimensions

It has been found in whatwg#6306 that this was an oversight at the time of its introduction. Current behavior goes against author expectations and no implementer has opposed the change to "no-clip".

Tests: web-platform-tests/wpt#27040.

Closes whatwg#6306.

* Remove CSP plugin-types blocking

With Flash not being supported anymore, the CSP directive plugin-types has lost its main reason for being and is being removed from the Content Security Policy specification: w3c/webappsec-csp#456.

This change removes references to the relevant algorithm from the Content Security Policy spec.

* Meta: set more dfn types

A follow-up to:

* whatwg#5694
* whatwg#5916

* Editorial: occuring → occurring

* Make all plugin-related APIs no-ops

Part of whatwg#6003.

* Disallow simple dialogs from different-origin domain iframes

Closes whatwg#5407.

* Revive @@iterator for PluginArray/MimeTypeArray/Plugin

@@iterator is implicitly installed by defining an indexed property getter. Since there is no other way to define it exclusively, this restores some methods back to being indexed getters.

This fixes an inadvertent observable behavior change in d4f07b8.

* Adjust web+ scheme security considerations to account for FTP removal

Also, network scheme is now reduced to HTTP(S) scheme.

Helps with whatwg#5375, but form submission issue remains.

See whatwg/fetch#1166 for context.

* Meta: export pause

Nobody but XMLHttpRequest take a dependency on this please. You have been warned.

Context: whatwg/xhr#311.

* Fix typo: ancestor → accessor

Fixes whatwg#6374.

Co-authored-by: Dominic Farolino <[email protected]>
Co-authored-by: Anne van Kesteren <[email protected]>
Co-authored-by: Domenic Denicola <[email protected]>
Co-authored-by: Emilio Cobos Álvarez <[email protected]>
Co-authored-by: Momdo Nakamura <[email protected]>
Co-authored-by: Jake Archibald <[email protected]>
Co-authored-by: Yutaka Hirano <[email protected]>
Co-authored-by: Shu-yu Guo <[email protected]>
Co-authored-by: Kaiido <[email protected]>
Co-authored-by: Antonio Sartori <[email protected]>
Co-authored-by: Michael[tm] Smith <[email protected]>
Co-authored-by: Ikko Ashimine <[email protected]>
Co-authored-by: Carlos IL <[email protected]>
Co-authored-by: Kagami Sascha Rosylight <[email protected]>
Co-authored-by: Simon Pieters <[email protected]>
blueboxd pushed a commit to blueboxd/chromium-legacy that referenced this issue Apr 16, 2021
As per whatwg/html#6306 it has been determined that the clipping of the sourceRect should not happen for createImageBitmap.

This CL thus replaces the uses of the IntersectionRect with the source rect determined through the parsed_options.crop_rect.

Bug: chromium:1162598
Change-Id: Iadf79699ad786cdaeb7e9b0fbcb94624d2942068
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2661436
Commit-Queue: Juanmi Huertas <[email protected]>
Reviewed-by: Juanmi Huertas <[email protected]>
Reviewed-by: Justin Novosad <[email protected]>
Cr-Commit-Position: refs/heads/master@{#873301}
mjfroman pushed a commit to mjfroman/moz-libwebrtc-third-party that referenced this issue Oct 14, 2022
As per whatwg/html#6306 it has been determined that the clipping of the sourceRect should not happen for createImageBitmap.

This CL thus replaces the uses of the IntersectionRect with the source rect determined through the parsed_options.crop_rect.

Bug: chromium:1162598
Change-Id: Iadf79699ad786cdaeb7e9b0fbcb94624d2942068
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2661436
Commit-Queue: Juanmi Huertas <[email protected]>
Reviewed-by: Juanmi Huertas <[email protected]>
Reviewed-by: Justin Novosad <[email protected]>
Cr-Commit-Position: refs/heads/master@{#873301}
GitOrigin-RevId: 88434d80a2e6ef4ae9850740d04ba138c94c8cb1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interop Implementations are not interoperable with each other topic: canvas
Development

No branches or pull requests

6 participants