在SVG元素中添加斜角和浮雕?

时间:2013-08-29 17:50:30

标签: css css3 svg svg-filters emboss

所以我想知道是否可以在SVG元素中添加斜角和浮雕?

我的矩形元素的CSS是这样的:

rect {
  fill: #e8e9eb;
  stroke: black;
  stroke-width: 1px;
  width: 70;
  height: 30;
}

我正在尝试将此CSS添加到here

-webkit-box-shadow: inset 0 1px 0 rgba(255,255,255,.5), inset 0 -2px 0 rgba(0,0,0,.25), inset 0 -3px 0 rgba(255,255,255,.2), 0 1px 0 rgba(0,0,0,.1);
-moz-box-shadow: inset 0 1px 0 rgba(255,255,255,.5), inset 0 -2px 0 rgba(0,0,0,.25), inset 0 -3px 0 rgba(255,255,255,.2), 0 1px 0 rgba(0,0,0,.1);
box-shadow: inset 0 1px 0 rgba(255,255,255,.5), inset 0 -2px 0 rgba(0,0,0,.25), inset 0 -3px 0 rgba(255,255,255,.2), 0 1px 0 rgba(0,0,0,.1);

我认为它不起作用的原因是因为它使用fill而不是background,但我不确定。有没有办法在使用fill CSS样式时执行此操作?如果没有,最好的方法是什么?

2 个答案:

答案 0 :(得分:11)

您希望使用SVG Filter effects来斜切任意SVG内容。

以下是两个斜角版本的示例:
http://jsfiddle.net/rcd5L/

<svg xmlns="http://www.w3.org/2000/svg" viewBox="-10 -10 120 100">
  <filter id="Bevel" filterUnits="objectBoundingBox" x="-10%" y="-10%" width="150%" height="150%">
    <feGaussianBlur in="SourceAlpha" stdDeviation="3" result="blur"/>
    <feSpecularLighting in="blur" surfaceScale="5" specularConstant="0.5" specularExponent="10" result="specOut" lighting-color="white">
      <fePointLight x="-5000" y="-10000" z="20000"/>
    </feSpecularLighting>
    <feComposite in="specOut" in2="SourceAlpha" operator="in" result="specOut2"/>
    <feComposite in="SourceGraphic" in2="specOut2" operator="arithmetic" k1="0" k2="1" k3="1" k4="0" result="litPaint" />
  </filter>
  <filter id="Bevel2" filterUnits="objectBoundingBox" x="-10%" y="-10%" width="150%" height="150%">
    <feGaussianBlur in="SourceAlpha" stdDeviation="0.5" result="blur"/>
    <feSpecularLighting in="blur" surfaceScale="5" specularConstant="0.5" specularExponent="10" result="specOut" lighting-color="white">
      <fePointLight x="-5000" y="-10000" z="0000"/>
    </feSpecularLighting>
    <feComposite in="specOut" in2="SourceAlpha" operator="in" result="specOut2"/>
    <feComposite in="SourceGraphic" in2="specOut2" operator="arithmetic" k1="0" k2="1" k3="1" k4="0" result="litPaint" />
  </filter>
  <rect width="100" height="40" filter="url(#Bevel)" />
  <rect y="50" width="100" height="40" filter="url(#Bevel2)" />
</svg>

答案 1 :(得分:2)

如果你想要这种斜角

,也可以尝试使用它
<svg xmlns="http://www.w3.org/2000/svg" viewBox="-10 -10 120 100">
<filter id="filter1" >

          <feFlood flood-color="black" result="COLOR-red" />

          <feMorphology operator="dilate" radius="0" in="SourceAlpha" result="STROKE_10" />

         <feConvolveMatrix order="8,8" divisor="1"
          kernelMatrix="1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1" in="STROKE_10" result="BEVEL_20" />

          <feOffset dx="1" dy="1" in="BEVEL_0" result="BEVEL_25"/>
          <feComposite operator="out" in="BEVEL_25" in2="STROKE_10" result="BEVEL_30"/>
          <feComposite in="COLOR-red" in2="BEVEL_30" operator="in" result="BEVEL_40" />
          <feMerge result="BEVEL_50">
            <feMergeNode in="BEVEL_40" />
            <feMergeNode in="SourceGraphic" />
          </feMerge>



      </filter>

    <rect width="100" height="40" filter="url(#filter1)" />
    <path x="50" y="50" fill="#3182bd" d="M9.184850993605149e-15,-150A150,150 0 0,1 140.95389311788625,-51.30302149885031L0,0Z" filter="url(#filter1)"></path>
</svg>