[英]Safari issue when rendering strokes using stroke-dasharray
I am animating some strokes belonging to an arrow icon, which works great in all browsers (including IE11), with exception to Safari. 我正在对属于箭头图标的一些笔画进行动画处理,该图标在所有浏览器(包括IE11)中都适用,但Safari除外。 For some reason, Safari is rendering small black flecks when stroke
dashes are set to 0
in a stroke-dasharray
rule. 由于某些原因,当stroke
破折号规则中的stroke-dasharray
破折号设置为0
时,Safari会渲染小的黑色斑点。 My code is below: 我的代码如下:
<svg aria-hidden="true" class="icon icon--arrow-right" height="24px" viewbox="0 0 24 24" width="24px">
<g>
<path class="stroke stroke--1" d="M4,12 L19.3,12"></path>
<polyline class="stroke stroke--2" points="15.05 7.76 19.3 12 15.05 16.24"></polyline>
</g>
</svg>
.icon {
stroke: currentColor;
fill: none;
shape-rendering: geometricPrecision;
}
.icon--arrow-right .stroke--1 {
stroke-dasharray: 0 15.29;
transition: stroke-dasharray 250ms ease-in-out;
}
.icon--arrow-right.active .stroke--1 {
stroke-dasharray: 15.29 0;
}
.icon--arrow-right .stroke--2 {
stroke-dasharray: 0 6.5 0 6.5;
transition: stroke-dasharray 250ms ease-in-out 250ms;
}
.icon--arrow-right.active .stroke--2 {
stroke-dasharray: 0 0 13 0;
}
Run the snippet below in Safari to recreate the issue. 在Safari中运行以下代码段以重新创建问题。
var el = document.querySelector('.icon'); el.onclick = function() { el.classList.toggle('active'); }
.icon { border: 1px solid currentColor; stroke: currentColor; fill: none; shape-rendering: geometricPrecision; cursor: pointer; } .icon--arrow-right .stroke--1 { stroke-dasharray: 0 15.29; transition: stroke-dasharray 250ms ease-in-out; } .icon--arrow-right.active .stroke--1 { stroke-dasharray: 15.29 0; } .icon--arrow-right .stroke--2 { stroke-dasharray: 0 6.5 0 6.5; transition: stroke-dasharray 250ms ease-in-out 250ms; } .icon--arrow-right.active .stroke--2 { stroke-dasharray: 0 0 13 0; }
<p>Click the button below to toggle the <code>.active</code> class.</p> <svg aria-hidden="true" class="icon icon--arrow-right" height="24px" viewbox="0 0 24 24" width="24px"> <g> <path class="stroke stroke--1" d="M4,12 L19.3,12"></path> <polyline class="stroke stroke--2" points="15.05 7.76 19.3 12 15.05 16.24"></polyline> </g> </svg> <p>In Safari, you will see black flecks where <code>stroke</code> dash lengths are set to <code>0</code> when the icon is not active.
Does anyone know why this is happening in Safari, and how to resolve it, so that the black flecks are not visible when a stroke-dash
is set to 0
. 有谁知道为什么这会在Safari中发生以及如何解决,所以当stroke-dash
设置为0
时,黑色斑点是不可见的。
Not sure exactly what is the issue here, sounds like Safari doesn't really like all this floating coordinates, but your values also sounds quite weird. 不确定到底是什么问题,听起来像Safari并不真正喜欢所有这些浮动坐标,但是您的值听起来也很奇怪。
Anyway, using a tiny offset for the first value of the polyline's dash-array then using precise values for others will make Safari happy: 无论如何,对折线的破折号数组的第一个值使用微小的偏移,然后对其他值使用精确的值会使Safari满意:
(note that I got these values by using Safari's result from each element's getTotalLength()
) (请注意,我通过使用来自每个元素的getTotalLength()
Safari结果获得了这些值)
var el = document.querySelector('.icon'); el.onclick = function() { el.classList.toggle('active'); }
.icon { border: 1px solid currentColor; stroke: currentColor; fill: none; shape-rendering: geometricPrecision; cursor: pointer; } .icon--arrow-right .stroke--1 { stroke-dasharray: 0 15.3; transition: stroke-dasharray 250ms ease-in-out; } .icon--arrow-right.active .stroke--1 { stroke-dasharray: 15.3 0; } .icon--arrow-right .stroke--2 { stroke-dasharray: 0.0001 6.003 0.0001 6.003; transition: stroke-dasharray 250ms ease-in-out 250ms; } .icon--arrow-right.active .stroke--2 { stroke-dasharray: 0 0 13 0; }
<p>Click the button below to toggle the <code>.active</code> class.</p> <svg aria-hidden="true" class="icon icon--arrow-right" height="240px" viewbox="0 0 24 24" width="240px"> <g> <path class="stroke stroke--1" d="M4,12 L19.3,12"></path> <polyline class="stroke stroke--2" points="15.05 7.76 19.3 12 15.05 16.24"></polyline> </g> </svg>
I would do this sort of animations by changing the value of the stroke-dashoffset
instead of animating the stroke-dasharray
value. 我会改变的价值做这种动画stroke-dashoffset
而不是动画的stroke-dasharray
值。 Please note I've changes the polygon with a path where you move twice to the tip of the arrow. 请注意,我用一条路径更改了多边形,在该路径中您两次移动到箭头的尖端。 This is fixing the issue you have on Safari. 这解决了您在Safari上遇到的问题。
var el = document.querySelector('.icon'); el.onclick = function() { el.classList.toggle('active'); }
.icon { border: 1px solid currentColor; stroke: currentColor; fill: none; shape-rendering: geometricPrecision; cursor: pointer; } .icon--arrow-right .stroke--1 { stroke-dasharray: 15.3; stroke-dashoffset: 15.3; transition: stroke-dashoffset 250ms ease-in-out; } .icon--arrow-right.active .stroke--1 { stroke-dashoffset: 0; } .icon--arrow-right .stroke--2 { stroke-dasharray: 6.22; stroke-dashoffset: 6.22; transition: stroke-dashoffset 250ms ease-in-out 250ms; } .icon--arrow-right.active .stroke--2 { stroke-dashoffset: 0; }
<p>Click the button below to toggle the <code>.active</code> class.</p> <svg aria-hidden="true" class="icon icon--arrow-right" height="240px" viewbox="0 0 24 24" width="240px"> <g> <path class="stroke stroke--1" d="M4,12 L19.3,12"></path> <!--<polyline class="stroke stroke--2" points="15.05 7.76 19.3 12 15.05 16.24"></polyline>--> <path class="stroke stroke--2" d="M19.3,12.3L15.05, 7.76M19.3,11.7L15.05, 16.24" /> </g> </svg> <p>In Safari, you will see black flecks where <code>stroke</code> dash lengths are set to <code>0</code> when the icon is not active.</p>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.