2017-02-24 69 views
4

我想在svg路径上模拟和“内部行程”。 我有一个svg地图与多个复杂的路径(国家),每个具有不同的填充颜色描边。 我想在第一个中添加一个“假内部中风”。 我设法用内部阴影技巧(使用高斯模糊滤镜)完成了一些事情,但无法将其设置为“非模糊”。使用SVG过滤器创建一个内部行程

理想的解决方案将作为一个SVG过滤器,所以我可以通过JS动态应用它,而无需更改路径或操作DOM。

非常感谢! 编辑1: 到目前为止,我想这招,但假的阴影有时在行程总是模糊的,所以我不知道这是即使是最好的办法......

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="150" height="150" style="transform:scale(2);transform-origin:0 0 "> 
 
    <defs> 
 
    <filter id='inset' x='-50%' y='-50%' width='200%' height='200%'> 
 
    
 
    <feFlood fill-color="black"/> 
 
    <feComposite in2="SourceAlpha" operator="out"/> 
 
    
 
    <feGaussianBlur stdDeviation='10' edgeMode="none" /> 
 
    <feOffset dx='0' dy='0' result='offsetblur'/> 
 
    <feFlood flood-color='#00ff00' result='color'/> 
 
    <feComposite in2='offsetblur' operator='in'/> 
 
    <feComposite in2='SourceAlpha' operator='in' /> 
 
    <feMerge> 
 
     <feMergeNode in='SourceGraphic'/> 
 
     <feMergeNode/> 
 
    </feMerge> 
 
    </filter> 
 
     
 
\t </defs> 
 
    
 
<path class="st0" d="M144.7,126.2l-2.8,8.8l-3.9-2.3l-2-7.7l1.7-4.3l5.5-4.4L144.7,126.2z M93.5,24.2l6,6.3l4.4-1l7.5,6l1.9,1.1 
 
\t l2.5-0.3l4,3.4l12.3,2.4l-4.3,8.9l-1.1,9.1l-2.4,2.2l-3.9-1.2l0.3,3.2l-6.3,7l-0.1,5.6l4.1-1.9l2.9,5.4L121,84l2.5,4.6l-3,3.7 
 
\t l2.2,9.3l4.6,1.5l-1,5.1l-7.8,6.6l-16.9-3.2l-12.5,3.8l-1,7l-9.9,1.5l-9.6-5.3l-3.1,2.5l-15.8-5.3l-3.4-4.6l4.4-7.1l1.6-24.1 
 
\t l-8.8-13l-6.3-6.4l-13.1-4.9l-0.9-9.4l11.1-2.8L48.9,47l-2.7-14.8l8.1,5.7l20-10.3l2.6-11l7.5-2.8l1.3,4.8l4,0.2L93.5,24.2z" stroke-width="1" fill="#00ffff" stroke="#FF0000" filter="url(#inset)"/> 
 
</svg>

回答

2

如果你想清楚的形状,你应该使用SVG变换而不是将CSS转换应用于svg元素。

而当您绘制“内侧中风”时,feMorphorogy元素很有用。这会减少(或增加)目标形状的绘画区域,因此您可以绘制“伪内/外”笔画。

<svg xmlns="http://www.w3.org/2000/svg" 
 
    xmlns:xlink="http://www.w3.org/1999/xlink" width="300" height="300"> 
 
    <defs> 
 
    <filter id='inset' x='-50%' y='-50%' width='200%' height='200%'> 
 
     <!--outside-stroke--> 
 
     <feFlood flood-color="red" result="outside-color"/> 
 
     <feMorphology in="SourceAlpha" operator="dilate" radius="2"/> 
 
     <feComposite in="outside-color" operator="in" result="outside-stroke"/> 
 
     <!--inside-stroke--> 
 
     <feFlood flood-color="blue" result="inside-color"/> 
 
     <feComposite in2="SourceAlpha" operator="in" result="inside-stroke"/> 
 
     <!--fill-area--> 
 
     <feMorphology in="SourceAlpha" operator="erode" radius="2"/> 
 
     <feComposite in="SourceGraphic" operator="in" result="fill-area"/> 
 
     <!--merge graphics--> 
 
     <feMerge> 
 
     <feMergeNode in="outside-stroke"/> 
 
     <feMergeNode in="inside-stroke"/> 
 
     <feMergeNode in="fill-area"/> 
 
     </feMerge> 
 
    </filter> 
 
    </defs> 
 
    <g transform="scale(2)"> 
 
    <path class="st0" d="M144.7,126.2l-2.8,8.8l-3.9-2.3l-2-7.7l1.7-4.3l5.5-4.4L144.7,126.2z M93.5,24.2l6,6.3l4.4-1l7.5,6l1.9,1.1 
 
    l2.5-0.3l4,3.4l12.3,2.4l-4.3,8.9l-1.1,9.1l-2.4,2.2l-3.9-1.2l0.3,3.2l-6.3,7l-0.1,5.6l4.1-1.9l2.9,5.4L121,84l2.5,4.6l-3,3.7 
 
    l2.2,9.3l4.6,1.5l-1,5.1l-7.8,6.6l-16.9-3.2l-12.5,3.8l-1,7l-9.9,1.5l-9.6-5.3l-3.1,2.5l-15.8-5.3l-3.4-4.6l4.4-7.1l1.6-24.1 
 
    l-8.8-13l-6.3-6.4l-13.1-4.9l-0.9-9.4l11.1-2.8L48.9,47l-2.7-14.8l8.1,5.7l20-10.3l2.6-11l7.5-2.8l1.3,4.8l4,0.2L93.5,24.2z" 
 
fill="#00ffff" filter="url(#inset)"/> 
 
    </g> 
 
</svg>

+0

非常感谢,我结束了使用它,它运作良好。 – Flunch

3

这就是你想要的。请注意,它取决于笔划是与任何填充颜色不同的单一颜色(在本例中为100%红色 - 您可以将笔触颜色更改为任何您想要的,但过滤器变得更复杂)。

您可以通过更改最终feColorMatrix最后一列中的值来调整“假”内部笔触的颜色。现在,它是100%蓝色。 (您也可以使用feMorphology创建此 - 在高清的答案 - 但这种做法不会保留原来的斜切。)

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="150" height="150" style="transform:scale(2);transform-origin:0 0 "> 
 
    <defs> 
 
    <filter id='fake-stroke' x='-50%' y='-50%' width='200%' height='200%' color-interpolation-filters="sRGB"> 
 
    
 
    <!-- select just the red outline and zero out the opacity of everything that's not 100% red. --> 
 
    <feColorMatrix type="matrix" values="1 0 0 0 0 
 
             0 0 0 0 0 
 
             0 0 0 0 0 
 
             255 -255 -255 -254 0" result="outline-only"/> 
 
    <feGaussianBlur stdDeviation="1"/> 
 

 
    <!-- select just the blur - not the original stroke. --> 
 
    <feComposite operator="out" in2="outline-only"/> 
 

 
    <!-- select just the blur that overlaps the original content --> 
 
    <feComposite operator="in" in2="SourceGraphic" /> 
 

 
    <!-- increase its opacity to 100% except the most blurred - to fake anti-aliasing --> 
 
    <feComponentTransfer> 
 
     <feFuncA type="table" tableValues="0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1"/> 
 
    </feComponentTransfer> 
 

 
    <!-- change the color of the fake stroke to the desired value --> 
 
    <feColorMatrix type="matrix" values ="0 0 0 0 0 
 
              0 0 0 0 0 
 
              0 0 0 0 1 
 
              0 0 0 1 0"/> 
 
    <!-- put it on top of the original --> 
 
    <feComposite operator="over" in2="SourceGraphic"/> 
 

 
    </filter> 
 
     
 
\t </defs> 
 
    
 
<path class="st0" d="M144.7,126.2l-2.8,8.8l-3.9-2.3l-2-7.7l1.7-4.3l5.5-4.4L144.7,126.2z M93.5,24.2l6,6.3l4.4-1l7.5,6l1.9,1.1 
 
\t l2.5-0.3l4,3.4l12.3,2.4l-4.3,8.9l-1.1,9.1l-2.4,2.2l-3.9-1.2l0.3,3.2l-6.3,7l-0.1,5.6l4.1-1.9l2.9,5.4L121,84l2.5,4.6l-3,3.7 
 
\t l2.2,9.3l4.6,1.5l-1,5.1l-7.8,6.6l-16.9-3.2l-12.5,3.8l-1,7l-9.9,1.5l-9.6-5.3l-3.1,2.5l-15.8-5.3l-3.4-4.6l4.4-7.1l1.6-24.1 
 
\t l-8.8-13l-6.3-6.4l-13.1-4.9l-0.9-9.4l11.1-2.8L48.9,47l-2.7-14.8l8.1,5.7l20-10.3l2.6-11l7.5-2.8l1.3,4.8l4,0.2L93.5,24.2z" stroke-width="2" fill="#00ffff" stroke="#FF0000" filter="url(#fake-stroke)"/> 
 
</svg>

+0

非常感谢,我尝试使用它,但在JS动态更新矩阵值失败了,其他的解决方案是我的使用情况更简单。 – Flunch