69 lines
2.0 KiB
JavaScript
Raw Normal View History

2025-01-30 22:55:15 +01:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.lerp = lerp;
exports.quadraticBezier = quadraticBezier;
exports.cubicBezier = cubicBezier;
exports.resizeCanvas = resizeCanvas;
exports.drawCircle = drawCircle;
exports.drawLine = drawLine;
exports.drawDashedLine = drawDashedLine;
function lerp(a, b, p) {
return {
2024-09-26 22:46:28 +02:00
x: a.x + (b.x - a.x) * p,
y: a.y + (b.y - a.y) * p
};
}
2025-01-30 22:55:15 +01:00
function quadraticBezier(a, b, c, res = 0.05) {
2024-09-27 17:57:08 +02:00
const eps = 0.001; // to prevent issues with float comparaison (p <= 1)
const curve = [];
for (let p = 0; p - 1 < eps; p += res) {
2025-01-30 22:55:15 +01:00
const ab = lerp(a, b, p);
2024-09-27 17:57:08 +02:00
const bc = lerp(b, c, p);
const abc = lerp(ab, bc, p);
curve.push(abc);
}
return curve;
}
2025-01-30 22:55:15 +01:00
function cubicBezier(a, b, c, d, res = 0.05) {
2024-09-26 23:36:06 +02:00
const eps = 0.001; // to prevent issues with float comparaison (p <= 1)
const curve = [];
for (let p = 0; p - 1 < eps; p += res) {
2025-01-30 22:55:15 +01:00
const ab = lerp(a, b, p);
const bc = lerp(b, c, p);
const cd = lerp(c, d, p);
2024-09-26 23:36:06 +02:00
const abc = lerp(ab, bc, p);
const bcd = lerp(bc, cd, p);
const abcd = lerp(abc, bcd, p);
curve.push(abcd);
}
return curve;
}
2025-01-30 22:55:15 +01:00
function resizeCanvas(ctx) {
2024-09-26 23:36:06 +02:00
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
2025-01-30 22:55:15 +01:00
ctx.canvas.width = window.innerWidth;
2024-09-26 23:36:06 +02:00
ctx.canvas.height = window.innerHeight;
}
2025-01-30 22:55:15 +01:00
function drawCircle(ctx, center, radius, color) {
2024-09-26 22:46:28 +02:00
ctx.save();
ctx.beginPath();
2025-01-30 22:55:15 +01:00
ctx.arc(center.x, center.y, radius, 0, 2 * Math.PI);
ctx.strokeStyle = `#${color.toString(16)}`;
2024-09-26 22:46:28 +02:00
ctx.lineWidth = 5;
ctx.stroke();
ctx.restore();
}
2025-01-30 22:55:15 +01:00
function drawLine(ctx, start, end, color, dashed = false) {
2024-09-26 22:46:28 +02:00
ctx.save();
ctx.beginPath();
2025-01-30 22:55:15 +01:00
if (dashed)
ctx.setLineDash([5, 5]);
ctx.strokeStyle = `#${color.toString(16)}`;
2024-09-26 22:46:28 +02:00
ctx.moveTo(start.x, start.y);
ctx.lineTo(end.x, end.y);
ctx.stroke();
ctx.restore();
}
2025-01-30 22:55:15 +01:00
function drawDashedLine(ctx, start, end, color) {
2024-09-26 22:46:28 +02:00
drawLine(ctx, start, end, color, true);
}