大佬教程收集整理的这篇文章主要介绍了参数化和重用HTML5中定义的自定义SVG过滤器?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我之前从未使用过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.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.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文档中的呈现方式:
这是原始的PNG图形(odd_shape.png):
问题1:如何重复使用前2个滤镜(阴影和外笔画),这样我就可以将它们简单地应用于组合滤镜(外部笔画 – 阴影),而不必复制和粘贴它们.
问题2:是否可以参数化自定义过滤器,以便我可以指定诸如笔触颜色或投影的透明度之类的内容?这将使它们更加可重用.
谢谢.
问题1的解决方案:我测试的浏览器都没有支持在filter property中指定多个过滤器,因此组合用户定义过滤器的最佳(也许是唯一)方法是使用Michael Mullany建议的技术:在嵌套中顺序应用它们< g取代;元素,根据需要创建过滤器图形.
问题2的解决方案:W3C的工作草案为SVG Parameters,草案包括polyfill script,用于使用和测试建议的功能.参数通过@R_792_1403@函数属性值(例如,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>
结果:
以上是大佬教程为你收集整理的参数化和重用HTML5中定义的自定义SVG过滤器?全部内容,希望文章能够帮你解决参数化和重用HTML5中定义的自定义SVG过滤器?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。