js-canvas-playground/bezier-2.js

59 lines
1.9 KiB
JavaScript
Raw Normal View History

2025-01-30 22:55:15 +01:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const common_js_1 = require("./common.js");
2024-09-27 12:53:05 +02:00
function drawPoints(ctx, points) {
const [a, b, c, d] = points;
2025-01-30 22:55:15 +01:00
(0, common_js_1.drawLine)(ctx, a, b, 0xFF00FF);
(0, common_js_1.drawLine)(ctx, c, d, 0xFF00FF);
2024-09-27 12:53:05 +02:00
for (const p of points) {
2025-01-30 22:55:15 +01:00
(0, common_js_1.drawCircle)(ctx, p, 2, 0xFF00FF);
2024-09-27 12:53:05 +02:00
}
}
function drawCurve(ctx, curve) {
2025-01-30 22:55:15 +01:00
for (let i = 0; i < curve.length - 1; ++i) {
(0, common_js_1.drawLine)(ctx, curve[i], curve[i + 1], 0xFFFFFF);
2024-09-27 12:53:05 +02:00
}
}
2024-09-26 22:46:28 +02:00
function init() {
2025-01-30 22:55:15 +01:00
const canvas = document.getElementById("game");
if (!canvas)
throw new Error("unable to get canvas HTML element");
const ctx = canvas.getContext("2d");
if (!ctx)
throw new Error("unable to get canvas 2D context");
2024-09-27 12:53:05 +02:00
let selection = undefined;
let points = [
{ x: 227, y: 434 },
{ x: 341, y: 234 },
{ x: 649, y: 255 },
{ x: 765, y: 450 },
];
2025-01-30 22:55:15 +01:00
(0, common_js_1.resizeCanvas)(ctx); // Init canvas
drawPoints(ctx, points);
const bezier = (0, common_js_1.cubicBezier)(...points);
drawCurve(ctx, bezier);
canvas.onmousedown = (evt) => {
const { clientX, clientY } = evt;
for (const p of points) {
if (Math.abs(p.x - clientX) < 10 && Math.abs(p.y - clientY) < 10) {
selection = points.indexOf(p);
2024-09-26 22:46:28 +02:00
}
2025-01-30 22:55:15 +01:00
}
};
canvas.onmousemove = (evt) => {
if (selection !== undefined) {
points[selection].x = evt.clientX;
points[selection].y = evt.clientY;
// redraw
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
const bezier = (0, common_js_1.cubicBezier)(...points);
drawCurve(ctx, bezier);
drawPoints(ctx, points);
}
};
canvas.onmouseup = () => {
selection = undefined;
};
2024-09-26 22:46:28 +02:00
}
init();