Giter Club home page Giter Club logo

Comments (2)

Kaiido avatar Kaiido commented on July 21, 2024

cc @whatwg/canvas

FWIW, there is no such option as { alpha: false, premultiplyAlpha: "none" } on createElement("canvas"), you probably got confused with getContext(type, option) though premultipliedAlpha is only a valid option for WebGL and not for bitmaprenderer, which only has alpha.
Still I guess there is indeed something that needs to be handled here to attain interop.

from html.

hybridherbst avatar hybridherbst commented on July 21, 2024

Sorry for the confusion, I meant it's not clear if premultiplyAlpha: "none" on createImageBitmap is to be respected when the data is read back. It is respected on Chrome (which I think is correct) but it's not on WebKit/Firefox.

To avoid more confusion, here's the code from the reproduction page linked above:

      // fetch an image with alpha channel and put it on a canvas with transferFromImageBitmap
      const url = "https://cdn.glitch.global/073b6ba0-42e6-4706-9e9d-9dfacb8f3a8b/TransparentCat-repro.png?v=1708264432674";
      const imageBitmap = await fetch(url)
        .then(response => response.blob())
        // this makes the difference on Chrome, but does not affect the result on WebKit/Firefox
        .then(blob => createImageBitmap(blob, { premultiplyAlpha: "none" }))
        .then(imageBitmap => imageBitmap);

      // create a canvas
      const canvas = document.createElement("canvas");
      // setting width/height is probably not needed as the transferred image takes over
      canvas.width = imageBitmap.width; 
      canvas.height = imageBitmap.height;
      // alpha parameter here does not influence the readback result, but does influence how the canvas is displayed on the page (seems to always be premultiplied, but with or without black background)
      const ctx = canvas.getContext("bitmaprenderer", { alpha: false });
      ctx.transferFromImageBitmap(imageBitmap);
      
      // turn canvas to PNG and download it
      const a2 = document.createElement("a");
      a2.innerText = "Download readback";
      // this should contain unpremultiplied data โ€“ approx. same RGBA data as in the original
      a2.href = canvas.toDataURL("image/png");

which results in these RGB channels in the readback images:

Options Chrome Firefox WebKit
createImageBitmap(blob, { premultiplyAlpha: "none" }) image-rgb-chrome image-rgb-firefox image-rgb-safari
createImageBitmap(blob, { premultiplyAlpha: "default" }) image-rgb-chrome-default image-rgb-firefox-default image-rgb-safari-default

Notes:

  • Chrome gets it right (I believe)
  • the option doesn't do anything in Safari
  • Firefox double-unpremultiplies instead of treating the data correctly (I believe)
  • For some reason, even the "default" result is different in Firefox (brighter)

from html.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.