Question:
Why does CanvasRenderingContext2D.clip()
closes an additional path when applying it to a collection of CanvasRenderingContext2D.arc()
sampled along the path of a quadratic curve?
Background
I am trying to create a path of quadratic segments with a longitudinal color split. Based on a comment to the question "Square curve with lengthwise color division" I am trying to accomplish this goal by going through the following steps:
- Draw the quadratic path
- Sample point on the quadratic curve
- Create a clipping region and draw a cycle at each sampled point
let region = new Path2D();
for (j = 0; j < pointsQBez.length; j++) {
region.arc(pointsQBez[j].x, pointsQBez[j].y, 4, 0, 2 * Math.PI );
}
ctx.clip(region)
- Split the canvas into two segments based on the curve
- Calculate the intersection of the start- and end-segment with the canvas border
- Close the path (first clipping region)
- Draw a rectangle over the whole canvas (second clipping region)
- Fill in the two regions created in step four
Steps 3, 4, and 5 in pictures:
Issue
The pink part in the third image above should have the same thickness as the turquoise. But for some strange reason, the whole inner part of the curve gets filled in.
Additional observations
- This behaviour does not show when using
CanvasRenderingContext2D.rect()
instead ofCanvasRenderingContext2D.arc()
:
- When using
CanvasRenderingContext2D.arc()
, the inner part of the curve that is filled in is not consistent