File size: 1,152 Bytes
10dd572
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { getStroke } from 'perfect-freehand';
import type { StrokeObject } from './types';
const STROKE_SMOOTHING = 0.62;
const STROKE_STREAMLINE = 0.48;
const STROKE_THINNING = 0.28;

export function drawStroke(ctx: CanvasRenderingContext2D, stroke: StrokeObject): void {
  if (stroke.points.length === 0) {
    return;
  }

  ctx.fillStyle = stroke.color;

  if (stroke.points.length === 1) {
    const [point] = stroke.points;
    ctx.beginPath();
    ctx.arc(point.x, point.y, stroke.width / 2, 0, Math.PI * 2);
    ctx.fill();
    return;
  }

  const outline = getStroke(
    stroke.points.map((point) => [point.x, point.y] as [number, number]),
    {
      size: stroke.width * 1.85,
      smoothing: STROKE_SMOOTHING,
      streamline: STROKE_STREAMLINE,
      thinning: STROKE_THINNING,
      simulatePressure: true,
      easing: (value) => value,
      last: true,
    }
  );

  if (outline.length === 0) {
    return;
  }

  ctx.beginPath();
  ctx.moveTo(outline[0][0], outline[0][1]);
  for (let index = 1; index < outline.length; index += 1) {
    ctx.lineTo(outline[index][0], outline[index][1]);
  }
  ctx.closePath();
  ctx.fill();
}