A browser source overlay for winter vibes for your Live Streams or Videos

using delta time for fall speed

-1
javascript.svg
···
-
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="32" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path fill="#F7DF1E" d="M0 0h256v256H0V0Z"></path><path d="m67.312 213.932l19.59-11.856c3.78 6.701 7.218 12.371 15.465 12.371c7.905 0 12.89-3.092 12.89-15.12v-81.798h24.057v82.138c0 24.917-14.606 36.259-35.916 36.259c-19.245 0-30.416-9.967-36.087-21.996m85.07-2.576l19.588-11.341c5.157 8.421 11.859 14.607 23.715 14.607c9.969 0 16.325-4.984 16.325-11.858c0-8.248-6.53-11.17-17.528-15.98l-6.013-2.58c-17.357-7.387-28.87-16.667-28.87-36.257c0-18.044 13.747-31.792 35.228-31.792c15.294 0 26.292 5.328 34.196 19.247l-18.732 12.03c-4.125-7.389-8.591-10.31-15.465-10.31c-7.046 0-11.514 4.468-11.514 10.31c0 7.217 4.468 10.14 14.778 14.608l6.014 2.577c20.45 8.765 31.963 17.7 31.963 37.804c0 21.654-17.012 33.51-39.867 33.51c-22.339 0-36.774-10.654-43.819-24.574"></path></svg>
+2 -5
src/config.js
···
export const MAX_SIZE = 4;
// maximum speed of a falling snowflake
-
export const MAX_SPEED = 1;
+
export const MAX_SPEED = 0.1;
// minimum speed of a snowflake falling
-
export const MIN_SPEED = 0.5;
-
-
// threshold for how fast snowflakes can go and have horizontal movement
-
export const SWAY_THRESHOLD = 0.8;
+
export const MIN_SPEED = 0.08;
+18 -14
src/main.js
···
} from "./snow_accumulator";
/**
+
* @type{Array.<Snowflake>}
+
*/
+
const snowFlakes = [];
+
+
/**
+
* @type SnowAccumulator
+
*/
+
const floor = createSnowAccumulator(CANVAS_HEIGHT);
+
+
/**
* @type {HTMLCanvasElement}
*/
let canvas;
···
*/
let ctx;
-
/**
-
* @type{Array.<Snowflake>}
-
*/
-
const snowFlakes = [];
-
let lastSpawn = 0;
-
let floor = createSnowAccumulator(CANVAS_HEIGHT);
-
let lastFrame = 0;
let fps = 0;
···
}
/**
-
* @param delta {DOMHighResTimeStamp}
+
* @param time {DOMHighResTimeStamp}
*/
-
function gameLoop(delta) {
-
fps = Math.round(1 / ((delta - lastFrame) / 1000));
+
function gameLoop(time) {
+
const delta = time - lastFrame;
+
fps = Math.round(1 / (delta / 1000));
+
ctx.clearRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);
-
if (delta - lastSpawn > 500 && snowFlakes.length < MAX_PARTICAL_COUNT) {
+
if (time - lastSpawn > 500 && snowFlakes.length < MAX_PARTICAL_COUNT) {
for (let i = 0; i < Math.floor(Math.random() * 2); i++) {
snowFlakes.push(createSnowflake());
}
}
snowFlakes.forEach((flake, idx) => {
-
moveSnowflake(flake);
+
moveSnowflake(flake, delta);
if (checkCollision(floor, flake)) {
snowFlakes[idx] = createSnowflake();
}
···
drawSnowAccumulator(ctx, floor);
ctx.fillText("FPS: " + fps, 10, 30);
-
lastFrame = delta;
-
+
lastFrame = time;
window.requestAnimationFrame(gameLoop);
}
+3 -12
src/snowflake.js
···
MAX_SPEED,
MIN_SIZE,
MIN_SPEED,
-
SWAY_THRESHOLD,
} from "./config";
/**
···
/**
* @param {Snowflake} snowflake
+
* @param {number} delta
*/
-
export function moveSnowflake(snowflake) {
-
snowflake.y += snowflake.speed;
-
const sway = Math.random() < 0.5 && snowflake.speed < SWAY_THRESHOLD;
-
if (sway) {
-
const direction = Math.random() < 0.5;
-
if (direction) {
-
snowflake.x += (snowflake.speed * 5) / 10;
-
} else {
-
snowflake.x -= (snowflake.speed * 5) / 10;
-
}
-
}
+
export function moveSnowflake(snowflake, delta) {
+
snowflake.y += snowflake.speed * delta;
}