gpt4 book ai didi

javascript - 如何在鼠标移动事件后围绕其中心旋转 Canvas 对象?

转载 作者:行者123 更新时间:2023-11-29 16:49:07 24 4
gpt4 key购买 nike

您好,当我移动鼠标时,我想围绕它的中心旋转这个形状,但目前它是围绕 (0, 0) 旋转的。如何更改我的代码?

源代码(另见 jsfiddle ):

var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

class Circle {
constructor(options) {
this.cx = options.x;
this.cy = options.y;
this.radius = options.radius;
this.color = options.color;
this.angle = 0;
this.toAngle = this.angle;

this.binding();
}

binding() {
const self = this;
window.addEventListener('mousemove', (e) => {
self.update(e.clientX, e.clientY);
});
}

update(nx, ny) {
this.toAngle = Math.atan2(ny - this.cy, nx - this.cx);
}

render() {
ctx.clearRect(0,0,canvas.width,canvas.height);

ctx.save();

ctx.beginPath();

ctx.lineWidth = 1;

if (this.toAngle !== this.angle) {
ctx.rotate(this.toAngle - this.angle);
}

ctx.strokeStyle = this.color;
ctx.arc(this.cx, this.cy, this.radius, 0, Math.PI * 2);

ctx.stroke();
ctx.closePath();

ctx.beginPath();

ctx.fillStyle = 'black';
ctx.fillRect(this.cx - this.radius / 4, this.cy - this.radius / 4, 20, 20);

ctx.closePath();
ctx.restore();
}
}

var rotatingCircle = new Circle({
x: 150,
y: 100,
radius: 40,
color: 'black'
});

function animate() {
rotatingCircle.render();
requestAnimationFrame(animate);
}

animate();

最佳答案

所有好的答案,令人沮丧的是没有......他们没有提到解决方案只有在当前转换处于默认状态时才有效。他们没有提到如何恢复到默认状态以及保存和恢复状态。

获取默认转换状态

ctx.setTransform(1,0,0,1,0,0);

保存和恢复所有状态

ctx.save();
ctx.transform(10,0,0,2,200,100); // set some transform state
ctx.globalAlpha = 0.4;
ctx.restore(); // each save must be followed by a restore at some point

它们可以嵌套

ctx.save();  // save default state
ctx.globalAlpha = 0.4;
ctx.save(); // save state with alpha = 0.4
ctx.transform(10,0,0,2,200,100); // set some transform state
ctx.restore(); // restore to alpha at 0.4
ctx.restore(); // restore to default.

setTransform 完全替换了当前的转换。同时变换、缩放、旋转、平移、将现有变换与适当的变换相乘。如果您有一个对象附加到另一个对象,并且希望将第一个对象的转换应用于第二个对象,并将其他转换应用于第二个对象而不是第一个对象,这将很方便。

ctx.rotate(Math.PI /2); // Rotates everything 90 clockwise
ctx.rotate(Math.PI /2); // Rotates everything another 90 clockwise so that
// everything is 180 from default

ctx.translate(1,1); // move diagonally down by 1. Origin is now at 1,1
ctx.translate(1,1); // move diagonally down by 1. Origin is now at 2,2
ctx.translate(1,1); // move diagonally down by 1. Origin is now at 3,3
ctx.translate(1,1); // move diagonally down by 1. Origin is now at 4,4

ctx.scale(2,2); // scale by 2 everything twice as big
ctx.scale(2,2); // scale by 2 everything four times as big

还有一个不需要 ctx 的默认转换状态的替代方案

// scaleX, scaleY are scales along axis x,y
// posX, posY is position of center point
// rotate is in radians clockwise with 0 representing the x axis across the screen
// image is an image to draw.
ctx.setTransform(scaleX,0,0,scaleY, posX, posY);
ctx.rotate(rotate);
ctx.drawImage(image,-image.width / 2, -image.height / 2);

或者如果不是图像而是对象

ctx.setTransform(scaleX,0,0,scaleY, posX, posY);
ctx.rotate(rotate);
ctx.translate(-object.width / 2, -object.height / 2);

关于javascript - 如何在鼠标移动事件后围绕其中心旋转 Canvas 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37914999/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com