Advertisement
nikolov_k

Moving Shapes - funny :)

May 1st, 2013
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var movingShapes = (function () {
  2.     var shapes = [];
  3.     var directions = [
  4.         new direction(0, 1),
  5.         new direction(1, 0),
  6.         new direction(0, -1),
  7.         new direction(-1, 0)
  8.     ]
  9.     function generateColor() {
  10.         var red = parseInt(Math.random() * 255);
  11.         var green = parseInt(Math.random() * 255);
  12.         var blue = parseInt(Math.random() * 255);
  13.         var color = 'rgb(' + red + ',' + green + ',' + blue + ')';
  14.         return color;
  15.     }
  16.     function direction(top, left) {
  17.         this.top = top;
  18.         this.left = left;
  19.     }
  20.     function rect(startTop, startLeft) {
  21.         this.type = "rect";
  22.         this.currentTop = startTop;
  23.         this.currentLeft = startLeft;
  24.         this.moves = 0;
  25.         this.div = document.createElement('div');
  26.         this.div.style.top = startTop + 'px';
  27.         this.div.style.left = startLeft + 'px';
  28.         this.div.style.backgroundColor = generateColor();
  29.         this.div.style.color = generateColor();
  30.         this.div.style.borderColor = generateColor();
  31.         this.div.innerHTML = "text";
  32.         document.body.appendChild(this.div);
  33.     }
  34.     function ellipse(centerTop, centerLeft) {
  35.         this.radius = 150;
  36.         this.centerTop = centerTop;
  37.         this.centerLeft = centerLeft;
  38.         this.type = "ellipse";
  39.         this.angle = 0;
  40.         this.div = document.createElement('div');
  41.         this.div.style.top = (centerTop + this.radius * Math.sin(this.angle * Math.PI / 180)) + "px";
  42.         this.div.style.left = (centerLeft + this.radius * Math.cos(this.angle * Math.PI / 180)) + "px";
  43.         this.div.style.backgroundColor = generateColor();
  44.         this.div.style.color = generateColor();
  45.         this.div.style.borderColor = generateColor();
  46.         this.div.innerHTML = "text";
  47.         document.body.appendChild(this.div);
  48.     }
  49.     function moveRectangle(shape) {
  50.         currentDirection = directions[parseInt(shape.moves / 100)%4];
  51.         shape.moves += 2;
  52.         shape.currentTop += currentDirection.top;
  53.         shape.currentLeft += currentDirection.left;
  54.         shape.div.style.top = shape.currentTop + 'px';
  55.         shape.div.style.left = shape.currentLeft + 'px';
  56.     }
  57.     function moveEllipse(shape) {
  58.         shape.angle += 1;
  59.         shape.div.style.top = (shape.centerTop + shape.radius * Math.sin(shape.angle * Math.PI / 180)) + "px";
  60.         shape.div.style.left = (shape.centerLeft + shape.radius * Math.cos(shape.angle * Math.PI / 180)) + "px";
  61.     }
  62.     function add(type) {
  63.         if (type == "rect") {
  64.             var shape = new rect(parseInt(Math.random() * 800), parseInt(Math.random() * 1000));
  65.             shapes.push(shape);
  66.         } else if (type== "ellipse") {
  67.             var shape = new ellipse(parseInt(Math.random() * 800), parseInt(Math.random() * 1000));
  68.             shapes.push(shape);
  69.         }
  70.     }
  71.     var rotateAll = function rotateAll() {
  72.         if (shapes.length > 0) {
  73.             for (var i = 0; i < shapes.length; i++) {
  74.                 if (shapes[i].type == "rect") {
  75.                     moveRectangle(shapes[i]);
  76.                 } else if (shapes[i].type == "ellipse") {
  77.                     moveEllipse(shapes[i]);
  78.                 }
  79.             }
  80.         }
  81.     }
  82.     function rotate() {
  83.         setInterval(rotateAll, 20);
  84.     }
  85.  
  86.     return {
  87.         add: add,
  88.         rotate: rotate
  89.     };
  90. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement