为每个元素单独转换线性渐变

huangapple go评论50阅读模式
英文:

Transforming Linear Gradients Individually For Each Element

问题

只要我正确理解:

  • 如果我想在SVG元素上使用线性渐变,比如<rect>,我需要首先将<linearGradient>放在<defs>内,然后在fillstyle中引用它。
  • 如果我想为每个<rect>单独转换渐变,我无法在没有为每个单独的<rect>定义<linearGradient>的情况下执行此操作,因为gradientTransform属性仅允许在渐变元素上使用。

我感到困惑,因为ChatGPT告诉我我可以在我的<rect>上放置gradientTransform,但MDN说

您可以将此属性与以下SVG元素一起使用:

  • <linearGradient>
  • <radialGradient>

所以我是否需要为每个<rect>使用单独的<linearGradient>

英文:

Just wanted to double check whether I understand this correctly:

  • If I want to use linear gradients on SVG elements, like &lt;rect&gt;, I need to first put the &lt;linearGradient&gt; inside the &lt;defs&gt; and then reference it either in fill or in style.
  • If I want to transform the gradient for each &lt;rect&gt; individually, I can't do that without defining a &lt;linearGradient&gt; for each individual &lt;rect&gt;, since the gradientTransform property is only allowed on gradient elements.

I'm just confused because ChatGPT told me I can put gradientTransform on my &lt;rect&gt; but MDN says

>You can use this attribute with the following SVG elements:
> - &lt;linearGradient&gt;
> - &lt;radialGradient&gt;

So I need individual &lt;linearGradient&gt;s for each &lt;rect&gt;?

答案1

得分: 2

是的,对于每个变换,您需要不同的渐变。但是SVG渐变元素支持一些模板化:

大多数绘制服务器元素接受href属性,它可以用来将兼容的绘制服务器元素定义为模板。如果当前元素上没有指定相应属性,那么将使用模板元素定义的属性,而且如果当前元素除了描述性元素之外没有子内容,那么模板元素的子内容会被克隆以替换它。

就像这样:

<linearGradient id="grad0" x1="0" y1="0" x2="1" y2="1">
  <stop offset="5%" stop-color="#A8F" />
  <stop offset="95%" stop-color="#FDC" />
</linearGradient>
<linearGradient id="grad1" href="grad0" gradientTransform="rotate(30)" />
<linearGradient id="grad2" href="grad0" gradientTransform="rotate(60)" />

这将允许您在grad1grad2中使用grad0作为模板,同时应用不同的渐变变换。

英文:

Yes, you will need a different gradient for each transformation. But the SVG gradient elements support a bit of templating:

> Most paint server elements accept an href attribute, which can be used to define a compatible paint server element as a template. Attributes defined for the template element are used instead of the initial value if corresponding attributes are not specified on the current element. Furthermore, if the current element does not have any child content other than descriptive elements, than the child content of the template element is cloned to replace it.

Like this:

&lt;linearGradient id=&quot;grad0&quot; x1=&quot;0&quot; y1=&quot;0&quot; x2=&quot;1&quot; y2=&quot;1&quot;&gt;
  &lt;stop offset=&quot;5%&quot; stop-color=&quot;#A8F&quot; /&gt;
  &lt;stop offset=&quot;95%&quot; stop-color=&quot;#FDC&quot; /&gt;
&lt;/linearGradient&gt;
&lt;linearGradient id=&quot;grad1&quot; href=&quot;grad0&quot; gradientTransform=&quot;rotate(30)&quot; /&gt;
&lt;linearGradient id=&quot;grad2&quot; href=&quot;grad0&quot; gradientTransform=&quot;rotate(60)&quot; /&gt;

huangapple
  • 本文由 发表于 2023年3月3日 19:11:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75626350.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定