- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我需要一种基于 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 文档中的呈现方式:
这是原始 PNG 图形 (odd_shape.png):
问题 1:如何重用前 2 个滤镜(drop-shadow
和 outer-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 的屏幕截图。
<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>
<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>
结果:
关于css - 参数化和重用 HTML5 中定义的自定义 SVG 过滤器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21763953/
我有一个对象数组,我想在键传入“filter”过滤器时提取值。下面是我尝试过的 Controller 代码片段,但我得到的响应类型未定义。请帮我找出哪里出错了。 var states = [{"HI
如果任何 J2EE 应用程序直接访问 servlet,然后 servlet 将相同的请求转发到某个 .jsp 页面。 request.getRequestDispatcher("Login.jsp")
我有一个带有图像缩略图的表单,可以通过复选框进行选择以进行下载。我想要一个包含 jQuery 中图像的数组,用于 Ajax 调用。 2个问题: - 表格顶部有一个复选框,用于切换我想要从映射中排除的所
我必须从服务器转储数据库,将 .sql 传输到另一台服务器,然后运行以下脚本以使用此语法删除某些行: DELETE wp_posts FROM wp_posts INNER JOIN wp_postm
我想从目录中过滤掉特定类型的文件,但收到错误“ token 语法错误,删除这些 token ”: File dir = new File("c:/etc/etc"); File[] f
几乎所有的 Web 应用程序都依赖外部的输入。这些数据通常来自用户或其他应用程序(比如 web 服务)。通过使用过滤器,您能够确保应用程序获得正确的输入类型。 您应该始终对外部数据进行过滤! 输
我正在开发一个由 OData 服务提供支持的搜索功能。它将返回一个或一列标题对象作为结果。我们需要搜索的许多字段不在标题对象中。它们仅在子对象(导航属性)中。能够针对子字段执行 OData 搜索并仍然
假设我有以下模型,它有一个方法 variants(): class Example(models.Model): text = models.CharField(max_length=255)
我有一个默认的列表列表,但我基本上想这样做: myDefaultDict = filter(lambda k: len(k)>1, myDefaultDict) 除了它似乎只适用于列表。我能做什么?
我正在使用 django-filter 来输出我的模型的过滤结果。那里没有问题。下一步是添加一个分页器……尽管现在已经苦苦挣扎了好几天。 views.py: def funds_overview(re
我正在做一个概念证明,我正在试验一种奇怪的行为。 我有一个按日期字段按范围分区的表,如果我设置固定日期或由 SYSDATE 创建的日期,查询的成本会发生很大变化。 这些是解释计划: SQL> SELE
如果一个或另一个值匹配,是否可以制作一个过滤器,例如一个中性的 PropertyFilter(并传递给链中的下一个过滤器)?就像是: value1 val
我是 VBA 初学者,正在尝试根据单元格值过滤数据,经过一番谷歌搜索后,我编写了一个有效的代码 Sub FilterDepartment_Sales() Sheet6.Activate
假设我在 excel 数据透视表中有两个过滤器。 两者最初都会显示筛选列的选定范围内的所有值。 当我仅在过滤器 1 中选择几个值时,过滤器 2 仍会继续显示基础数据中所选范围内特定过滤器列中的所有值。
是否可以定义自定义 build-ins (名称不再适合)在 ftl? 由于语义前提,我不想让它成为一个函数,而是一个内置的。 最佳答案 这是不可能的,?语法是为内置函数保留的。 (顺便说一句,这意味着
我试图在 Edit | 之外添加一个链接通过插件删除wordpress管理员>用户>所有用户列表中的链接..这是我第一次尝试通过查看其他插件或搜索google来制作wordpress插件.. 我添加了
我正在尝试按照以下教程使用 django 过滤器进行分页,但该教程似乎缺少某些内容,而且我无法使用基于函数的 View 方法显示分页。 https://simpleisbetterthancomple
由于我是 Powershell 新手,因此寻求最佳实践方面的帮助, 我有一个 csv 文件,我想过滤掉 csv 中的每一行,除了包含“未安装”的行 然后,我想根据包含计算机列表的单独 csv 文件过滤
我正在尝试创建一个搜索查询,它会告诉我我作为审阅者添加到其中的打开更改,但我还没有提交最新补丁集的代码审查。这应该包括其他人已经评论过的更改,但我没有。 我能找到的最接近的是 is:reviewer
在我的 Web 应用程序中,我有 3 个主要部分 1. 客户 2. 供应商 3. 管理员 我正在使用 java session 过滤器来检查用户 session 并允许访问网站的特定部分。 因此客户只
我是一名优秀的程序员,十分优秀!