【问题标题】:How to rotate the shapes in a tangram game without using `context.rotate`如何在不使用`context.rotate`的情况下旋转七巧板游戏中的形状
【发布时间】:2015-03-07 15:50:57
【问题描述】:

在画布中 600 * 600 tangle 的三个点是 (100, 100), (300, 100), (200,200) 旋转中心为 (200, 100)

我想实现拖动三角形旋转。但我不知道如何改变这些点的坐标。

ps:我知道如何计算旋转角度 var rotateRadian = Math.atan2(mouseY - center.y, mouseX - center.x) - Math.atan2(startDragPosition.y - center.y, startDragPosition.x - center.x);

[来自下方评论的其他信息]

我想搭建一个七巧板,有矩形、三角形和平行四边形。

【问题讨论】:

    标签: javascript canvas rotation


    【解决方案1】:

    你有一个好的开始!

    首先计算鼠标到中心点的角度:

    // calc mouse angle
    var dx=mouseX-centerX;
    var dy=mouseY-centerY;
    radianAngle=Math.atan2(dy,dx);
    

    既然你有一个等边三角形,你的三角形就是一个正多边形。使用三角函数计算多边形的 3 个旋转点:

    // vertex#1
    var x1=cx+radius*Math.cos(rotationAngle);
    var y1=cy+radius*Math.sin(rotationAngle);
    
    // vertex#2
    rotationAngle+=PI2/3;
    var x2=cx+radius*Math.cos(rotationAngle);
    var y2=cy+radius*Math.sin(rotationAngle);
    
    // vertex#3
    rotationAngle+=PI2/3;
    var x3=cx+radius*Math.cos(rotationAngle);
    var y3=cy+radius*Math.sin(rotationAngle);
    

    然后只需监听 mousemove 事件并相应地重绘三角形:

    var canvas=document.getElementById("canvas");
    var ctx=canvas.getContext("2d");
    var cw=canvas.width;
    var ch=canvas.height;
    var $canvas=$("#canvas");
    var canvasOffset=$canvas.offset();
    var offsetX=canvasOffset.left;
    var offsetY=canvasOffset.top;
    var scrollX=$canvas.scrollLeft();
    var scrollY=$canvas.scrollTop();
    ctx.fillStyle='red';
    ctx.strokeStyle='blue';
    ctx.lineWidth=2;
    
    var isDown=false;
    var startX;
    var startY;
    
    var PI2=Math.PI*2;
    var cx=150;
    var cy=150;
    var sideLength=50;
    var originalAngle=0;
    var radius=sideLength*Math.sqrt(3)/3;
    
    draw(200,200);
    
    $("#canvas").mousemove(function(e){handleMouseMove(e);});
    
    
    function draw(mx,my){
    
      //
      var dx=mx-cx;
      var dy=my-cy;
      var currentAngle=Math.atan2(dy,dx);
      var rotationAngle=currentAngle-originalAngle;
      // vertex#1
      var x1=cx+radius*Math.cos(rotationAngle);
      var y1=cy+radius*Math.sin(rotationAngle);
      // vertex#2
      rotationAngle+=PI2/3;
      var x2=cx+radius*Math.cos(rotationAngle);
      var y2=cy+radius*Math.sin(rotationAngle);
      // vertex#3
      rotationAngle+=PI2/3;
      var x3=cx+radius*Math.cos(rotationAngle);
      var y3=cy+radius*Math.sin(rotationAngle);
    
      ctx.clearRect(0,0,cw,ch);
      ctx.beginPath();
      ctx.moveTo(x1,y1);
      ctx.lineTo(x2,y2);
      ctx.lineTo(x3,y3);
      ctx.closePath();
      ctx.stroke();
    
      ctx.beginPath();
      ctx.arc(x1,y1,3,0,PI2);
      ctx.closePath();
      ctx.fill();
    
    }
    
    
    function handleMouseMove(e){
      // tell the browser we're handling this event
      e.preventDefault();
      e.stopPropagation();
    
      mx=parseInt(e.clientX-offsetX);
      my=parseInt(e.clientY-offsetY);
    
      draw(mx,my);
    
    }
    body{ background-color: ivory; }
    #canvas{border:1px solid red;}
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <h4>The triangle will rotate according to the mouse</h4>
    <canvas id="canvas" width=300 height=300></canvas>

    [根据提问者评论添加]

    数学类似,但更复杂,因为七巧板形状不规则。

    对于每个形状,您需要:

    找到形状的中心,即它的旋转中心

    cx=average of all x vertices. 
    cy=average of all y vertices.
    

    找出形状的 3 或 4 个径向长度

    // for each radius
    radial length=Math.sqrt(Math.pow(vx-cx,2)+Math.pow(vy-cy,2))
    

    找出形状的 3 或 4 个径向角

    // for each radius
    radial angle=Math.atan2(Math.pow(vy-cy,2),Math.pow(vx-cx,2))
    

    现在在我的答案中使用相同的三角函数来围绕质心旋转顶点

    【讨论】:

    • 很好,在你的例子中,你的三角形是一个正多边形。您可以通过“rotationAngle+=PI2/3”计算角度;但在我目前的项目中,我想构建一个七巧板,有矩形、三角形和平行四边形。我不能再使用“rotationAngle += PI2/3”了……你能帮帮我吗?
    • 真的吗?!您的问题与七巧板无关!数学是相似的,但更复杂,因为七巧板形状不规则。我已经添加到我的答案中以解释额外的数学。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多