-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimpleWave.html
94 lines (82 loc) · 2.34 KB
/
simpleWave.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body style="margin: 0"></body>
<script>
const createCanvas = () => {
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
document.body.appendChild(canvas);
return [canvas, ctx];
};
const setCanvasSize = (canvas) => {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
};
const drawCurve = (ctx, { sx, sy, cx1, cy1, cx2, cy2, ex, ey }) => {
ctx.strokeStyle = "black";
ctx.beginPath();
ctx.moveTo(sx, sy);
ctx.bezierCurveTo(cx1, cy1, cx2, cy2, ex, ey);
ctx.stroke();
};
const updateCurves = (ctx, waves) => {
const cx1 = getWave(waves) * window.innerWidth;
const curveParam = {
sx: 0,
sy: 0,
cx1,
cy1: 0,
cx2: window.innerWidth / 2,
cy2: window.innerHeight,
ex: window.innerWidth,
ey: window.innerHeight,
};
drawCurve(ctx, curveParam);
};
const updateAnimation = (ctx, waves) => {
ctx.clearRect(0, 0, window.innerWidth, innerHeight);
updateCurves(ctx, waves);
updateWave(waves);
requestAnimationFrame(() => {
updateAnimation(ctx, waves);
});
};
const createeWaves = (len) => {
const waves = [];
for (let i = 0; i < len; i++) {
waves.push(Math.random() * 360);
}
return waves;
};
const getWave = (waves) => {
let blendedWave = waves.reduce((acc, cur) => {
return acc + Math.sin((cur / 180) * Math.PI);
}, 0);
return (blendedWave / waves.length + 1) / 2;
};
const config = {
waveSpeed: 1,
};
const updateWave = (waves) => {
waves.forEach((wave, i) => {
let r = Math.random() * (i + 1) * config.waveSpeed;
waves[i] = (wave + r) % 360;
});
};
window.onload = () => {
const [canvas, ctx] = createCanvas();
window.addEventListener("resize", () => {
setCanvasSize(canvas);
});
setCanvasSize(canvas);
const waves = createeWaves(1);
updateAnimation(ctx, waves);
};
</script>
</html>