如何使 Firefox 的 canvas 的行为与基于 Chromium 的浏览器相同?

How to make Firefox's canvas behave the same as in Chromium-based browsers?

在基于 Chromium 的浏览器中(我使用的是 brave),canvas 以这种方式呈现:

但在 Firefox 中,出于某种原因,它是这样呈现的:

这是我使用的方法:

context.drawImage(image, 100, 100, 100, 400);

我没有在 canvas 元素上使用任何 CSS,只是这样:

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
}

然后调整大小并附加 canvas:

const canvas = document.createElement("canvas");
document.body.appendChild(canvas);

export const graphics = canvas.getContext("2d");

const adaptResolution = () => {
  canvas.width = innerWidth;
  canvas.height = innerHeight;
};

adaptResolution();
addEventListener("resize", adaptResolution);

我能否使 Firefox canvas 的行为与基于 Chromium 的浏览器相同?是什么导致了这种差异,我该如何解决?


这是我要在屏幕上呈现的 SVG:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->

<svg
   width="768"
   height="576"
   viewBox="0 0 203.2 152.4"
   version="1.1"
   id="svg5"
   inkscape:version="1.1 (c68e22c387, 2021-05-23)"
   sodipodi:docname="box.svg"
   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
   xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
   xmlns="http://www.w3.org/2000/svg"
   xmlns:svg="http://www.w3.org/2000/svg">
  <sodipodi:namedview
     id="namedview7"
     pagecolor="#ffffff"
     bordercolor="#666666"
     borderopacity="1.0"
     inkscape:pageshadow="2"
     inkscape:pageopacity="0.0"
     inkscape:pagecheckerboard="0"
     inkscape:document-units="px"
     showgrid="false"
     units="px"
     inkscape:zoom="0.38752104"
     inkscape:cx="107.09096"
     inkscape:cy="187.08661"
     inkscape:window-width="1920"
     inkscape:window-height="1016"
     inkscape:window-x="0"
     inkscape:window-y="27"
     inkscape:window-maximized="1"
     inkscape:current-layer="layer1" />
  <defs
     id="defs2" />
  <g
     inkscape:label="Layer 1"
     inkscape:groupmode="layer"
     id="layer1">
    <rect
       style="fill:#000000;stroke-width:0.387859"
       id="rect31"
       width="206.87587"
       height="153.6207"
       x="-1.7069"
       y="-1.7068989" />
  </g>
</svg>

这是一个互操作问题,我已经在 the specs 上报告过。
不幸的是,它还没有得到太多关注...

问题是目前尚不清楚应该对哪个“图像”drawImage 的调整大小选项起作用。 Chrome 认为它应该以默认大小处理输入 svg 并对其进行拉伸,Firefox 认为调整大小选项是输出框,并将调整 svg 内容以适应此框。

要在 Firefox 中获得 Chrome 的行为,您可以将 preserveAspectRatio="none" 属性添加到 SVG 图像的根目录:

const svg = new Blob([`<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->

<svg
   preserveAspectRatio="none"
   width="768"
   height="576"
   viewBox="0 0 203.2 152.4"
   version="1.1"
   id="svg5"
   inkscape:version="1.1 (c68e22c387, 2021-05-23)"
   sodipodi:docname="box.svg"
   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
   xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
   xmlns="http://www.w3.org/2000/svg"
   xmlns:svg="http://www.w3.org/2000/svg">
  <sodipodi:namedview
     id="namedview7"
     pagecolor="#ffffff"
     bordercolor="#666666"
     borderopacity="1.0"
     inkscape:pageshadow="2"
     inkscape:pageopacity="0.0"
     inkscape:pagecheckerboard="0"
     inkscape:document-units="px"
     showgrid="false"
     units="px"
     inkscape:zoom="0.38752104"
     inkscape:cx="107.09096"
     inkscape:cy="187.08661"
     inkscape:window-width="1920"
     inkscape:window-height="1016"
     inkscape:window-x="0"
     inkscape:window-y="27"
     inkscape:window-maximized="1"
     inkscape:current-layer="layer1" />
  <defs
     id="defs2" />
  <g
     inkscape:label="Layer 1"
     inkscape:groupmode="layer"
     id="layer1">
    <rect
       style="fill:#000000;stroke-width:0.387859"
       id="rect31"
       width="206.87587"
       height="153.6207"
       x="-1.7069"
       y="-1.7068989" />
  </g>
</svg>`], { type:"image/svg+xml"});
const img = new Image();
img.src = URL.createObjectURL(svg);
img.onload = e => document.querySelector("canvas").getContext("2d").drawImage(img, 0, 0, 100, 400);
<canvas width="500" height="500"></canvas>