gpt4 book ai didi

css - 参数化和重用 HTML5 中定义的自定义 SVG 过滤器?

转载 作者:搜寻专家 更新时间:2023-10-31 22:29:45 24 4
gpt4 key购买 nike

我需要一种基于 alpha 蒙版向透明 PNG 图像添加“描边”(轮廓)和阴影效果的方法,而我能找到的唯一解决方案是使用自定义 SVG 滤镜。 (注意:我需要这些效果的网络应用程序供我自己私有(private)使用,所以这个解决方案与旧版浏览器不兼容没关系。继续......)

我以前从未使用过 SVG,但单独创建笔划和阴影滤镜非常简单。不幸的是,如果不实际将滤镜复制并粘贴到新滤镜中,我找不到创建组合效果的方法,如下面的代码所示:

<svg width="0" height="0" xmlns="http://www.w3.org/2000/svg">

<!-- drop shadow -->
<filter id="drop-shadow">
<feGaussianBlur in="SourceAlpha" stdDeviation="4" />
<feOffset result="m_offsetBlurred" dx="12" dy="12" />
<feFlood result="m_floodTrans50" flood-color="rgba(0,0,0,0.5)" />
<feComposite result="m_offsetBlurredTrans50" in="m_floodTrans50" in2="m_offsetBlurred" operator="in" />
<feMerge>
<feMergeNode in="m_offsetBlurredTrans50" />
<feMergeNode in="SourceGraphic" />
</feMerge>
</filter>


<!-- outer stroke -->
<filter id="outer-stroke">
<!-- create rectangle of the desired color -->
<feFlood result="m_floodRect" flood-color="black" />

<!-- create copy of png's alpha mask and expand -->
<feMorphology result="m_expandedMask" in="SourceAlpha" operator="dilate" radius="1" />

<!-- "cut out" a section of the flood fill matching the expanded copy -->
<feComposite result="m_expandedColored" in="m_floodRect" in2="m_expandedMask" operator="in" />

<!-- blend it behind the original shape to create the outline effect -->
<feBlend in="SourceGraphic" in2="m_expandedColored" mode="normal" />
</filter>


<!-- drop shadow & outer stroke (must copy & paste the 2 filters above, which violates the DRY principle) -->
<filter id="outer-stroke-drop-shadow">
<!-- create rectangle of the desired color -->
<feFlood result="m_floodRect" flood-color="black" />

<!-- create copy of png's alpha mask and expand -->
<feMorphology result="m_expandedMask" in="SourceAlpha" operator="dilate" radius="1" />

<!-- "cut out" a section of the flood fill matching the expanded copy -->
<feComposite result="m_expandedColored" in="m_floodRect" in2="m_expandedMask" operator="in" />

<!-- blend it behind the original shape to create the outline effect -->
<feBlend result="m_stroked" in="SourceGraphic" in2="m_expandedColored" mode="normal" />

<!-- add drop shadow -->
<feGaussianBlur result="m_blurred" in="SourceAlpha" stdDeviation="4" />
<feOffset result="m_offsetBlurred" in="m_blurred" dx="12" dy="12" />
<feFlood result="m_floodTrans50" flood-color="rgba(0,0,0,0.5)" />
<feComposite result="m_offsetBlurredTrans50" in="m_floodTrans50" in2="m_offsetBlurred" operator="in" />
<feMerge>
<feMergeNode in="m_offsetBlurredTrans50" />
<feMergeNode in="m_stroked" />
</feMerge>
</filter>
</svg>


<style>
.fx_drop_shadow { filter: url('#drop-shadow'); }
.fx_outer_stroke { filter: url('#outer-stroke'); }
.fx_outer_stroke_drop_shadow { filter: url('#outer-stroke-drop-shadow'); }
</style>


<div>
<img src="gfx/odd_shape.png" />
<img src="gfx/odd_shape.png" class="fx_drop_shadow" />
<img src="gfx/odd_shape.png" class="fx_outer_stroke" />
<img src="gfx/odd_shape.png" class="fx_outer_stroke_drop_shadow" />
</div>

以下是上述代码在 HTML5 文档中的呈现方式:

SVG filters applied to a PNG image

这是原始 PNG 图形 (odd_shape.png):

enter image description here

问题 1:如何重用前 2 个滤镜(drop-shadowouter-stroke)以便我可以简单地应用它们在组合滤镜 (outer-stroke-drop-shadow) 中,而不必复制和粘贴它们。

问题 2:是否可以参数化自定义滤镜,以便我可以指定描边颜色或阴影透明度等内容?这将使它们更加可重用。


谢谢。

最佳答案

这是一个完整的解决方案,适用于我测试过的两种浏览器(Firefox 和 Chrome)...

问题 1 的解决方案: 我测试的浏览器都不支持在 filter property 中指定多个过滤器。 , 所以最好的(也许是唯一的)组合用户定义过滤器的方法是使用 Michael Mullany 建议的技术:在嵌套的 <g> 中顺序应用它们元素,根据需要创建过滤器图。

问题 2 的解决方案:W3C 有一份 SVG Parameters 的工作草案草稿包括 polyfill script用于使用和测试建议的功能。参数通过 param() 声明功能属性值(例如 param(shadowColor) black )并通过类似查询字符串的界面(例如 foo.svg?shadowColor=red )或通过 <object> 的子元素定义容器(例如 <param name="shadowColor" value="red"/> )。

下面提供了两种解决方案的演示代码,以及来自 Firefox 的屏幕截图。


在 mypage.html 中:

<object type="image/svg+xml" data="filters.svg?osColor=lime&dsAlpha=0.4"></object>
<object type="image/svg+xml" data="filters.svg?osColor=white&osWidth=4&dsAlpha=0.8&dsBlurSigma=8&dsOffsetX=32"></object>


在 filters.svg 中:

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="300px" height="320px" viewBox="0 0 300 320">
<defs>
<filter id="dropShadow" width="150%">
<feGaussianBlur in="SourceAlpha" stdDeviation="param(dsBlurSigma) 4" />
<feOffset result="m_offsetBlurred" dx="param(dsOffsetX) 12" dy="param(dsOffsetY) 12" />
<feComponentTransfer result="m_offsetBlurredTranslucent" in="m_offsetBlurred">
<feFuncA type="linear" slope="param(dsAlpha) 0.5" />
</feComponentTransfer>
<feMerge>
<feMergeNode in="m_offsetBlurredTranslucent" />
<feMergeNode in="SourceGraphic" />
</feMerge>
</filter>
<filter id="outerStroke" width="150%">
<feFlood result="m_floodRect" flood-color="param(osColor) black" />
<feMorphology result="m_expandedMask" in="SourceAlpha" operator="dilate" radius="param(osWidth) 1" />
<feComposite result="m_expandedColored" in="m_floodRect" in2="m_expandedMask" operator="in" />
<feBlend in="SourceGraphic" in2="m_expandedColored" mode="normal" />
</filter>
</defs>

<!-- combine stroke & drop shadow -->
<g style='filter:url(#dropShadow);' width='300' height='320'>
<g style='filter:url(#outerStroke);'>
<image width='240' height='280' xlink:href="gfx/odd_shape.png"></image>
</g>
</g>

<!-- use polyfill from http://dev.w3.org/SVG/modules/param/master/SVGParamPrimer.html -->
<script type="text/ecmascript" xlink:href="http://dev.w3.org/SVG/modules/param/master/param.js" />
</svg>

结果:

enter image description here

关于css - 参数化和重用 HTML5 中定义的自定义 SVG 过滤器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21763953/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com