[英]How to rotate a span around the center of an image?
I have a canvas with an image: 我有一个带图像的画布:
On the right side of the picture you can see a white square with a refresh icon a little bit hidden by the black circle. 在图片的右侧,您可以看到一个带有刷新图标的白色正方形,有点被黑色圆圈隐藏。 I created this like that:
我创建了这样的:
var rotatorSpan = document.createElement("span");
rotatorSpan.id = _this.idRotator;
rotatorSpan.className = "glyphicons glyphicons-restart";
document.getElementById("canvas-overlay").appendChild(rotatorSpan);
In my draw function: 在我的绘图功能:
this.drawRotator = function() {
var pivotX = _this.x + (_this.imageWidth / 2);
var pivotY = _this.y + (_this.imageHeight / 2);
var radius = Math.sqrt(Math.pow((_this.x - pivotX), 2) + Math.pow((_this.y - pivotY), 2));
var rotatorX = _this.x + (_this.imageWidth / 2) + radius;
var rotatorY = _this.y + (_this.imageHeight / 2) + (_this.indicatorRadius / 2);
_this.ctx.save();
_this.ctx.translate(pivotX, pivotY);
_this.ctx.rotate(_this.rotation);
_this.ctx.translate(-pivotX, -pivotY);
_this.ctx.beginPath();
_this.ctx.arc(rotatorX, rotatorY, this.indicatorRadius, 0, Math.PI * 2, false);
_this.ctx.closePath();
_this.ctx.fill();
_this.ctx.restore();
document.getElementById(_this.idRotator).style.position = "absolute";
document.getElementById(_this.idRotator).style.top = rotatorY + "px";
document.getElementById(_this.idRotator).style.left = rotatorX + "px";
document.getElementById(_this.idRotator).style.fontSize = "1em";
document.getElementById(_this.idRotator).style.backgroundColor = "#fff";
document.getElementById(_this.idRotator).style.border = "1px solid #000;";
document.getElementById(_this.idRotator).style.zIndex = 1000;
document.getElementById(_this.idRotator).style.transform = "rotate(" + _this.rotation + "rad)";
};
Now when i rotate my picture: 现在当我旋转我的照片时:
you can see that everything is rotating except the white square with the refresh icon. 你可以看到一切都在旋转,除了带有刷新图标的白色方块。 It just rotates around its own center point.
它只围绕自己的中心点旋转。
The Question 问题
How can i rotate that white square (the span element i created) around the center point of my image? 如何围绕图像的中心点旋转白色方块(我创建的跨度元素)?
You can use transform-origin
to set center of rotation. 您可以使用
transform-origin
设置旋转中心。 Here is a working demo. 这是一个工作演示。 Note that this property may not work in older versions of web browsers transform origin
请注意,此属性可能无法在旧版本的Web浏览器转换原点中使用
@-webkit-keyframes rotating /* Safari and Chrome */ { from { -ms-transform: rotate(0deg); -moz-transform: rotate(0deg); -webkit-transform: rotate(0deg); -o-transform: rotate(0deg); transform: rotate(0deg); } to { -ms-transform: rotate(360deg); -moz-transform: rotate(360deg); -webkit-transform: rotate(360deg); -o-transform: rotate(360deg); transform: rotate(360deg); } } @keyframes rotating { from { -ms-transform: rotate(0deg); -moz-transform: rotate(0deg); -webkit-transform: rotate(0deg); -o-transform: rotate(0deg); transform: rotate(0deg); } to { -ms-transform: rotate(360deg); -moz-transform: rotate(360deg); -webkit-transform: rotate(360deg); -o-transform: rotate(360deg); transform: rotate(360deg); } } .square{ background:black; width:30px; height:30px; -webkit-animation: rotating 2s linear infinite; -moz-animation: rotating 2s linear infinite; -ms-animation: rotating 2s linear infinite; -o-animation: rotating 2s linear infinite; animation: rotating 2s linear infinite; -webkit-transform-origin: 100px 100px; }
<div class="square"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.