chenyc
2025-05-29 92f69c57b920cf62ecc9f15f9ed196fa26dbf2ac
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { Point } from '../classes/index';
 
export function drawContour(
  ctx: CanvasRenderingContext2D,
  points: Point[],
  isClosed = false,
) {
  ctx.beginPath();
 
  points.slice(1).forEach(({ x, y }, prevIdx) => {
    const from = points[prevIdx];
    ctx.moveTo(from.x, from.y);
    ctx.lineTo(x, y);
  });
 
  if (isClosed) {
    const from = points[points.length - 1];
    const to = points[0];
    if (!from || !to) {
      return;
    }
 
    ctx.moveTo(from.x, from.y);
    ctx.lineTo(to.x, to.y);
  }
 
  ctx.stroke();
}