Advertisement
Guest User

Moving particles

a guest
May 4th, 2013
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 5.44 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3.     <head>
  4.         <title>Quarks and atoms</title>
  5.         <style>
  6.             body {
  7.                 background-color: black;
  8.             }
  9.             #wrapper {
  10.                 width: 45%;
  11.                 height: 100%;
  12.                 float: left;
  13.             }
  14.             .atom, .quant{
  15.                 border: 5px solid darkgreen;
  16.                 width: 30px;
  17.                 height: 30px;
  18.                 position: absolute;
  19.                 text-align: center;
  20.                 color: white;
  21.                 line-height:1.9em;
  22.                 font-style: italic;
  23.                 text-indent: -5px;
  24.             }
  25.             .atom {
  26.                 border-radius: 20px;
  27.             }
  28.         </style>
  29.     </head>
  30.     <body>
  31.         <button id="Button1" onclick = "addElement('atom')">Add electron</button>
  32.         <button id="add_atom" onclick = "addElement('quant')">Add quant</button>
  33.         <div id="wrapper">
  34.             <div class="atom">
  35.                 ∈
  36.             </div>
  37.             <div class="quant">
  38.                 Q
  39.             </div>
  40.         </div>
  41.     </body>
  42.     <script>
  43.         var TWO_PI = Math.PI * 2;
  44.         var circleX = window.innerWidth/4;
  45.         var circleY = window.innerHeight/2;
  46.         var circleR = 150;
  47.         var TriangleX = (window.innerWidth / 4) * 2;
  48.         var TriangleY = (window.innerHeight / 2) + 150;
  49.  
  50.         var stepAngle = 0.025;
  51.         var animationSpeed = 10;
  52.         var atom = [];
  53.         var quant = [];
  54.         var wrapper = document.getElementById("wrapper");
  55.  
  56.         atom.push({
  57.             element: document.querySelectorAll(".atom")[0],
  58.             angle: 0
  59.         });
  60.         quant.push({
  61.             element: document.querySelectorAll(".quant")[0],
  62.             posX: TriangleX,
  63.             posY: TriangleY,
  64.             direction: "up",
  65.         });
  66.  
  67.         (function runMotion(){
  68.             setInterval("moveAtoms()", animationSpeed);
  69.             setInterval("moveQuants()", animationSpeed);
  70.         })();
  71.  
  72.         function moveAtoms() {
  73.             for (var i = 0; i < atom.length; i++) {
  74.                if (atom[i].angle < TWO_PI) {
  75.                    atom[i].angle += stepAngle;
  76.                }
  77.                else {
  78.                    atom[i].angle = 0;
  79.                }
  80.                var newX = circleX + Math.cos(atom[i].angle) * circleR;
  81.                var newY = circleY + Math.sin(atom[i].angle) * circleR;
  82.  
  83.                setPos.posX(atom[i].element, newX);
  84.                setPos.posY(atom[i].element, newY);
  85.            }
  86.        }
  87.  
  88.        function moveQuants() {
  89.            for (var i = 0; i < quant.length; i++) {
  90.                if (quant[i].direction === "up") {
  91.                    quant[i].posY -= 2;
  92.                    if (quant[i].posY <= TriangleY - 300) {
  93.                        quant[i].direction = "right";
  94.                    }
  95.                }
  96.                if (quant[i].direction === "right") {
  97.                    quant[i].posX += 2;
  98.                    console.log(quant[i].posX);
  99.                    if (quant[i].posX >= TriangleX + 200) {
  100.                         quant[i].direction = "down";
  101.                     }
  102.                 }
  103.                 else if (quant[i].direction === "down") {
  104.                     quant[i].posY += 2;
  105.                     if (quant[i].posY >= TriangleY) {
  106.                         quant[i].direction = "left";
  107.                     }
  108.                 }
  109.                 else if (quant[i].direction === "left") {
  110.                     quant[i].posX -= 2;
  111.                     if (quant[i].posX <= TriangleX) {
  112.                        quant[i].direction = "up";
  113.                    }
  114.                }
  115.  
  116.                setPos.posX(quant[i].element, quant[i].posX);
  117.                setPos.posY(quant[i].element, quant[i].posY);
  118.            }
  119.        }
  120.  
  121.        function addElement(type){
  122.            var newElement = document.createElement("div");
  123.  
  124.            newElement.className = type;
  125.            console.log();
  126.            newElement.style.backgroundColor = randomCssColor();
  127.            newElement.style.borderColor = randomCssColor();
  128.            newElement.style.color = randomCssColor();
  129.            newElement.innerText = type === 'atom' ? '∈' : 'Q';
  130.            wrapper.appendChild(newElement);
  131.  
  132.            if (type === 'atom') {
  133.                atom.push({
  134.                    element: newElement,
  135.                    angle: 0,
  136.                });
  137.            }
  138.            else {
  139.                quant.push({
  140.                    element: newElement,
  141.                    posX: TriangleX,
  142.                    posY: TriangleY,
  143.                    direction: "up",
  144.                });
  145.            }
  146.        }
  147.  
  148.        function randomCssColor() {
  149.            var red = parseInt(Math.random() * 255);
  150.            var green = parseInt(Math.random() * 255);
  151.            var blue = parseInt(Math.random() * 255);
  152.            var colors = red + "," + green + "," + blue;
  153.            return "rgb(" + colors + ")";
  154.        }
  155.  
  156.        var setPos = (function () {
  157.            
  158.            function posX(obj, pos) {
  159.                obj.style.left = pos + "px";
  160.            }
  161.            function posY(obj, pos) {
  162.                obj.style.top = pos + "px";
  163.            }
  164.  
  165.            return {
  166.                posX: posX,
  167.                posY: posY,
  168.            }
  169.        })();
  170.  
  171.    </script>
  172. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement