我有一个星号图标的最小SVG图像:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100">
<defs>
<style>
.a {
fill: red;
transform-box: fill-box;
transform-origin: center;
}
</style>
</defs>
<!-- x = 45 because it's 10px wide and centered around x=50, so x0 == 45, x1 == 55 -->
<rect class="a" x="45" y="0" width="10" height="100"/>
<rect class="a" x="45" y="0" width="10" height="100" transform="rotate(-45)"/>
<rect class="a" x="45" y="0" width="10" height="100" transform="rotate( 45)"/>
<rect class="a" x="45" y="0" width="10" height="100" transform="rotate(-90)"/>
</svg>
这可以在Firefox 75和Chrome 80中正确显示,但在iOS或macOS上无法正确显示:
但是在Safari上,与每个元素transform-origin
的transform=""
属性相比,似乎被忽略或乱序应用<rect>
:
有人告诉我它在macOS Safari上呈现相同的损坏图像(我现在无法立即访问macOS Safari,因此无法从此处发布屏幕截图,抱歉)。
I've searched around Google to see if Safari/WebKit is lacking support for transform-box
, transform-origin
or transform="rotate(deg)"
but as far as I can tell they've all been fully supported by Safari for years - so I don't understand why it's applying the transformations out-of-order and causing the broken rendering.
After fiddling with the SVG in Safari, Safari's Web Inspector does seem to recognize the transform-origin
SVG style property, but it doesn't actually use it when applying transformations, which is weird (e.g. using transform-origin: center;
or transform-origin: 0px 0px
or transform-origin: 50px 50px
had no effect) - so a fix lies in changing the rotation centre using some other means.
As far as I can tell, macOS Safari and iOS Safari both only use transform-origin
for CSS-based transformations using the transform
property and not when using SVG attribute-based transformations using transform=""
- whereas Blink and Gecko both use transform-origin
for both attribute-based and CSS-based transformations.
translate
stepOne approach is to add a translate
step so the centre of rotation is centred in the canvas, then perform the rotation, then undo the translate
step:
transform="translate( 50, 50 ) rotate(45) translate( -50, -50 )"
Like so:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" width="100" height="100">
<rect fill="red" x="45" y="0" width="10" height="100" />
<rect fill="blue" x="45" y="0" width="10" height="100" transform="translate(50,50) rotate(-45) translate(-50,-50)" />
<rect fill="green" x="45" y="0" width="10" height="100" transform="translate(50,50) rotate( 45) translate(-50,-50)" />
<rect fill="orange" x="45" y="0" width="10" height="100" transform="translate(50,50) rotate(-90) translate(-50,-50)" />
</svg>
rotate( angle, x, y )
:我看到转换功能支持使用其他参数指定旋转中心,而不是translate
在transform
链上应用初始步骤,因此可以在Safari,Chrome(+ Edge + Opera)和Firefox中使用:rotate
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100" width="100" height="100">
<rect fill="red" x="45" y="0" width="10" height="100" />
<rect fill="blue" x="45" y="0" width="10" height="100" transform="rotate(-45, 50, 50)" />
<rect fill="green" x="45" y="0" width="10" height="100" transform="rotate( 45, 50, 50)" />
<rect fill="orange" x="45" y="0" width="10" height="100" transform="rotate(-90, 50, 50)" />
</svg>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句