Difference between revisions of "Template:Cologne-Duesseldorf/particles"

Line 1: Line 1:
 
(function() {
 
(function() {
   var particlesQuantity = 100,
+
   var particlesQuantity = 300,
    velocity = 0.3,
+
      velocity = 1,
    maxRadius = 20;
+
      maxRadius = 4,
 
+
      matrixField = 50,
   var canvas = document.getElementById("canvas"),
+
      matrixVel = 10;
    context,
+
 
    particles = [];
+
   var canvas = document.getElementById('canvas'),
 
+
      context,
 +
      particles = [],
 +
      mouse = {x:0,y:0};
 +
 
   if (canvas && canvas.getContext) {
 
   if (canvas && canvas.getContext) {
     context = canvas.getContext("2d");
+
     context = canvas.getContext('2d');
 
+
   
     for (var i = 0; i < particlesQuantity; i++) {
+
     for (var i=0; i < particlesQuantity; i++) {
       var alpha = Math.random();
+
       var gray = Math.round(Math.random()*255);
 
+
     
 
       particles.push({
 
       particles.push({
         x: Math.round(Math.random() * window.innerWidth),
+
         x: Math.round(Math.random()*window.innerWidth),
         y: Math.round(Math.random() * window.innerHeight),
+
         y: Math.round(Math.random()*window.innerHeight),
         velx: Math.random() * velocity * 2 - velocity,
+
         velx: Math.random()*velocity*2 - velocity,
         vely: Math.random() * velocity * 2 - velocity,
+
         vely: Math.random()*velocity*2 - velocity,
         radius: (1 - alpha) * maxRadius,
+
         radius: Math.round(Math.random()*maxRadius),
         color: "rgba(" + [20, 147, 117, alpha].join(",") + ")"
+
         color: 'rgb(' + [gray,gray,gray].join(',') + ')'
 
       });
 
       });
    }
+
    }
 +
 
 
     initialize();
 
     initialize();
 
   }
 
   }
 
+
 
 
   function resizeCanvas() {
 
   function resizeCanvas() {
 
     canvas.width = window.innerWidth;
 
     canvas.width = window.innerWidth;
 
     canvas.height = window.innerHeight;
 
     canvas.height = window.innerHeight;
 
   }
 
   }
 
+
 
 +
  function mouseMove(e) {
 +
    mouse.x = e.layerX;
 +
    mouse.y = e.layerY;
 +
  }
 +
 
 
   function render() {
 
   function render() {
     context.clearRect(0, 0, window.innerWidth, window.innerHeight);
+
     context.clearRect(0,0,window.innerWidth,window.innerHeight);
 
+
   
     var particle, len = particles.length;
+
     var particle,
 
+
        len = particles.length;
     for (var i = 0; i < len; i++) {
+
   
 +
     for (var i=0; i < len; i++) {
 
       particle = particles[i];
 
       particle = particles[i];
       if (particle.x < 0) {
+
     
        particle.velx = velocity + Math.random();
+
       if (!particle.frozen) {
      } else if (particle.x > window.innerWidth) {
+
        if (particle.x < 0) {
        particle.velx = -velocity - Math.random();
+
          particle.velx = velocity + Math.random();
      }
+
        } else if (particle.x > window.innerWidth) {
 
+
          particle.velx = -velocity - Math.random();
      if (particle.y < 0) {
+
        }  
        particle.vely = velocity + Math.random();
+
       
      } else if (particle.y > window.innerHeight) {
+
        if (particle.y < 0) {
        particle.vely = -velocity - Math.random();
+
          particle.vely = velocity + Math.random();
 +
        } else if (particle.y > window.innerHeight) {
 +
          particle.vely = -velocity - Math.random();
 +
        }
 +
       
 +
        var distance = distanceBetween(mouse,particle);
 +
        if (distance < matrixField) {
 +
          particle.x += particle.velx/matrixVel;
 +
          particle.y += particle.vely/matrixVel;
 +
          } else {
 +
          particle.x += particle.velx;
 +
          particle.y += particle.vely;
 +
        }
 
       }
 
       }
      particle.x += particle.velx;
+
   
      particle.y += particle.vely;
+
 
+
 
+
 
       context.fillStyle = particle.color;
 
       context.fillStyle = particle.color;
 
       context.beginPath();
 
       context.beginPath();
       context.arc(
+
       context.arc(particle.x,particle.y,particle.radius,0,Math.PI*2,true);
        particle.x,
+
       //context.lineWidth = 2;
        particle.y,
+
      //context.stroke();
        particle.radius,
+
        0,
+
        Math.PI * 2,
+
        true
+
       );
+
 
       context.closePath();
 
       context.closePath();
 
       context.fill();
 
       context.fill();
 
     }
 
     }
 
+
   
 
     context.save();
 
     context.save();
     context.restore();
+
    context.beginPath();
 +
    context.arc(mouse.x,mouse.y,matrixField*2,0,Math.PI*2,false);
 +
    context.clip();
 +
   
 +
    context.beginPath();
 +
    context.globalAlpha = 0.25;
 +
    context.arc(mouse.x,mouse.y,matrixField,0,Math.PI*2,false);
 +
    var grd = context.createRadialGradient(mouse.x, mouse.y, matrixField/5, mouse.x, mouse.y, matrixField);
 +
    grd.addColorStop(0, '#ffffff');
 +
    grd.addColorStop(1, '#808080');
 +
    context.fillStyle = grd;
 +
    context.shadowColor = 'black';
 +
    context.shadowBlur = 10;
 +
    context.shadowOffsetX = 0;
 +
    context.shadowOffsetY = 0;
 +
    context.fill();
 +
   
 +
     context.restore();  
 
   }
 
   }
 
+
 
   function distanceBetween(a, b) {
+
   function mouseIn() {
     var dx = Math.pow(a.x - b.x, 2),
+
     var closestIndex = 0,
       dy = Math.pow(a.y - b.y, 2);
+
        closestDistance = 1000,
     return Math.sqrt(dx + dy);
+
        len = particles.length;
 +
   
 +
    for (var i=0; i < len; i++) {
 +
       var thisDistance = distanceBetween(particles[i],mouse);
 +
      if (thisDistance < closestDistance) {
 +
        closestDistance = thisDistance;
 +
        closestIndex = i;
 +
      }
 +
     }
 +
   
 +
    if (closestDistance < particles[closestIndex].radius) {
 +
      particles[closestDistance].frozen = true;
 +
    }
 +
   
 
   }
 
   }
 
+
 
 +
  function distanceBetween(a,b) {
 +
    var dx = Math.pow(a.x - b.x,2),
 +
        dy = Math.pow(a.y - b.y,2);
 +
    return Math.sqrt(dx+dy);
 +
  }
 +
 
 
   function initialize() {
 
   function initialize() {
     window.addEventListener("resize", resizeCanvas, false);
+
    canvas.addEventListener('mousemove',mouseMove,false);
     window.requestAnimFrame = (function() {
+
     window.addEventListener('resize',resizeCanvas,false);
       return (
+
    window.addEventListener('mousein',mouseIn,false);
        window.requestAnimationFrame ||
+
     window.requestAnimFrame = (function(){
        window.webkitRequestAnimationFrame ||
+
       return window.requestAnimationFrame       ||
        window.mozRequestAnimationFrame ||
+
              window.webkitRequestAnimationFrame ||
        window.oRequestAnimationFrame ||
+
              window.mozRequestAnimationFrame   ||
        window.msRequestAnimationFrame ||
+
              window.oRequestAnimationFrame     ||
        function(callback) {
+
              window.msRequestAnimationFrame     ||
          window.setTimeout(callback, 1000 / 60);
+
              function( callback ){
        }
+
                window.setTimeout(callback, 1000 / 60);
      );
+
              };
 
     })();
 
     })();
 
+
   
     (function animloop() {
+
     (function animloop(){
 
       requestAnimFrame(animloop);
 
       requestAnimFrame(animloop);
 
       render();
 
       render();
 
     })();
 
     })();
 
+
   
 
     resizeCanvas();
 
     resizeCanvas();
 
   }
 
   }
 
})();
 
})();

Revision as of 18:21, 31 October 2017

(function() {

 var particlesQuantity = 300,
     velocity = 1,
     maxRadius = 4,
     matrixField = 50,
     matrixVel = 10;
 
 var canvas = document.getElementById('canvas'),
     context,
     particles = [],
     mouse = {x:0,y:0};

 if (canvas && canvas.getContext) {
   context = canvas.getContext('2d');
   
   for (var i=0; i < particlesQuantity; i++) {
     var gray = Math.round(Math.random()*255);
     
     particles.push({
       x: Math.round(Math.random()*window.innerWidth),
       y: Math.round(Math.random()*window.innerHeight),
       velx: Math.random()*velocity*2 - velocity,
       vely: Math.random()*velocity*2 - velocity,
       radius: Math.round(Math.random()*maxRadius),
       color: 'rgb(' + [gray,gray,gray].join(',') + ')'
     });
    }
 
   initialize();
 }
 
 function resizeCanvas() {
   canvas.width = window.innerWidth;
   canvas.height = window.innerHeight;
 }
 
 function mouseMove(e) {
   mouse.x = e.layerX;
   mouse.y = e.layerY;
 }
 
 function render() {
   context.clearRect(0,0,window.innerWidth,window.innerHeight);
   
   var particle,
       len = particles.length;
   
   for (var i=0; i < len; i++) {
     particle = particles[i];
     
     if (!particle.frozen) {
       if (particle.x < 0) {
         particle.velx = velocity + Math.random();
       } else if (particle.x > window.innerWidth) {
         particle.velx = -velocity - Math.random();
       } 
       
       if (particle.y < 0) {
         particle.vely = velocity + Math.random();
       } else if (particle.y > window.innerHeight) {
         particle.vely = -velocity - Math.random();
       }
       
       var distance = distanceBetween(mouse,particle);
       if (distance < matrixField) {
         particle.x += particle.velx/matrixVel;
         particle.y += particle.vely/matrixVel;
         } else {
         particle.x += particle.velx;
         particle.y += particle.vely;
       }
     }
    
     context.fillStyle = particle.color;
     context.beginPath();
     context.arc(particle.x,particle.y,particle.radius,0,Math.PI*2,true);
     //context.lineWidth = 2;
     //context.stroke();
     context.closePath();
     context.fill();
   }
   
   context.save();
   context.beginPath();
   context.arc(mouse.x,mouse.y,matrixField*2,0,Math.PI*2,false);
   context.clip();
   
   context.beginPath();
   context.globalAlpha = 0.25;
   context.arc(mouse.x,mouse.y,matrixField,0,Math.PI*2,false);
   var grd = context.createRadialGradient(mouse.x, mouse.y, matrixField/5, mouse.x, mouse.y, matrixField);
   grd.addColorStop(0, '#ffffff');
   grd.addColorStop(1, '#808080');
   context.fillStyle = grd;
   context.shadowColor = 'black';
   context.shadowBlur = 10;
   context.shadowOffsetX = 0;
   context.shadowOffsetY = 0;
   context.fill();
   
   context.restore();    
 }
 
 function mouseIn() {
   var closestIndex = 0,
       closestDistance = 1000, 
       len = particles.length;
   
   for (var i=0; i < len; i++) {
     var thisDistance = distanceBetween(particles[i],mouse);
     if (thisDistance < closestDistance) {
       closestDistance = thisDistance;
       closestIndex = i;
     }
   }
   
   if (closestDistance < particles[closestIndex].radius) {
     particles[closestDistance].frozen = true;
   }
   
 }
 
 function distanceBetween(a,b) {
   var dx = Math.pow(a.x - b.x,2),
       dy = Math.pow(a.y - b.y,2);
   return Math.sqrt(dx+dy);
 }
 
 function initialize() {
   canvas.addEventListener('mousemove',mouseMove,false);
   window.addEventListener('resize',resizeCanvas,false);
   window.addEventListener('mousein',mouseIn,false);
   window.requestAnimFrame = (function(){
     return  window.requestAnimationFrame       ||
             window.webkitRequestAnimationFrame ||
             window.mozRequestAnimationFrame    ||
             window.oRequestAnimationFrame      ||
             window.msRequestAnimationFrame     ||
             function( callback ){
               window.setTimeout(callback, 1000 / 60);
             };
   })();
   
   (function animloop(){
     requestAnimFrame(animloop);
     render();
   })();
   
   resizeCanvas();
 }

})();