A browser source overlay for winter vibes for your Live Streams or Videos
1import Config from "./config.js";
2
3/**
4 * @param {number} y
5 * @param {number} x
6 * @param {number} width
7 * @returns {SnowAccumulator}
8 */
9export function createSnowAccumulator(x, y, width, height = 0) {
10 return {
11 accumulator: 0,
12 y,
13 x,
14 width,
15 height,
16 };
17}
18
19/**
20 * @param {SnowAccumulator} sa
21 * @param {Object} item
22 * @param {number} item.y
23 * @returns {boolean}
24 */
25export function snowAccumulatorCollisionY(sa, { y }) {
26 if (y >= sa.y - sa.height) {
27 return true;
28 }
29
30 return false;
31}
32
33/**
34 * @param {SnowAccumulator} sa
35 * @param {Object} item
36 * @param {number} item.x
37 * @returns {boolean}
38 */
39export function snowAccumulatorCollisionX(sa, { x }) {
40 if (x >= sa.x && x <= sa.x + sa.width) {
41 return true;
42 }
43
44 return false;
45}
46
47/**
48 * @param {SnowAccumulator} sa
49 * @param {Object} item
50 * @param {number} item.size
51 */
52export function accumulateSnow(sa, { size }) {
53 sa.accumulator += size;
54
55 if (sa.accumulator % Config.groundAccumulator.max == 0) {
56 sa.height += 1;
57 }
58}
59
60/**
61 * @param {SnowAccumulator} sa
62 */
63export function resetSnowAccumulator(sa) {
64 sa.accumulator = 0;
65 sa.height = 0;
66}
67
68/**
69 * @param {CanvasRenderingContext2D} ctx
70 * @param {SnowAccumulator[]} accumulators
71 * @param {Plow} plow
72 */
73export function drawSnowAccumulators(ctx, accumulators, plow) {
74 const points = accumulators.map((sa) => [sa.x, sa.y - sa.height]);
75 const avgHeight =
76 accumulators.reduce((acc, sa) => acc + sa.height, 0) / accumulators.length;
77 const normalized = points
78 .map(([x, y]) => [x, y - avgHeight])
79 .filter(([x]) => !(plow && plow.direction === "left" && x >= plow.x));
80
81 ctx.beginPath();
82 normalized.forEach(([x, y]) => {
83 ctx.lineTo(x, y);
84 });
85 plow &&
86 normalized.length > 0 &&
87 ctx.lineTo(plow.x + 2, normalized[normalized.length - 1][1]);
88 plow && ctx.lineTo(plow.x, Config.canvas.height);
89 !plow &&
90 ctx.lineTo(Config.canvas.width, normalized[normalized.length - 1][1]);
91 ctx.lineTo(Config.canvas.width, Config.canvas.height);
92 ctx.lineTo(0, Config.canvas.height);
93 ctx.fillStyle = "#fff";
94 ctx.fill();
95}