SVG with drop-shadow blurry on mobile browser

时光总嘲笑我的痴心妄想 提交于 2019-12-22 00:29:39

问题


i am struggling to make work SVG icons with drop-shadow on as much as possible platforms.

I am using following simple CSS for that:

.test{
  width: 14px;
  height: 14px;
  background-image: url("../images/test.svg");
  background-repeat: no-repeat;
  opacity: 0.8;
  -webkit-filter: drop-shadow(1px 1px 0px #E0E0E0);
}

On Desktop browsers like Chrome, Firefox & IE its working good. But on mobile Browser the problems begin: In Chrome the icon gets blurry & in Firefox the drop-shadow is just ignored. When removing the drop-shadow, the icon will be clear again in Chrome, so i think it has something in common with the drop-shadow.

Anyone had the same problem and found maybe a solution for this?


回答1:


Just found the answer for it.
This problem occurs due to the way SVG filters are generated: Before a filter is applied, a bitmap image of the respective element is created which is then manipulated and layered on top. You can find a great explanation here.

To show svg images sharp on HDPI screens, you can try using the filterRes-property on the effect, which provides a pixel resolution for calculated svg effects, more on that here.
Use it like so:

<filter id="dropshadow" height="170%" filterRes="200">
  <feGaussianBlur in="SourceAlpha" stdDeviation="20"/>
  <feOffset dx="0" dy="2" result="offsetblur"/>
  <feMerge> 
    <feMergeNode/>
    <feMergeNode in="SourceGraphic"/>
  </feMerge>
</filter>
<path id="icon" fill="#fff" style="filter:url(#dropshadow)" d="..."></path>
</svg>

Say you add background-size: contain to your css - the drawn container will presumably be 14px x 14px. To render the shadow with sufficient resolution for retina displays (eg. factor 2), you should take a healthy value of 30 for filterRes. This is not ideal, but it seems like it is the only working alternative at the moment. Kind of defeats the reason for using svg at all.



来源:https://stackoverflow.com/questions/25351902/svg-with-drop-shadow-blurry-on-mobile-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!