add bezier #3 demo

This commit is contained in:
Thibaud Gasser 2024-09-26 23:36:06 +02:00
parent e49d6f1732
commit e09b3e1d73
5 changed files with 108 additions and 24 deletions

View File

@ -1,23 +1,7 @@
import { lerp, drawCircle, drawLine, drawDashedLine, resizeCanvas } from "./common.js"
import { lerp, drawCircle, drawLine, drawDashedLine, resizeCanvas, cubicBezier } from "./common.js"
const points = [];
function cubicBezier(a, b, c, d, res=0.05) {
const eps = 0.001; // to prevent issues with float comparaison (p <= 1)
const curve = [];
for (let p = 0; p - 1 < eps; p += res) {
const ab = lerp(a, b, p);
const bc = lerp(b, c, p);
const cd = lerp(c, d, p);
const abc = lerp(ab, bc, p);
const bcd = lerp(bc, cd, p);
const abcd = lerp(abc, bcd, p);
curve.push(abcd);
console.log(p);
}
return curve;
}
function init() {
const canvas = document.getElementById("canvas");
if (canvas.getContext) {

12
bezier-3.html Normal file
View File

@ -0,0 +1,12 @@
<!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>

64
bezier-3.js Normal file
View File

@ -0,0 +1,64 @@
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

@ -1,9 +1,3 @@
export function resizeCanvas(ctx) {
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
ctx.canvas.width = window.innerWidth;
ctx.canvas.height = window.innerHeight;
}
export function lerp(a, b, p) {
return {
x: a.x + (b.x - a.x) * p,
@ -11,6 +5,27 @@ export function lerp(a, b, p) {
};
}
export function cubicBezier(a, b, c, d, res=0.05) {
const eps = 0.001; // to prevent issues with float comparaison (p <= 1)
const curve = [];
for (let p = 0; p - 1 < eps; p += res) {
const ab = lerp(a, b, p);
const bc = lerp(b, c, p);
const cd = lerp(c, d, p);
const abc = lerp(ab, bc, p);
const bcd = lerp(bc, cd, p);
const abcd = lerp(abc, bcd, p);
curve.push(abcd);
}
return curve;
}
export function resizeCanvas(ctx) {
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
ctx.canvas.width = window.innerWidth;
ctx.canvas.height = window.innerHeight;
}
export function drawCircle(ctx, {x, y}, radius, color) {
ctx.save();
ctx.beginPath();

View File

@ -4,12 +4,21 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body {
background-color: black;
}
li > a {
color: white;
}
</style>
</head>
<body style="margin: 0; padding 0; overflow: hidden; background-color: #000000">
<body>
<ul>
<li><a href="/grid.html">Grid</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-3.html">Bezier demo #3</a></li>
</ul>
</body>
</html>