JSFiddle - React, Tailwind, and code Playground
HTML
<canvas id="myCanvas" width="800" height="200" style="border: 1px solid black;"></canvas>
<div id="result" style="margin-top: 20px;">N/A</div>
<script>
var canvas = document.getElementById('myCanvas');
var ctx = canvas.getContext('2d');
var resultDiv = document.getElementById('result');
// Function to flip the y-coordinate
function flipY(y) {
return canvas.height - y;
}
// Class to represent a single Bézier curve block
class BezierBlock {
constructor(P0, P1, P2, P3) {
this.P0 = { x: P0.x, y: flipY(P0.y) }; // Start point
this.P1 = { x: P1.x, y: flipY(P1.y) }; // First control point
this.P2 = { x: P2.x, y: flipY(P2.y) }; // Second control point
this.P3 = { x: P3.x, y: flipY(P3.y) }; // End point
this.minX = Math.min(this.P0.x, this.P3.x);
this.maxX = Math.max(this.P0.x, this.P3.x);
}
draw() {
// Draw the cubic Bézier curve
ctx.setLineDash([]);
ctx.beginPath();
ctx.moveTo(this.P0.x, this.P0.y);
ctx.bezierCurveTo(this.P1.x, this.P1.y, this.P2.x, this.P2.y, this.P3.x, this.P3.y);
ctx.strokeStyle = 'black';
ctx.stroke();
// Draw the vertical cursor line at the current slider position
ctx.setLineDash([5, 5]);
ctx.beginPath();
ctx.moveTo(currentX, 0);
ctx.lineTo(currentX, canvas.height);
ctx.strokeStyle = 'blue';
ctx.stroke();
ctx.setLineDash([]);
// Draw the control points
ctx.fillStyle = 'red';
ctx.fillRect(this.P0.x - 3, this.P0.y - 3, 6, 6); // P0
ctx.fillStyle = 'blue';
ctx.fillRect(this.P1.x - 3, this.P1.y - 3, 6, 6); // P1
ctx.fillStyle = 'blue';
ctx.fillRect(this.P2.x - 3, this.P2.y - 3, 6, 6); // P2
ctx.fillStyle = 'red';
...