8000 Do we really want to clip the sourceRect of createImageBitmap to the input's dimensions? · Issue #6306 · whatwg/html · GitHub
[go: up one dir, main page]

Skip to content

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

@Kaiido

Description

@Kaiido

#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

Metadata

Metadata

Assignees

No one assigned

    Labels

    interopImplementations are not interoperable with each othertopic: canvas

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0