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

纽奎

嗨,我想在移动鼠标时围绕其中心旋转此形状,但是当前它正在围绕(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();
用户名

你需要:

  • 首先平移到旋转点(枢轴)
  • 然后旋转
  • 然后:
    • 答:使用(-width / 2,-height / 2)作为相对坐标在(0,0)处绘制(对于居中的工程图)
    • B:向后平移并使用对象的绝对位置,并减去居中图形的相对坐标

修改后的代码:

ctx.beginPath();
ctx.lineWidth = 1;

ctx.translate(this.cx, this.cy);               // translate to pivot

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

ctx.strokeStyle = this.color;
ctx.arc(0, 0, this.radius, 0, Math.PI * 2);    // render at pivot

ctx.closePath();                               // must come before stroke() btw.
ctx.stroke();

ctx.beginPath();
ctx.fillStyle = 'black';
ctx.fillRect(-this.radius / 4, -this.radius / 4, 20, 20);  // render at pivot

改良的小提琴

温馨提示:您当前正在使用save()/restore()调用来维护转换矩阵。另一种方法是使用初始替换save()/ restore()-的绝对值设置矩阵,而不是第一个translate()

ctx.setTranform(1,0,0,1,this.cx, this.cy);    // translate to pivot

您还可以针对每个样式分别设置样式。无论如何,它不会改变核心解决方案。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何围绕其中心旋转PShape?

来自分类Dev

如何围绕自己的中心旋转 SVG 对象?

来自分类Dev

围绕鼠标旋转多个对象

来自分类Dev

当对象最初由其中心旋转时,围绕该点旋转对象

来自分类Dev

SVG动画围绕其中心旋转组

来自分类Dev

LibGDX 1.5围绕其中心旋转的精灵

来自分类Dev

围绕其中心旋转绘制的文本

来自分类Dev

[AS3]围绕其中心旋转雪碧

来自分类Dev

围绕其中心旋转多边形

来自分类Dev

如何在matlab中围绕对象中心旋转图像?

来自分类Dev

PDFlib - 以任意度数围绕中心旋转对象

来自分类Dev

旋转对象后如何调整鼠标位置?

来自分类Dev

基于包括对角线在内的鼠标移动,围绕点旋转对象

来自分类Dev

如何使用scipy.ndimage.interpolation.affine_transform围绕其中心旋转图像?

来自分类Dev

d3.js:如何在翻译时围绕其中心旋转SVG?

来自分类Dev

如何以支持自动渐变的方式围绕其中心旋转PyTorch图像张量?

来自分类Dev

如何使用scipy.ndimage.interpolation.affine_transform围绕其中心旋转图像?

来自分类Dev

如何围绕选择中心旋转选择的QGraphicsItems?

来自分类Dev

如何围绕选择中心旋转选择的QGraphicsItems?

来自分类Dev

如何围绕图像中心旋转跨度?

来自分类Dev

如何围绕椭圆中心旋转ArcSegment

来自分类Dev

JavaFX如何围绕它的中心旋转绘图

来自分类Dev

围绕其中心点旋转UIImageView吗?

来自分类Dev

围绕其中心旋转自定义图像

来自分类Dev

围绕其中心旋转SCNText节点(Swift-Scenekit)

来自分类Dev

使用CABasicAnimation围绕其中心旋转CAShapeLayer弧

来自分类Dev

围绕其中心旋转UIView并保持其大小

来自分类Dev

SFML-使用“变换”围绕其中心旋转精灵

来自分类Dev

EaselJS-围绕其中心旋转形状

Related 相关文章

  1. 1

    如何围绕其中心旋转PShape?

  2. 2

    如何围绕自己的中心旋转 SVG 对象?

  3. 3

    围绕鼠标旋转多个对象

  4. 4

    当对象最初由其中心旋转时,围绕该点旋转对象

  5. 5

    SVG动画围绕其中心旋转组

  6. 6

    LibGDX 1.5围绕其中心旋转的精灵

  7. 7

    围绕其中心旋转绘制的文本

  8. 8

    [AS3]围绕其中心旋转雪碧

  9. 9

    围绕其中心旋转多边形

  10. 10

    如何在matlab中围绕对象中心旋转图像?

  11. 11

    PDFlib - 以任意度数围绕中心旋转对象

  12. 12

    旋转对象后如何调整鼠标位置?

  13. 13

    基于包括对角线在内的鼠标移动,围绕点旋转对象

  14. 14

    如何使用scipy.ndimage.interpolation.affine_transform围绕其中心旋转图像?

  15. 15

    d3.js:如何在翻译时围绕其中心旋转SVG?

  16. 16

    如何以支持自动渐变的方式围绕其中心旋转PyTorch图像张量?

  17. 17

    如何使用scipy.ndimage.interpolation.affine_transform围绕其中心旋转图像?

  18. 18

    如何围绕选择中心旋转选择的QGraphicsItems?

  19. 19

    如何围绕选择中心旋转选择的QGraphicsItems?

  20. 20

    如何围绕图像中心旋转跨度?

  21. 21

    如何围绕椭圆中心旋转ArcSegment

  22. 22

    JavaFX如何围绕它的中心旋转绘图

  23. 23

    围绕其中心点旋转UIImageView吗?

  24. 24

    围绕其中心旋转自定义图像

  25. 25

    围绕其中心旋转SCNText节点(Swift-Scenekit)

  26. 26

    使用CABasicAnimation围绕其中心旋转CAShapeLayer弧

  27. 27

    围绕其中心旋转UIView并保持其大小

  28. 28

    SFML-使用“变换”围绕其中心旋转精灵

  29. 29

    EaselJS-围绕其中心旋转形状

热门标签

归档