Mediawiki:common.js

Вернуться

Строка 1: Строка 1:
/* Снег */
/* Снег */
(function(){
(function(){
     var canvas = document.querySelector('canvas');
     var canvas = document.createElement('canvas');
     if(!canvas) return;
     canvas.style.position = 'fixed';
    canvas.style.left = '0';
    canvas.style.top = '0';
    canvas.style.width = '100%';
    canvas.style.height = '100%';
    canvas.style.pointerEvents = 'none';
    canvas.style.zIndex = '9999';
    document.body.appendChild(canvas);


     var ctx = canvas.getContext('2d');
     var ctx = canvas.getContext('2d');
     var score = 0;
     var w = 0, h = 0, flakes = [], t = 0;
    var caughtFlakes = [];


     canvas.addEventListener('mousemove', function(e){
     function rand(a,b){ return Math.random()*(b-a)+a; }
        var mx = e.clientX;
        var my = e.clientY;


        for(var i=0;i<flakes.length;i++){
    function initFlakes(){
            var f = flakes[i];
        flakes = [];
            var dx = mx - f.x;
        var count = Math.max(40, Math.floor(w/20));
            var dy = my - f.y;
        for(var i=0; i<count; i++){
            if(Math.sqrt(dx*dx+dy*dy) < f.r*5){
            flakes.push({
                caughtFlakes.push(f);
                 x: rand(0,w),
                score++;
                y: rand(-h,h),
                 flakes[i] = {x: Math.random()*canvas.width, y: -10, r: f.r, vx: f.vx, vy: f.vy, s: f.s};
                r: rand(1,4),
             }
                vx: rand(-0.3,0.3),
                vy: rand(0.5,1.2),
                swing: rand(0,Math.PI*2),
                swingSpeed: rand(0.01,0.03)
             });
         }
         }
     });
     }
 
    function resize(){
        w = canvas.width = window.innerWidth;
        h = canvas.height = window.innerHeight;
        initFlakes();
    }


     var oldLoop = loop;
     var raf = window.requestAnimationFrame || function(fn){ setTimeout(fn,16); };
     loop = function(){
 
         t += 0.003;
     function loop(){
         ctx.clearRect(0,0,canvas.width,canvas.height);
         t += 0.01;
         ctx.clearRect(0,0,w,h);
        ctx.fillStyle = 'rgba(255,255,255,0.85)';
         ctx.beginPath();
         ctx.beginPath();
         for(var i=0;i<flakes.length;i++){
         for(var i=0; i<flakes.length; i++){
             var f = flakes[i];
             var f = flakes[i];
             f.x += f.vx + Math.cos(t+f.s)*0.2;
            f.swing += f.swingSpeed;
             f.y += f.vy + Math.sin(t+f.s)*0.1;
             f.x += f.vx + Math.sin(f.swing)*0.5;
             if(f.y > canvas.height+10){ f.y = -10; f.x = Math.random()*canvas.width; }
             f.y += f.vy;
             if(f.x < -20 || f.x > canvas.width+20){ f.x = Math.random()*canvas.width; f.y = -10; }
 
             if(f.y > h + 5){
                f.y = -5;
                f.x = rand(0,w);
            }
             if(f.x < -10) f.x = w+10;
            if(f.x > w+10) f.x = -10;


             ctx.moveTo(f.x,f.y);
             ctx.moveTo(f.x,f.y);
             ctx.arc(f.x,f.y,f.r,0,6.283);
             ctx.arc(f.x,f.y,f.r,0,2*Math.PI);
         }
         }
        ctx.fillStyle = 'rgba(255,255,255,0.85)';
         ctx.fill();
         ctx.fill();
        raf(loop);
    }


        ctx.font = "20px Arial";
    window.addEventListener('resize', resize);
        ctx.fillStyle = "red";
    resize();
        ctx.fillText("Счёт: "+score, 10, 30);
     loop();
 
        raf(loop);
     };
})();
})();