Reputation: 33
I am trying to rotate the image about its center. But in Safari, the top left corner of the SVG, it's origin is always taken as the center for rotation.
var rotate_val= 30;
function rotate(val){
rotate_val = rotate_val + val;
var transform = "translate(0, 0) scale(1, 1) rotate("+rotate_val+ ")";
var svg = d3.select("g").transition()
.style('-webkit-transform-origin', '50% 50%')
.style('transform-origin', 'center')
.duration(1200)
.attrTween("transform",function(interpolate) {
return d3.interpolate(transform, transform);
});
}
<body>
<button onclick="rotate(30)">Rotate + 30</button>
<button onclick="rotate(-30)">Rotate - 30</button>
<div id = "svgcontainer" style="border: 1px solid">
<svg width = "100%" height = "100%" viewBox="0 0 750 400">
<g>
<image x="325" y="150" xlink:href="image.png" height="100" width="100"/>
</g>
</svg>
</div>
</body>
https://jsfiddle.net/ztw2omgb/
Upvotes: 2
Views: 3233
Reputation: 1863
According to the MDN docs for transform-origin, the status of transform-origin
for SVG
is Compatibility Unknown. So it may not be implemented yet in Safari.
However, if you remove the transform-origin
attributes completely, you can use the second and third arguments of the rotate()
function to define the center of rotation for your selected element. e.g.
Your image's x
, and y
values are 325 and 150 respectively, and the width and height are both 100. So the center of rotation should be the x
and y
values plus half the width and height (50) giving 375 and 200.
function rotate(val){
rotate_val = rotate_val + val;
// note the rotate function now contains second and third argument,
// which specify the center of rotation.
var transform = "translate(0, 0) scale(1, 1) rotate("+rotate_val+ ", 375, 200)";
d3.select("g").transition()
.duration(1200)
.attrTween("transform",function(interpolate) {
return d3.interpolate(transform, transform);
});
}
Upvotes: 10