svg - 使用 SVG 滤镜创建小插图?

标签 svg filter

我想使用 SVG 滤镜在图像上创建晕影。解决这个问题的最佳方法是什么?我已经尝试创建一个 feFlood,其渐变为 flood-color,但这不起作用。现在我正在使用在 illustrator 中生成的 png,但我想将其全部保存在 svg 中。

为了说明我的目标,这是原文:

without vignette

这是应该的:

with vignette

更新:

我正在使用 svg.jssvg.filter.js动态生成代码的插件。这是我尝试过的:

// create svg canvas
var draw = SVG('canvas').size(400,400)

// define gradient
var gradient = draw.gradient('radial', function(stop) {
    stop.at({ offset: 0, opacity: 0 })
    stop.at({ offset: 1 })
})
gradient.radius('80%')

// create image
var image = draw.image('http://distilleryimage11.ak.instagram.com/89ac2e90d9b111e297bf22000a1f9263_7.jpg').size(400,400)

// add filter
image.filter(function(add) {
    add.blend(add.source, add.flood(gradient), 'multiply')
})

这是生成的代码:

<svg id="SvgjsSvg1000" xmlns="http://www.w3.org/2000/svg" version="1.1" width="400" height="400" xmlns:xlink="http://www.w3.org/1999/xlink"     style="position:relative;overflow:hidden;left:0px;top:0px;">
    <image id="SvgjsImage1005" xlink:href="http://distilleryimage11.ak.instagram.com/89ac2e90d9b111e297bf22000a1f9263_7.jpg" width="400" height="400" filter="url(    #SvgjsFilter1006)"></image>
    <defs id="SvgjsDefs1001">
        <radialGradient id="SvgjsRadialGradient1002" r="80%">
            <stop id="SvgjsStop1003" stop-opacity="0" offset="0"></stop>
            <stop id="SvgjsStop1004" offset="1"></stop>
        </radialGradient>
        <filter id="SvgjsFilter1006">
            <feFlood id="SvgjsFeFlood1007" in="SourceGraphic" result="SvgjsFeFlood1007Out" flood-color="url(#SvgjsRadialGradient1002)"></feFlood>
            <feBlend id="SvgjsFeBlend1008" in="SourceGraphic" result="SvgjsFeBlend1008Out" in2="SvgjsFeFlood1007Out" mode="multiply"></feBlend>
        </filter>
    </defs>
</svg>

结果是一个全黑的图像。似乎 feFlood 元素不接受渐变作为填充,因为它使用颜色。

这是一个带有示例代码的 fiddle :http://jsfiddle.net/wout/VmUu6/

最佳答案

这在没有聚光灯的情况下对我有用,我使用具有多个停止点的径向渐变,您可以调整颜色、停止点和不透明度级别以获得您想要的效果。使用 r(radius 来扩大/缩短效果,使用 fx,fy 来定位。渐变往往比使用一些需要大量数学处理的过滤器更有效

<radialGradient id="grad"
                fx="50%" fy="50%" r="55%"
                spreadMethod="pad">
    <stop offset="30%"   stop-color="#222222" stop-opacity="0"/>
    <stop offset="40%"   stop-color="#222222" stop-opacity="0.2"/>
    <stop offset="50%"   stop-color="#222222" stop-opacity="0.4"/>
    <stop offset="70%" stop-color="#222222" stop-opacity="0.6" />
    <stop offset="100%" stop-color="#222222" stop-opacity="1" />
</radialGradient>

应用于位于图像上方的矩形

  <image id="background" x="0" y="0" width="800px" height="530px"  preserveAspectRatio="true"
     xlink:href="http://i1-qa.adis.ws/i/Client_23/ss_collection_reddress?w=800"/>
<rect filter="url(#blur)" style="fill:url(#grad)" x="0" y="0" width="800px" height="530px"/>

vignette example

关于svg - 使用 SVG 滤镜创建小插图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18018318/

相关文章:

delphi - Delphi:使用枚举器按类类型过滤TList <T:class>吗?

string - 过滤数组中的字符串以获取特定属性 Matlab

html - 将引用 SVG 图像嵌入 HTML 文件

javascript - svg - 绘制空心矩形(矩形 donut )?

javascript - 栅格化动态生成的 SVG

javascript - 我得到 [1,15],而不是 [1,0,15]。 JavaScript 过滤器

java - 如何在 Java 中过滤数组?

C++图像内核环绕卷积

javascript - 无法删除所有 D3 SVG 元素

css - ImageMagick 将 SVG 转换为具有透明背景的 PNG