【问题标题】:Html5, JavaScript, Canvas Animations? [closed]Html5、JavaScript、画布动画? [关闭]
【发布时间】:2012-04-27 09:14:26
【问题描述】:

我的手表“秒针”图像需要每秒移动一次。如果我使用画布,我会每秒删除一次画布,因此加载手表照片和“秒针”照片会很难看。

我需要帮助。

【问题讨论】:

  • 那就不要把手表照片放在画布上,把画布画在上面。
  • 我没有写代码,我只是有想法,无法实现,真的需要帮助。

标签: javascript html animation canvas timing


【解决方案1】:

一个(有点)简单的方法是使用两个画布。



这是关于 w3schools 的精彩教程:
http://www.w3schools.com/canvas/canvas_clock_start.asp

以下是链接中的代码。

<!DOCTYPE html>
<html>
<body>

<canvas id="canvas" width="400" height="400"
style="background-color:#333">
</canvas>

<script>
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var radius = canvas.height / 2;
ctx.translate(radius, radius);
radius = radius * 0.90
setInterval(drawClock, 1000);

function drawClock() {
  drawFace(ctx, radius);
  drawNumbers(ctx, radius);
  drawTime(ctx, radius);
}

function drawFace(ctx, radius) {
  var grad;
  ctx.beginPath();
  ctx.arc(0, 0, radius, 0, 2*Math.PI);
  ctx.fillStyle = 'white';
  ctx.fill();
  grad = ctx.createRadialGradient(0,0,radius*0.95, 0,0,radius*1.05);
  grad.addColorStop(0, '#333');
  grad.addColorStop(0.5, 'white');
  grad.addColorStop(1, '#333');
  ctx.strokeStyle = grad;
  ctx.lineWidth = radius*0.1;
  ctx.stroke();
  ctx.beginPath();
  ctx.arc(0, 0, radius*0.1, 0, 2*Math.PI);
  ctx.fillStyle = '#333';
  ctx.fill();
}

function drawNumbers(ctx, radius) {
  var ang;
  var num;
  ctx.font = radius*0.15 + "px arial";
  ctx.textBaseline="middle";
  ctx.textAlign="center";
  for(num = 1; num < 13; num++){
    ang = num * Math.PI / 6;
    ctx.rotate(ang);
    ctx.translate(0, -radius*0.85);
    ctx.rotate(-ang);
    ctx.fillText(num.toString(), 0, 0);
    ctx.rotate(ang);
    ctx.translate(0, radius*0.85);
    ctx.rotate(-ang);
  }
}

function drawTime(ctx, radius){
    var now = new Date();
    var hour = now.getHours();
    var minute = now.getMinutes();
    var second = now.getSeconds();
    //hour
    hour=hour%12;
    hour=(hour*Math.PI/6)+
    (minute*Math.PI/(6*60))+
    (second*Math.PI/(360*60));
    drawHand(ctx, hour, radius*0.5, radius*0.07);
    //minute
    minute=(minute*Math.PI/30)+(second*Math.PI/(30*60));
    drawHand(ctx, minute, radius*0.8, radius*0.07);
    // second
    second=(second*Math.PI/30);
    drawHand(ctx, second, radius*0.9, radius*0.02);
}

function drawHand(ctx, pos, length, width) {
    ctx.beginPath();
    ctx.lineWidth = width;
    ctx.lineCap = "round";
    ctx.moveTo(0,0);
    ctx.rotate(pos);
    ctx.lineTo(0, -length);
    ctx.stroke();
    ctx.rotate(-pos);
}
</script>

</body>
</html>

【讨论】:

    【解决方案2】:

    您需要使用图层...仅绘制正在移动的部分。 这个地址有一个很棒的教程: http://arc.id.au/CanvasLayers.html

    来自本网站的引述:

    传统的动画技术是使用一堆透明图层覆盖背景图像,每个要设置动画的项目都绘制在单独的图层上。只有每帧发生变化的图层会被重绘,其他图层保持不变。图形引擎可以在不需要用户代码的情况下,从底层中挑选出要重绘的暴露区域。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-16
      • 2012-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-11
      • 2013-10-12
      • 2012-05-15
      相关资源
      最近更新 更多