【问题标题】:How can I increment the rotation angle for a JS object in HTML5 canvas?如何增加 HTML5 画布中 JS 对象的旋转角度?
【发布时间】:2014-11-30 04:07:40
【问题描述】:

我的问题是红线的初始化旋转了一个等于黄线的角度...... 我只想在开始时使这个角度等于零(红线角度)。 ** 然后随着鼠标移动旋转。 .

这里是如何画线的

var x = W / 2;
var y = H / 2;
var lineLength = 900;
ctx.save();
ctx.translate(x, y);
 ctx.rotate(((0.003 * (mouse.x  - p.w))/2)) ; // how to increment the angle ??
ctx.moveTo(-lineLength / 2,0, 0);
ctx.lineTo(lineLength / 2.0, 0);
ctx.stroke();
ctx.restore();

【问题讨论】:

  • 请更好地描述您想要实现的目标。
  • 我现在将编辑帖子。
  • 感谢您的澄清 :-) 我已经发布了一个应该满足您需求的答案。此外,请务必从 context.beginPath 开始所有路径操作 - 否则您之前的所有行都将使用每个 context.stroke 命令重绘。

标签: javascript html canvas rotation html5-canvas


【解决方案1】:

代码中的一个小故障:确保每个路径绘制操作都使用 context.beginPath();

以下是围绕中点旋转一条线的方法:

    function draw(angle){
        ctx.clearRect(0,0,cw,ch);
        ctx.save();
        // draw rotated line
        ctx.translate(cx,cy);
        ctx.rotate(angle) ; 
        ctx.beginPath();
        ctx.moveTo(-lineLength/2,0)
        ctx.lineTo(lineLength/2,0);
        ctx.strokeStyle='orange';
        ctx.stroke();
        ctx.restore();
    }

示例代码和演示:http://jsfiddle.net/m1erickson/3g0yvLov/

<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>

<style>
    body{ background-color: ivory; }
    #canvas{border:1px solid red;}
</style>

<script>
$(function(){

    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;
    ctx.lineWidth=10;

    var cx=canvas.width/2;
    var cy=canvas.height/2;
    var lineLength = 90;

    $("#canvas").mousemove(function(e){handleMouseMove(e);});

    function draw(angle){
        ctx.clearRect(0,0,cw,ch);
        ctx.save();
        // draw unrotated line
        ctx.beginPath();
        ctx.moveTo(cx-lineLength/2,cy)
        ctx.lineTo(cx+lineLength/2,cy);
        ctx.strokeStyle='red';
        ctx.stroke();
        // draw rotated line
        ctx.translate(cx,cy);
        ctx.rotate(angle) ; 
        ctx.beginPath();
        ctx.moveTo(-lineLength/2,0)
        ctx.lineTo(lineLength/2,0);
        ctx.strokeStyle='orange';
        ctx.stroke();
        ctx.restore();
    }

    function handleMouseMove(e){
      e.preventDefault();
      e.stopPropagation();

      mouseX=parseInt(e.clientX-offsetX);
      mouseY=parseInt(e.clientY-offsetY);

      // Put your mousemove stuff here
      var dx=mouseX-cx;
      var dy=mouseY-cy;
      var radianAngle=Math.atan2(dy,dx);
      draw(radianAngle);

    }


}); // end $(function(){});
</script>
</head>
<body>
    <canvas id="canvas" width=300 height=300></canvas>
</body>
</html>

【讨论】:

    猜你喜欢
    • 2014-12-23
    • 1970-01-01
    • 2016-08-27
    • 2016-12-06
    • 2013-02-23
    • 1970-01-01
    • 2015-05-25
    • 2014-07-11
    • 1970-01-01
    相关资源
    最近更新 更多