简体   繁体   English

在形状填充上应用SVG遮罩

[英]Applying SVG Mask over shape Fill

I want to darken the fill of objects according to the defined gradient below. 我想根据下面定义的渐变使对象的填充变暗。 However, when the mask goes over the fill, it lightens it instead. 但是,当遮罩超过填充量时,它会变亮。 I am assuming this has to be based on the decision on how to blend the colors. 我认为这必须基于关于如何混合颜色的决定。 Its using additive colors rather than subtractive. 它使用加色而不是减色。 Do I need to apply a filter on top of the mask? 我是否需要在面膜上应用过滤器? I feel like there should be an attribute in either the gradient or mask definitions to make it blend the colors the way I want. 我觉得应该在渐变或遮罩定义中都有一个属性,使其以我想要的方式混合颜色。 Code and fiddle link below: 下面的代码和小提琴链接:

<svg width="400px" height="500px">
  <defs>
    <linearGradient id="mygradient" gradientUnits="userSpaceOnUse"
                    x1="0" y1="0" x2="0" y2="100%">
      <stop offset="0" stop-color="black"/>
      <stop offset="0.5" stop-color="white"/>                
      <stop offset="1" stop-color="black"/>
    </linearGradient>

      <mask id="mask1" x="0" y="0" width="400" height="500" MaskUnits="userSpaceOnUse" color-interpolation="sRGB">
    <rect x="0" y="0" width="400" height="500"
        style="stroke:none; fill: url(#mygradient)"/>
  </mask>
  </defs>



  <g fill="red" mask= "url(#mask1)" stroke="black" stroke-width="4"
    feblend="Multiply">

    <rect x="5" y="5" width="390" height="90"/>
    <rect x="5" y="105" width="390" height="90"/>
    <rect x="5" y="205" width="390" height="90"/>
    <rect x="5" y="305" width="390" height="90"/>
     <rect x="5" y="405" width="390" height="90"/>

  </g>

</svg>

https://jsfiddle.net/6q3x8x5u/ https://jsfiddle.net/6q3x8x5u/

You can replace the mask with a simple <rect> with the proper blend mode. 您可以使用具有适当混合模式的简单<rect>替换蒙版。

(Below the <g> ): (在<g>下方):

<rect x="0" y="0" width="400" height="500" style="stroke:none;
  fill: url(#mygradient); mix-blend-mode: multiply" />

See this fiddle 看到这个小提琴

Why the mask? 为什么要戴口罩? Masks are used to vary the transparency, not the colour. 遮罩用于改变透明度,而不是颜色。

You have a gradient in your SVG that you are not using. 您的SVG中有一个未使用的渐变。 If you adjust the colours of the gradient, you can get what it sounds like you want. 如果您调整渐变的颜色,则可以得到想要的效果。

 <svg width="400px" height="500px"> <defs> <linearGradient id="mygradient" gradientUnits="userSpaceOnUse" x1="0" y1="0" x2="0" y2="100%"> <stop offset="0" stop-color="black"/> <stop offset="0.5" stop-color="red"/> <stop offset="1" stop-color="black"/> </linearGradient> </defs> <g fill="url(#mygradient)" stroke="black" stroke-width="4" feblend="Multiply"> <rect x="5" y="5" width="390" height="90"/> <rect x="5" y="105" width="390" height="90"/> <rect x="5" y="205" width="390" height="90"/> <rect x="5" y="305" width="390" height="90"/> <rect x="5" y="405" width="390" height="90"/> </g> </svg> 

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

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