Let's say I have a bezier curve produced from the following code:
const ctx = document.querySelector('canvas').getContext('2d');
ctx.beginPath();
ctx.moveTo(50, 20);
ctx.quadraticCurveTo(230, 30, 50, 100);
ctx.stroke();
<canvas></canvas>
Is there a way to only draw, say, the last 90% of it?
For my application I want to "consume" the curve, and create an animation where a circle moves along the line path, eating the curve along the way.
The only thing I could think of was to instead of drawing the curve using the quadraticCurveTo function, to instead calculate a huge list of points manually through the following function:
t = 0.5; // given example value
x = (1 - t) * (1 - t) * p[0].x + 2 * (1 - t) * t * p[1].x + t * t * p[2].x;
y = (1 - t) * (1 - t) * p[0].y + 2 * (1 - t) * t * p[1].y + t * t * p[2].y;
And then just do moveTo and lineTo for each of the 300 or so points.
But that has three issues:
- It is computationally expensive
- How do you determine how many points to calculate?
- Won't it still be jagged unless you calculate thousands of points?
Is there a better way?