0

我一直在遵循各种指南,并为一个简单的动画准备了基本代码,但在我的一生中,我无法让画布正确重置。clearRect() 函数运行良好,但是当我立即尝试再次绘制弧线时,它会再次绘制所有路径的总和,而不仅仅是绘制圆的单个段。

有人可以告诉我我在这里做错了什么,我知道这是一个简单的解决方案!简而言之,我希望下面的测试页面产生一种旋转段,而不仅仅是画一个圆圈:

<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">

</head>
<body>
<span id="degrees_output" style="display:block;width:60px"></span>
<button onclick="continue_animation=false;">Stop</button>
<canvas id="myCanvas" width="578" height="200"></canvas>
<script>
  var degrees = 0;
  var continue_animation = true;
  var canvas = document.getElementById('myCanvas');
  var context = canvas.getContext('2d');
  var output = document.getElementById('degrees_output');      

  window.requestAnimFrame = (function(callback) {
    return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
    function(callback) {
      window.setTimeout(callback, 1000 / 60);
    };
  })();

  function animate() {        
    // update
    output.innerHTML = degrees;

    var radians = (degrees / 180) * Math.PI;

    if (degrees >= 360)
        degrees = 0;
    else
        degrees += 1;

    // clear
    if (degrees % 20 == 0)
    {           
        context.clearRect(0, 0, 578, 200);

    }

    context.beginPath();

    // setup the line style     
    context.strokeStyle = '#fa00ff';
    context.lineWidth = 5;
    context.lineCap = 'round';


    context.arc(50, 50, 20, 0, radians, false);

    // colour the path
    context.stroke();

    context.closePath();                


    // request new frame
    requestAnimFrame(function() {
        if (continue_animation)
            animate();
    });
  }
  animate();

</script>

4

1 回答 1

0

如果您想要一个固定长度的弧线环绕您的圆圈,您需要在 context.arc 中指定起始角度和结束角度,如下所示:

context.arc(50, 50, 20, startRadians, radians, false);

startRadians 可以是“弧度”值后面的任何角度:

var startRadians= (degrees-45) * Math.PI/180;

并且,是的,在将弧线绘制到新位置之前,您需要清除画布(至少是前一个弧线):

context.clearRect(0,0,canvas.width,canvas.height);

这是代码和小提琴:http: //jsfiddle.net/m1erickson/VFhzX/

<!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 degrees = 0;
    var continue_animation = true;
    var canvas = document.getElementById('myCanvas');
    var context = canvas.getContext('2d');
    var output = document.getElementById('degrees_output');      

    window.requestAnimFrame = (function(callback) {
      return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
      function(callback) {
        window.setTimeout(callback, 1000 / 60);
      };
    })();

    function animate() {        
      // update
      output.innerHTML = degrees;

      var radians = (degrees / 180) * Math.PI;
      var startRadians= (degrees-45) * Math.PI/180;

      if (degrees >= 360)
          degrees = 0;
      else
          degrees += 1;

      // clear
      if (degrees % 20 == 0)
      {           
          context.clearRect(0, 0, 578, 200);

      }

      context.clearRect(0,0,canvas.width,canvas.height);

      context.beginPath();

      // setup the line style     
      context.strokeStyle = '#fa00ff';
      context.lineWidth = 5;
      context.lineCap = 'round';


      context.arc(50, 50, 20, startRadians, radians, false);

      // colour the path
      context.stroke();

      context.closePath();                


      // request new frame
      requestAnimFrame(function() {
          if (continue_animation)
              animate();
      });
    }
    animate();



}); // end $(function(){});
</script>

</head>

<body>
    <span id="degrees_output" style="display:block;width:60px"></span>
    <button onclick="continue_animation=false;">Stop</button>
    <canvas id="myCanvas" width="578" height="200"></canvas>
</body>
</html>
于 2013-04-15T18:30:41.997 回答