Difference between revisions of "Template:Vilnius-Lithuania/JS/WaveScript"

m
 
(3 intermediate revisions by 2 users not shown)
Line 2: Line 2:
 
  *      Have an animWave div with an svg with a animWavePath path inside
 
  *      Have an animWave div with an svg with a animWavePath path inside
 
  *          (<div id="animWave><svg width="100%" height="100%" version="1.1" xmlns="http://www.w3.org/2000/svg"><path id="animWavePath" d="" fill="#fafafc"/></svg></div>)
 
  *          (<div id="animWave><svg width="100%" height="100%" version="1.1" xmlns="http://www.w3.org/2000/svg"><path id="animWavePath" d="" fill="#fafafc"/></svg></div>)
  *      Have a setWaveParams function which returns a waveParams object (width, height, waveWidth, waveHeight, waveDelta, speed, wavePoints)
+
  *      call the makeWave function with element and function which returns waveParams object (width, height, waveWidth, waveHeight, waveDelta, speed, wavePoints)
 
  */
 
  */
var wave;
+
//var wave;
var waveParams;
+
var waveParams = {};
  
 
function initializeWaveAnimation(el) {
 
function initializeWaveAnimation(el) {
waveParams = setWaveParams();
+
    el.style.backgroundImage = "unset";
el.style.backgroundImage = "unset";
+
    var wave = el.querySelector('.animWavePath');
  
//    wave = document.getElementById("animWavePath");
+
    let myWaveParams = waveParams[el.id];
wave = document.querySelectorAll('.animWavePath');
+
  
function calculateWavePoints(factor) {
+
    function calculateWavePoints(factor) {
let points = [];
+
        myWaveParams = waveParams[el.id];
 +
        let points = [];
  
for (let i = 0, n = waveParams.wavePoints; i <= n; i++) {
+
        for (let i = 0, n = myWaveParams.wavePoints; i <= n; i++) {
let x = i / n * waveParams.waveWidth;
+
            let x = i / n * myWaveParams.waveWidth;
let sinSeed = (factor + (i + i % n)) * waveParams.speed * 100;
+
            let sinSeed = (factor + (i + i % n)) * myWaveParams.speed * 100;
let sinHeight = Math.sin(sinSeed / 100) * waveParams.waveDelta;
+
            let sinHeight = Math.sin(sinSeed / 100) * myWaveParams.waveDelta;
let yPos = Math.sin(sinSeed / 100) * sinHeight + waveParams.waveHeight;
+
            let yPos = Math.sin(sinSeed / 100) * sinHeight + myWaveParams.waveHeight;
points.push({
+
            points.push({
x: x,
+
                x: x,
y: yPos
+
                y: yPos
});
+
            });
}
+
        }
  
return points;
+
        return points;
}
+
    }
  
function buildPath(points) {
+
    function buildPath(points) {
let SVGString = "M " + points[0].x + " " + points[0].y;
+
        let SVGString = "M " + points[0].x + " " + points[0].y;
  
let cp0 = {
+
        let cp0 = {
x: (points[1].x - points[0].x) / 2,
+
            x: (points[1].x - points[0].x) / 2,
y: (points[1].y - points[0].y) + points[0].y + (points[1].y - points[0].y)
+
            y: (points[1].y - points[0].y) + points[0].y + (points[1].y - points[0].y)
};
+
        };
  
SVGString += " C " + cp0.x + " " + cp0.y + " " + cp0.x + " " + cp0.y + " " + points[1].x + " " + points[1].y;
+
        SVGString += " C " + cp0.x + " " + cp0.y + " " + cp0.x + " " + cp0.y + " " + points[1].x + " " + points[1].y;
  
let prevCp = cp0;
+
        let prevCp = cp0;
let inverted = -1;
+
        let inverted = -1;
  
for (let i = 1; i < points.length - 1; i++) {
+
        for (let i = 1; i < points.length - 1; i++) {
// let cpLength = Math.sqrt(prevCp.x * prevCp.x + prevCp.y * prevCp.y);
+
            // let cpLength = Math.sqrt(prevCp.x * prevCp.x + prevCp.y * prevCp.y);
let cp1 = {
+
            let cp1 = {
x: (points[i].x - prevCp.x) + points[i].x,
+
                x: (points[i].x - prevCp.x) + points[i].x,
y: (points[i].y - prevCp.y) + points[i].y
+
                y: (points[i].y - prevCp.y) + points[i].y
};
+
            };
  
SVGString += " C " + cp1.x + " " + cp1.y + " " + cp1.x + " " + cp1.y + " " + points[i + 1].x + " " + points[i + 1].y;
+
            SVGString += " C " + cp1.x + " " + cp1.y + " " + cp1.x + " " + cp1.y + " " + points[i + 1].x + " " + points[i + 1].y;
prevCp = cp1;
+
            prevCp = cp1;
inverted = -inverted;
+
            inverted = -inverted;
};
+
        };
  
SVGString += " L " + waveParams.width + " " + waveParams.height;
+
        SVGString += " L " + myWaveParams.width + " " + myWaveParams.height;
SVGString += " L 0 " + waveParams.height + " Z";
+
        SVGString += " L 0 " + myWaveParams.height + " Z";
return SVGString;
+
        return SVGString;
}
+
    }
  
let lastUpdate = window.Date.now();
+
    let lastUpdate = window.Date.now();
let totalTime = 0;
+
    let totalTime = 0;
  
function tick(timeElapsed) {
+
    function tick(timeElapsed) {
totalTime += (timeElapsed - lastUpdate) / 1000;
+
        totalTime += (timeElapsed - lastUpdate) / 1000;
lastUpdate = timeElapsed;
+
        lastUpdate = timeElapsed;
  
let factor = totalTime * Math.PI;
+
        let factor = totalTime * Math.PI;
wave.forEach(function (e) {
+
        wave.setAttribute("d", buildPath(calculateWavePoints(factor)));
e.setAttribute("d", buildPath(calculateWavePoints(factor)));
+
})
+
  
  
window.requestAnimationFrame(tick);
+
        window.requestAnimationFrame(tick);
};
+
    };
tick(performance.now());
+
    tick(performance.now());
 
}
 
}
  
 
function setWaveParams_default() {
 
function setWaveParams_default() {
let container = document.body;
+
    let container = document.body;
return {
+
    return {
width: container.offsetWidth,
+
        width: container.offsetWidth,
height: container.offsetHeight,
+
        height: container.offsetHeight,
waveWidth: container.offsetWidth,
+
        waveWidth: container.offsetWidth,
waveHeight: window.innerHeight,
+
        waveHeight: window.innerHeight,
waveDelta: 15,
+
        waveDelta: 15,
speed: 0.15,
+
        speed: 0.15,
wavePoints: 6,
+
        wavePoints: 6,
};
+
    };
 
}
 
}
  
 +
function setRandomParams() {
 +
    return {
 +
        waveDelta: Math.floor(Math.random() * 20) + 10,
 +
        speed: Math.random() + 0.05,
 +
        wavePoints: Math.floor(Math.random() * 5) + 2,
 +
    }
 +
}
  
let animElement = document.getElementById("animWave");
+
function makeWave(element, setWaveParamsFunction = setWaveParams_default){
if (animElement != null) {
+
    waveParams[element.id] = setWaveParamsFunction();
if (window.setWaveParams == null) {
+
    initializeWaveAnimation(element);
var setWaveParams = setWaveParams_default;
+
    window.addEventListener("resize", () => {
}
+
        waveParams[element.id] = setWaveParamsFunction();
initializeWaveAnimation(animElement);
+
    });
window.addEventListener("resize", () => {
+
    window.addEventListener('load', () => {
waveParams = setWaveParams();
+
        waveParams[element.id] = setWaveParamsFunction();
});
+
    })
 
}
 
}

Latest revision as of 18:42, 20 October 2020

/* Prerequisites:

*      Have an animWave div with an svg with a animWavePath path inside
* (
<svg width="100%" height="100%" version="1.1" xmlns="http://www.w3.org/2000/svg"><path id="animWavePath" d="" fill="#fafafc"/></svg>
)
*      call the makeWave function with element and function which returns waveParams object (width, height, waveWidth, waveHeight, waveDelta, speed, wavePoints)
*/

//var wave; var waveParams = {};

function initializeWaveAnimation(el) {

   el.style.backgroundImage = "unset";
   var wave = el.querySelector('.animWavePath');
   let myWaveParams = waveParams[el.id];
   function calculateWavePoints(factor) {
       myWaveParams = waveParams[el.id];
       let points = [];
       for (let i = 0, n = myWaveParams.wavePoints; i <= n; i++) {
           let x = i / n * myWaveParams.waveWidth;
           let sinSeed = (factor + (i + i % n)) * myWaveParams.speed * 100;
           let sinHeight = Math.sin(sinSeed / 100) * myWaveParams.waveDelta;
           let yPos = Math.sin(sinSeed / 100) * sinHeight + myWaveParams.waveHeight;
           points.push({
               x: x,
               y: yPos
           });
       }
       return points;
   }
   function buildPath(points) {
       let SVGString = "M " + points[0].x + " " + points[0].y;
       let cp0 = {
           x: (points[1].x - points[0].x) / 2,
           y: (points[1].y - points[0].y) + points[0].y + (points[1].y - points[0].y)
       };
       SVGString += " C " + cp0.x + " " + cp0.y + " " + cp0.x + " " + cp0.y + " " + points[1].x + " " + points[1].y;
       let prevCp = cp0;
       let inverted = -1;
       for (let i = 1; i < points.length - 1; i++) {
           // let cpLength = Math.sqrt(prevCp.x * prevCp.x + prevCp.y * prevCp.y);
           let cp1 = {
               x: (points[i].x - prevCp.x) + points[i].x,
               y: (points[i].y - prevCp.y) + points[i].y
           };
           SVGString += " C " + cp1.x + " " + cp1.y + " " + cp1.x + " " + cp1.y + " " + points[i + 1].x + " " + points[i + 1].y;
           prevCp = cp1;
           inverted = -inverted;
       };
       SVGString += " L " + myWaveParams.width + " " + myWaveParams.height;
       SVGString += " L 0 " + myWaveParams.height + " Z";
       return SVGString;
   }
   let lastUpdate = window.Date.now();
   let totalTime = 0;
   function tick(timeElapsed) {
       totalTime += (timeElapsed - lastUpdate) / 1000;
       lastUpdate = timeElapsed;
       let factor = totalTime * Math.PI;
       wave.setAttribute("d", buildPath(calculateWavePoints(factor)));


       window.requestAnimationFrame(tick);
   };
   tick(performance.now());

}

function setWaveParams_default() {

   let container = document.body;
   return {
       width: container.offsetWidth,
       height: container.offsetHeight,
       waveWidth: container.offsetWidth,
       waveHeight: window.innerHeight,
       waveDelta: 15,
       speed: 0.15,
       wavePoints: 6,
   };

}

function setRandomParams() {

   return {
       waveDelta: Math.floor(Math.random() * 20) + 10,
       speed: Math.random() + 0.05,
       wavePoints: Math.floor(Math.random() * 5) + 2,
   }

}

function makeWave(element, setWaveParamsFunction = setWaveParams_default){

   waveParams[element.id] = setWaveParamsFunction();
   initializeWaveAnimation(element);
   window.addEventListener("resize", () => {
       waveParams[element.id] = setWaveParamsFunction();
   });
   window.addEventListener('load', () => {
       waveParams[element.id] = setWaveParamsFunction();
   })

}