File size: 3,737 Bytes
f2b0008 | 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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 | self.onmessage = (e) => {
switch (e.data.type) {
case "createCanvas": createCanvas(e.data);
break;
case "initCanvas": initCanvas(e.data.footerOffsetHeight, e.data.clientWidth, e.data.clientHeight);
break;
case "startAnimation": startAnimation();
break;
case "onShareModeChange": onShareModeChange(e.data.active);
break;
case "switchAnimation": switchAnimation(e.data.animate);
break;
}
};
let baseColorNormal;
let baseColorShareMode;
let baseOpacityNormal;
let baseOpacityShareMode;
let speed;
let fps;
let c;
let cCtx;
let x0, y0, w, h, dw, offset;
let startTime;
let animate = true;
let currentFrame = 0;
let lastFrame;
let baseColor;
let baseOpacity;
function createCanvas(data) {
baseColorNormal = data.baseColorNormal;
baseColorShareMode = data.baseColorShareMode;
baseOpacityNormal = data.baseOpacityNormal;
baseOpacityShareMode = data.baseOpacityShareMode;
speed = data.speed;
fps = data.fps;
c = data.canvas;
cCtx = c.getContext("2d");
lastFrame = fps / speed - 1;
baseColor = baseColorNormal;
baseOpacity = baseOpacityNormal;
}
function initCanvas(footerOffsetHeight, clientWidth, clientHeight) {
let oldW = w;
let oldH = h;
let oldOffset = offset;
w = clientWidth;
h = clientHeight;
offset = footerOffsetHeight - 28;
if (oldW === w && oldH === h && oldOffset === offset) return; // nothing has changed
c.width = w;
c.height = h;
x0 = w / 2;
y0 = h - offset;
dw = Math.round(Math.min(Math.max(0.6 * w, h)) / 10);
drawFrame(currentFrame);
}
function startAnimation() {
startTime = Date.now();
animateBg();
}
function switchAnimation(state) {
if (!animate && state) {
// animation starts again. Set startTime to specific value to prevent frame jump
startTime = Date.now() - 1000 * currentFrame / fps;
}
animate = state;
requestAnimationFrame(animateBg);
}
function onShareModeChange(active) {
baseColor = active ? baseColorShareMode : baseColorNormal;
baseOpacity = active ? baseOpacityShareMode : baseOpacityNormal;
drawFrame(currentFrame);
}
function drawCircle(ctx, radius) {
ctx.lineWidth = 2;
let opacity = Math.max(0, baseOpacity * (1 - 1.2 * radius / Math.max(w, h)));
if (radius > dw * 7) {
opacity *= (8 * dw - radius) / dw
}
if (ctx.setStrokeColor) {
// older blink/webkit based browsers do not understand opacity in strokeStyle. Use deprecated setStrokeColor instead
// https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D/strokeStyle#webkitblink-specific_note
ctx.setStrokeColor("grey", opacity);
}
else {
ctx.strokeStyle = `rgb(${baseColor} / ${opacity})`;
}
ctx.beginPath();
ctx.arc(x0, y0, radius, 0, 2 * Math.PI);
ctx.stroke();
}
function drawCircles(ctx, frame) {
ctx.clearRect(0, 0, w, h);
for (let i = 7; i >= 0; i--) {
drawCircle(ctx, dw * i + speed * dw * frame / fps + 33);
}
}
function drawFrame(frame) {
cCtx.clearRect(0, 0, w, h);
drawCircles(cCtx, frame);
}
function animateBg() {
let now = Date.now();
if (!animate && currentFrame === lastFrame) {
// Animation stopped and cycle finished -> stop drawing frames
return;
}
let timeSinceLastFullCycle = (now - startTime) % (1000 / speed);
let nextFrame = Math.trunc(fps * timeSinceLastFullCycle / 1000);
// Only draw frame if it differs from current frame
if (nextFrame !== currentFrame) {
drawFrame(nextFrame);
currentFrame = nextFrame;
}
requestAnimationFrame(animateBg);
} |