Search Topic Here

Sunday, 29 December 2013

Oscillator


code

<!DOCTYPE HTML>
<html>
  <head>
    <style>
      body {
        margin: 0px;
        padding: 0px;
      }
    </style>
  </head>
  <body>
    <canvas id="myCanvas" width="578" height="500"></canvas>
    <script>
      window.requestAnimFrame = (function(callback) {
        return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
        function(callback) {
          window.setTimeout(callback, 1000 / 60);
        };
      })();

      function drawSpring(canvas, context) {
        context.beginPath();
        context.moveTo(0, 0);

        for(var y = 0; y < 200; y++) {
          // Sine wave equation
          var x = 30 * Math.sin(y / 9.05);
          context.lineTo(x, y);
        }
      }
      function drawWeight(canvas, context) {
        var size = 100;
        context.save();
        context.fillStyle = 'red';
        context.fillRect(-size / 2, 0, size, size);
        context.restore();
      }
      function animate(canvas, theta, lastTime) {
        var context = canvas.getContext('2d');

        // update
        var time = (new Date()).getTime();
        var timeDiff = time - lastTime;
        theta += timeDiff / 400;
        lastTime = time;

        var scale = 0.8 * (Math.sin(theta) + 1.3);

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

        // draw
        context.save();
        context.translate(canvas.width / 2, 0);

        context.save();
        context.scale(1, scale);
        drawSpring(canvas, context);
        context.restore();

        context.lineWidth = 6;
        context.strokeStyle = '#0096FF';
        // blue-ish color
        context.stroke();

        context.translate(0, 200 * scale);
        drawWeight(canvas, context);
        context.restore();

        // request new frame
        requestAnimFrame(function() {
          animate(canvas, theta, lastTime);
        });
      }
      var canvas = document.getElementById('myCanvas');
      var theta = 0;
      var time = (new Date()).getTime();
      animate(canvas, theta, time);

    </script>
  </body>
</html>

Animation With HTML5



code


<html>
  <head>
    <style>
      body {
        margin: 0px;
        padding: 0px;
      }
      #buttons {
        position: absolute;
        top: 5px;
        left: 10px;
      }
      #buttons > input {
        padding: 10px;
        display: block;
        margin-top: 5px;
      }
    </style>
  </head>
  <body>
    <div id="container"></div>
    <div id="buttons">
      <input type="button" id="start" value="Start">
      <input type="button" id="stop" value="Stop">
    </div>
    <script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v4.7.4.min.js"></script>
    <script defer="defer">
      var stage = new Kinetic.Stage({
        container: 'container',
        width: 578,
        height: 200
      });
      var layer = new Kinetic.Layer();

      var hexagon = new Kinetic.RegularPolygon({
        x: stage.getWidth() / 2,
        y: stage.getHeight() / 2,
        sides: 6,
        radius: 70,
        fill: 'red',
        stroke: 'black',
        strokeWidth: 4
      });

      layer.add(hexagon);
      stage.add(layer);

      var amplitude = 150;
      // in ms
      var period = 2000;
      var centerX = stage.getWidth() / 2;

      var anim = new Kinetic.Animation(function(frame) {
        hexagon.setX(amplitude * Math.sin(frame.time * 2 * Math.PI / period) + centerX);
      }, layer);

      document.getElementById('start').addEventListener('click', function() {
        anim.start();
      }, false);
      
      document.getElementById('stop').addEventListener('click', function() {
        anim.stop();
      }, false);
    </script>
  </body

How to create a professional website using HTML, PHP

create a professional website