merge demo bezier #2 and #3

This commit is contained in:
Thibaud Gasser 2024-09-27 12:53:05 +02:00
parent e09b3e1d73
commit a200c3d3cf
5 changed files with 50 additions and 100 deletions

View File

@ -1,4 +1,4 @@
<!DOCTYPE html> <!DOCTYPE HTML>
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">

View File

@ -1,35 +1,62 @@
import { lerp, drawCircle, drawLine, drawDashedLine, resizeCanvas, cubicBezier } from "./common.js" import { resizeCanvas, drawCircle, drawLine, cubicBezier } from "./common.js";
function drawPoints(ctx, points) {
const [a, b, c, d] = points;
drawLine(ctx, a, b, 0xFF00FF);
drawLine(ctx, c, d, 0xFF00FF);
for (const p of points) {
drawCircle(ctx, p, 2, 0xFF00FF);
}
}
function drawCurve(ctx, curve) {
for (let i=0; i < curve.length - 1; ++i) {
drawLine(ctx, curve[i], curve[i+1], 0xFFFFFF);
}
}
const points = [];
function init() { function init() {
const canvas = document.getElementById("canvas"); const canvas = document.getElementById("canvas");
let selection = undefined;
let points = [
{ x: 227, y: 434 },
{ x: 341, y: 234 },
{ x: 649, y: 255 },
{ x: 765, y: 450 },
];
if (canvas.getContext) { if (canvas.getContext) {
const ctx = canvas.getContext("2d"); const ctx = canvas.getContext("2d");
resizeCanvas(ctx); // Init canvas to full window size resizeCanvas(ctx); // Init canvas
drawPoints(ctx, points);
const bezier = cubicBezier(...points);
drawCurve(ctx, bezier);
canvas.addEventListener("click", (evt) => { canvas.onmousedown = (evt) => {
const {clientX, clientY} = evt; const { clientX, clientY } = evt;
if (points.length < 4) { for (const p of points) {
const p = {x: clientX, y: clientY}; if (Math.abs(p.x - clientX) < 10 && Math.abs(p.y - clientY) < 10) {
points.push(p); selection = points.indexOf(p);
drawCircle(ctx, p, 1, 0xFF00FF);
if (points.length == 4) {
const [a, b, c, d] = points;
drawDashedLine(ctx, a, b, 0xFF00FF);
drawDashedLine(ctx, b, c, 0xFF00FF);
drawDashedLine(ctx, c, d, 0xFF00FF);
const curve = cubicBezier(a, b, c, d);
for (let i=0; i < curve.length - 1; ++i) {
drawLine(ctx, curve[i], curve[i+1], 0xFFFFFF);
}
} }
} }
}); };
window.addEventListener('resize', () => resizeCanvas(ctx)); 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 = cubicBezier(...points);
drawCurve(ctx, bezier);
drawPoints(ctx, points);
}
};
canvas.onmouseup = () => {
selection = undefined;
};
} }
} }

View File

@ -1,12 +0,0 @@
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body style="margin: 0; padding 0; overflow: hidden; background-color: #000000">
<canvas id="canvas" width="800" height="600"></canvas>
</body>
<script type="module" src="bezier-3.js"></script>
</html>

View File

@ -1,64 +0,0 @@
import { resizeCanvas, drawCircle, drawLine, cubicBezier } from "./common.js";
function drawPoints(ctx, points) {
const [a, b, c, d] = points;
drawLine(ctx, a, b, 0xFF00FF);
drawLine(ctx, c, d, 0xFF00FF);
for (const p of points) {
drawCircle(ctx, p, 2, 0xFF00FF);
}
}
function drawCurve(ctx, curve) {
for (let i=0; i < curve.length - 1; ++i) {
drawLine(ctx, curve[i], curve[i+1], 0xFFFFFF);
}
}
function init() {
const canvas = document.getElementById("canvas");
let selection = undefined;
let points = [
{ x: 300, y: 100 },
{ x: 400, y: 100 },
{ x: 100, y: 300 },
{ x: 300, y: 300 },
];
if (canvas.getContext) {
const ctx = canvas.getContext("2d");
resizeCanvas(ctx); // Init canvas
drawPoints(ctx, points);
const bezier = 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);
}
}
};
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 = cubicBezier(...points);
drawCurve(ctx, bezier);
drawPoints(ctx, points);
}
};
canvas.onmouseup = () => {
selection = undefined;
};
}
}
init();

View File

@ -18,7 +18,6 @@
<li><a href="/grid.html">Grid</a></li> <li><a href="/grid.html">Grid</a></li>
<li><a href="/bezier-1.html">Bezier demo #1</a></li> <li><a href="/bezier-1.html">Bezier demo #1</a></li>
<li><a href="/bezier-2.html">Bezier demo #2</a></li> <li><a href="/bezier-2.html">Bezier demo #2</a></li>
<li><a href="/bezier-3.html">Bezier demo #3</a></li>
</ul> </ul>
</body> </body>
</html> </html>