简体   繁体   English

Chrome 上的 JavaScript Canvas SVG 线性渐变渲染

[英]JavaScript Canvas SVG linear gradient rendering on Chrome

So the details of this question are a bit obtuse.所以这个问题的细节有点迟钝。 Here is what my team is trying to do... We are using Figma to create SVGs.这是我的团队正在尝试做的事情……我们正在使用 Figma 来创建 SVG。 These SVGs are relatively simplistic, but we are using them to create animations in the body of our application.这些 SVG 相对简单,但我们使用它们在应用程序主体中创建动画。 Our application is based around the HTML5 Canvas.我们的应用程序基于 HTML5 Canvas。 Once an image file is loaded it is drawn on the Canvas using the CanvasRenderingContext2D.drawImage() method.加载图像文件后,将使用CanvasRenderingContext2D.drawImage()方法将其绘制在 Canvas 上。 Here's the kicker.这是踢球者。 Some SVGs just don't appear.有些 SVG 只是没有出现。 Others do appear.其他人确实出现。 Some might appear partially.有些可能会部分出现。 Here is the issue that brings me to StackOverflow (though hopefully, I'll be able to provide an answer for it, shortly).这是将我带到 StackOverflow 的问题(尽管希望我很快能够为它提供答案)。 Some SVGs use a linear-gradient generated by Figma, but those linear gradients don't render at all in the JS Canvas.一些 SVG 使用 Figma 生成的线性渐变,但这些线性渐变根本不会在 JS Canvas 中呈现。 What's worse is that this issue is only happening on Chrome!更糟糕的是,这个问题发生在 Chrome 上! I tried the same process in Safari, and it does work.我在 Safari 中尝试了相同的过程,它确实有效。

So, here's the thing: has anyone encountered this browser-specific sort of SVG issue with linear gradients, and, if so, how did you fix it?所以,事情是这样的:有没有人遇到过这种带有线性渐变的浏览器特定类型的 SVG 问题,如果有,你是如何解决的?

I suspect the answer lies in the format of the linearGradient tag.我怀疑答案在于 linearGradient 标签的格式。 Here are two versions of the same file, one from Figma and one from Inkscape (the one from Inkscape works).这是同一个文件的两个版本,一个来自 Figma,一个来自 Inkscape(一个来自 Inkscape 作品)。

Figma无花果

<defs>
<linearGradient id="paint0_linear_338:2881" x1="400" y1="0" x2="400" y2="800" gradientUnits="userSpaceOnUse">
<stop stop-color="#99CCFF" stop-opacity="0.6"/>
<stop offset="0.864583" stop-color="#87C3FF" stop-opacity="0.1"/>
</linearGradient>
<clipPath id="clip0_338:2881">
<rect width="800" height="800" fill="white"/>
</clipPath>
</defs>

Inkscape墨景

<defs
     id="defs1024">
    <linearGradient
       inkscape:collect="always"
       id="MAINGRAD">
      <stop
         style="stop-color:#99ccff;stop-opacity:0.60000002"
         offset="0"
         id="stop2574" />
      <stop
         style="stop-color:#87c3ff;stop-opacity:0.1"
         offset="1"
         id="stop2576" />
    </linearGradient>
    <clipPath
       id="clip0_338:2881">
      <rect
         width="800"
         height="800"
         fill="white"
         id="rect1021" />
    </clipPath>
    <linearGradient
       inkscape:collect="always"
       xlink:href="#MAINGRAD"
       id="linearGradient3482"
       gradientUnits="userSpaceOnUse"
       x1="399.724"
       y1="2.1855699e-08"
       x2="399.724"
       y2="800" />
  </defs>

The issue turned out to be relatively simple.事实证明,这个问题相对简单。 The Canvas in Chrome seems to be sensitive to IDs containing certain formats. Chrome 中的 Canvas 似乎对包含某些格式的 ID 很敏感。 I used the following tool to simplify the exported Figma file format.我使用以下工具来简化导出的 Figma 文件格式。

pip install scour
scour -i input.svg -o output.svg --shorten-ids

Scour simply parses the file and replaces each unique ID with the shortest string possible. Scour 简单地解析文件并用最短的字符串替换每个唯一的 ID。 In this case, replacing paint0_linear_338:2881 with a .在这种情况下,将paint0_linear_338:2881替换a . This was the only change required to fix the problem, and I suspect the issue was the colon.这是解决问题所需的唯一更改,我怀疑问题出在冒号上。 I wouldn't be surprised if updates in future Chrome versions fix this bug.如果未来 Chrome 版本的更新修复了这个错误,我不会感到惊讶。

(Current Chrome Version: 94.0.4606.81 (Official Build) (arm64) ) (当前 Chrome 版本: 94.0.4606.81 (Official Build) (arm64)版本94.0.4606.81 (Official Build) (arm64)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM