Advertisement
monoteen

Alphavet

Nov 23rd, 2014
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 2.04 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.     <head>
  4.         <title>TODO supply a title</title>
  5.         <meta charset="UTF-8">
  6.         <script>
  7.             function nextRandomInteger(limit) {
  8.                 return Math.round(Math.random()*limit);
  9.             }
  10.             var alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  11.             function randomAlphavet(){
  12.                 return alphavet.charAt(nextRandomInteger(alphavet.length-1));
  13.             }
  14.             function randomSpeed(maxSpeed){
  15.                 return Math.random()*maxSpeed - Math.random()*maxSpeed;
  16.             }
  17.         </script>
  18.         <script>
  19.             var canvasWidth = 700;
  20.             var canvasHeight = 500;
  21.             function MovingText(){
  22.                 this.x = nextRandomInteger(canvasWidth);
  23.                 this.y = nextRandomInteger(canvasHeight);
  24.                 this.vx = randomSpeed(10);
  25.                 this.vy = randomSpeed(10);
  26.                
  27.                 this.body = document.createElement('h1');
  28.                 this.body.innerHTML = randomAlphavet();
  29.                 this.body.style.position = 'absolute';
  30.                
  31.                 document.body.appendChild(this.body);
  32.             }
  33.             MovingText.prototype.move = function(){
  34.                 if(this.x <0 || this.x > canvasWidth){this.vx *= -1;}
  35.                 if(this.y <0 || this.y > canvasHeight){this.vy *= -1;}
  36.                
  37.                 this.x += this.vx;
  38.                 this.y += this.vy;
  39.                
  40.                 this.body.style.left = this.x + 'px';
  41.                 this.body.style.top = this.y + 'px';
  42.             };
  43.         </script>
  44.         <script>
  45.             window.onload = function(){
  46.                 var movingTexts = [];
  47.                
  48.                 for(var i=0; i<100; i++) movingTexts.push(new MovingText());
  49.                
  50.                setInterval(function(){
  51.                    for(var i in movingTexts) movingTexts[i].move();
  52.                },1000/60);
  53.            };
  54.        </script>
  55.     </head>
  56.     <body>
  57.     </body>
  58. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement