HTML Canvas Animation Swing pendulum

Description

HTML Canvas Animation Swing pendulum

View in separate window

<!DOCTYPE HTML>
<html>
<head>
<script>
  var Animation = function(canvasId) {
    this.canvas = document.getElementById(canvasId);
    this.context = this.canvas.getContext("2d");
    this.t = 0;//from   w w  w .ja v  a  2s.c o  m
    this.timeInterval = 0;
    this.startTime = 0;
    this.lastTime = 0;
    this.frame = 0;
    this.animating = false;

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

  Animation.prototype.getContext = function() {
    return this.context;
  };

  Animation.prototype.getCanvas = function() {
    return this.canvas;
  };

  Animation.prototype.clear = function() {
    this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
  };

  Animation.prototype.setDrawStage = function(func) {
    this.drawStage = func;
  };

  Animation.prototype.getFrame = function() {
    return this.frame;
  };

  Animation.prototype.start = function() {
    this.animating = true;
    var date = new Date();
    this.startTime = date.getTime();
    this.lastTime = this.startTime;

    if (this.drawStage !== undefined) {
      this.drawStage();
    }

    this.animationLoop();
  };

  Animation.prototype.stop = function() {
    this.animating = false;
  };
  Animation.prototype.getTimeInterval = function() {
    return this.timeInterval;
  };

  Animation.prototype.getTime = function() {
    return this.t;
  };


  Animation.prototype.animationLoop = function() {
    var that = this;

    this.frame++;
    var date = new Date();
    var thisTime = date.getTime();
    this.timeInterval = thisTime - this.lastTime;
    this.t += this.timeInterval;
    this.lastTime = thisTime;

    if (this.drawStage !== undefined) {
      this.drawStage();
    }

    if (this.animating) {
      requestAnimFrame(function() {
        that.animationLoop();
      });
    }
  };
            window.onload = function(){
                var anim = new Animation("myCanvas");
                var canvas = anim.getCanvas();
                var context = anim.getContext();
                
                var amplitude = Math.PI / 4; // 45 degrees
                var period = 4000; // ms
                var theta = 0;
                var pendulumLength = 250;
                var pendulumWidth = 10;
                var rotationPointX = canvas.width / 2;
                var rotationPointY = 20;
                
                anim.setDrawStage(function(){
                    theta = (amplitude * Math.sin((2 * Math.PI * this.getTime()) / period)) + Math.PI / 2;
                    this.clear();

                    context.beginPath();
                    var endPointX = rotationPointX + (pendulumLength * Math.cos(theta));
                    var endPointY = rotationPointY + (pendulumLength * Math.sin(theta));
                    context.beginPath();
                    context.moveTo(rotationPointX, rotationPointY);
                    context.lineTo(endPointX, endPointY);
                    context.lineWidth = pendulumWidth;
                    context.strokeStyle = "black";
                    context.stroke();
                    
                    // draw bottom circle
                    context.beginPath();
                    context.arc(endPointX, endPointY, 40, 0, 2 * Math.PI, false);
                    var grd = context.createLinearGradient(endPointX - 50, endPointY - 50, endPointX + 50, endPointY + 50);
                    
                    grd.addColorStop(0, "#444");
                    grd.addColorStop(0.5, "gray");
                    grd.addColorStop(1, "#444");
                    context.fillStyle = grd;
                    context.fill();
                });
                
                anim.start();
            };
</script>
</head>
<body>
  <canvas id="myCanvas" width="600" height="350"
    style="border: 1px solid black;">
        </canvas>
</body>
</html>



PreviousNext

Related