简体   繁体   English

对 SVG 组应用 CSS 变换(例如,D3 的缩放变换)导致其文本元素在 Safari 中闪烁

[英]Applying CSS transform on SVG group (e.g., D3's zoom transform) causes its text elements to flicker in Safari

I notice that applying CSS transform on SVG groups would cause their children text elements to flicker in Safari. The transformation of other elements looks smooth in Safari. The transformation of all children looks smooth in Firefox or Chrome.我注意到在 SVG 组上应用 CSS 变换会导致其子文本元素在 Safari 中闪烁。其他元素的变换在 Safari 中看起来很平滑。所有子元素的变换在 Firefox 或 Chrome 中看起来很平滑。

See below videos for example.例如,请参见下面的视频。 The code is attached at the end of this post and also at https://codepen.io/xiaohk/pen/yLqOZXx .代码附在这篇文章的末尾,也附在 https://codepen.io/xiaohk/pen/yLqOZXx

Safari Safari

在此处输入图像描述

Firefox Firefox

在此处输入图像描述

Chrome铬合金

在此处输入图像描述

Attempted Workarounds尝试的解决方法

I found several related questions:我发现了几个相关的问题:

However, all the solutions do not work for my problem.但是,所有解决方案都不适用于我的问题。 For example, I tried to set -webkit-transform-style:preserve-3d;例如,我尝试设置-webkit-transform-style:preserve-3d; , -webkit-transform: translateZ(0); , -webkit-transform: translateZ(0); , and -webkit-filter: opacity(1); , 和-webkit-filter: opacity(1); . .

Any suggestions are appreciated, thank you!任何建议表示赞赏,谢谢!

 const svg = d3.select('.svg-element'); const group = svg.append('g').attr('class', 'container-group'); group.append('circle').attr('cx', 120).attr('cy', 100).attr('r', 20).style('fill', 'pink') const ys = [80, 100, 120, 140, 160]; const text = group.selectAll('text.my-text').data(ys).join('text').attr('class', 'my-text').attr('x', (_, i) => 250 + i * 10).attr('y', d => d).text('A quick and lazy fox.') svg.call( d3.zoom().extent([[0, 0], [500, 300]]).scaleExtent([1, 8]).on("zoom", zoomed) ); function zoomed({transform}) { group.attr("transform", transform.toString()); }
 .container { width: 100%; display: flex; justify-content: center; padding: 20px 0; -webkit-transform-style:preserve-3d; -webkit-filter: opacity(1); }.svg-element { width: 500px; height: 300px; border: 1px solid black; } g.container-group { -webkit-backface-visibility: hidden; } text.my-text { -webkit-transform-style:preserve-3d; -webkit-transform: translateZ(0); pointer-events: none; dominant-baseline: middle; text-anchor: middle; }
 <script src="https://d3js.org/d3.v7.min.js" charset="utf-8"></script> <div class="container"> <svg class="svg-element" width=300 height=300> </div>

Add text-rendering: geometricPrecision;添加text-rendering: geometricPrecision; that will stop Safari from adjusting the font kerning to best display the text at each scale.这将阻止 Safari 调整字体字距以在每个比例下最好地显示文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM