All files / src/editor Draw.ts

100% Statements 158/158
100% Branches 102/102
100% Functions 17/17
100% Lines 146/146

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342        5x         71x               5x 5x                       35x               12x 12x   12x           12x         12x         5x               5x 34x     76x 76x               11x 11x 19x 11x               160x 160x 160x 160x 160x 160x   22x 22x 22x 22x 22x     3x 3x 3x                 3x 3x     4x 4x 4x   17x 17x       17x 17x 17x       17x 17x 17x 17x       17x       17x 17x 17x 17x 17x     4x 4x 4x   3x 3x 3x 3x 3x 3x   31x 31x 30x 30x 30x 30x   30x 30x 30x 30x 30x     26x 26x 26x 26x     26x 27x 27x   26x     50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x     160x                     94x 94x 94x 94x 94x 94x 94x 94x                     146x 94x       18x 8x 8x 8x 9x 8x 8x   10x 1x           36x 36x 9x 18x 18x 18x                     5x 5x   5x           5x                   11x 16x 16x           4x   5x     5x       2x   2x   12x 12x           6x   3x                     6x 6x 6x 4x 4x 4x   2x 2x    
import type { Rect, Shape } from '../lib/Db';
 
export type Source = CanvasImageSource & { width: number; height: number };
 
export const normRect = (
  x1: number,
  y1: number,
  x2: number,
  y2: number,
): Rect => ({
  x: Math.min(x1, x2),
  y: Math.min(y1, y2),
  w: Math.abs(x2 - x1),
  h: Math.abs(y2 - y1),
});
 
export type Handle = 'nw' | 'n' | 'ne' | 'e' | 'se' | 's' | 'sw' | 'w';
export const HANDLES: Handle[] = ['nw', 'n', 'ne', 'e', 'se', 's', 'sw', 'w'];
export const HANDLE_CURSOR: Record<Handle, string> = {
  n: 'ns-resize',
  s: 'ns-resize',
  e: 'ew-resize',
  w: 'ew-resize',
  nw: 'nwse-resize',
  se: 'nwse-resize',
  ne: 'nesw-resize',
  sw: 'nesw-resize',
};
 
export function handlePoint(r: Rect, h: Handle): [number, number] {
  return [
    h.includes('w') ? r.x : h.includes('e') ? r.x + r.w : r.x + r.w / 2,
    h.includes('n') ? r.y : h.includes('s') ? r.y + r.h : r.y + r.h / 2,
  ];
}
 
/** The edge or corner of `r` under (x, y); whole edges are grabbable, not just their midpoints. */
export function handleAt(r: Rect, x: number, y: number, tol: number) {
  const inX = x >= r.x - tol && x <= r.x + r.w + tol;
  const inY = y >= r.y - tol && y <= r.y + r.h + tol;
  const v =
    inX && Math.abs(y - r.y) <= tol
      ? 'n'
      : inX && Math.abs(y - (r.y + r.h)) <= tol
        ? 's'
        : '';
  const h =
    inY && Math.abs(x - r.x) <= tol
      ? 'w'
      : inY && Math.abs(x - (r.x + r.w)) <= tol
        ? 'e'
        : '';
  return (v + h || undefined) as Handle | undefined;
}
 
/** Moves the edges named by `h` to (x, y); dragging past the opposite edge flips the rect. */
export function resizeRect(r: Rect, h: Handle, x: number, y: number) {
  return normRect(
    h.includes('w') ? x : r.x,
    h.includes('n') ? y : r.y,
    h.includes('e') ? x : r.x + r.w,
    h.includes('s') ? y : r.y + r.h,
  );
}
 
const font = (size: number) =>
  `600 ${size}px ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif`;
 
function isLight(hex: string) {
  const n = parseInt(hex.slice(1), 16);
  return (
    (0.299 * ((n >> 16) & 255) + 0.587 * ((n >> 8) & 255) + 0.114 * (n & 255)) /
      255 >
    0.6
  );
}
 
function tracePath(ctx: CanvasRenderingContext2D, pts: number[]) {
  ctx.beginPath();
  ctx.moveTo(pts[0], pts[1]);
  for (let i = 2; i < pts.length; i += 2) ctx.lineTo(pts[i], pts[i + 1]);
  if (pts.length === 2) ctx.lineTo(pts[0] + 0.01, pts[1]);
}
 
export function drawShape(
  ctx: CanvasRenderingContext2D,
  s: Shape,
  img: Source,
) {
  const [x1, y1, x2, y2] = s.pts;
  ctx.save();
  ctx.strokeStyle = ctx.fillStyle = s.color;
  ctx.lineWidth = s.size;
  ctx.lineCap = ctx.lineJoin = 'round';
  switch (s.kind) {
    case 'rect': {
      const r = normRect(x1, y1, x2, y2);
      ctx.beginPath();
      ctx.roundRect(r.x, r.y, r.w, r.h, s.size);
      ctx.stroke();
      break;
    }
    case 'ellipse': {
      const r = normRect(x1, y1, x2, y2);
      ctx.beginPath();
      ctx.ellipse(
        r.x + r.w / 2,
        r.y + r.h / 2,
        r.w / 2,
        r.h / 2,
        0,
        0,
        Math.PI * 2,
      );
      ctx.stroke();
      break;
    }
    case 'line':
      tracePath(ctx, s.pts);
      ctx.stroke();
      break;
    case 'arrow': {
      const angle = Math.atan2(y2 - y1, x2 - x1);
      const head = Math.min(
        Math.max(s.size * 4, 14),
        Math.hypot(x2 - x1, y2 - y1),
      );
      ctx.beginPath();
      ctx.moveTo(x1, y1);
      ctx.lineTo(
        x2 - Math.cos(angle) * head * 0.7,
        y2 - Math.sin(angle) * head * 0.7,
      );
      ctx.stroke();
      ctx.beginPath();
      ctx.moveTo(x2, y2);
      ctx.lineTo(
        x2 - head * Math.cos(angle - 0.42),
        y2 - head * Math.sin(angle - 0.42),
      );
      ctx.lineTo(
        x2 - head * Math.cos(angle + 0.42),
        y2 - head * Math.sin(angle + 0.42),
      );
      ctx.closePath();
      ctx.lineWidth = s.size / 2;
      ctx.fill();
      ctx.stroke();
      break;
    }
    case 'pen':
      tracePath(ctx, s.pts);
      ctx.stroke();
      break;
    case 'highlight':
      ctx.globalCompositeOperation = 'multiply';
      ctx.globalAlpha = 0.45;
      ctx.lineCap = 'butt';
      tracePath(ctx, s.pts);
      ctx.stroke();
      break;
    case 'blur': {
      const r = normRect(x1, y1, x2, y2);
      if (r.w < 1 || r.h < 1) break;
      const tw = Math.max(1, Math.ceil(r.w / s.size));
      const th = Math.max(1, Math.ceil(r.h / s.size));
      const tmp = new OffscreenCanvas(tw, th);
      const t = tmp.getContext('2d')!;
      // High quality averages each block; the default samples a few pixels and leaves speckles.
      t.imageSmoothingQuality = 'high';
      t.drawImage(img, r.x, r.y, r.w, r.h, 0, 0, tw, th);
      ctx.imageSmoothingEnabled = false;
      ctx.drawImage(tmp, 0, 0, tw, th, r.x, r.y, r.w, r.h);
      break;
    }
    case 'text': {
      ctx.font = font(s.size);
      ctx.textBaseline = 'top';
      ctx.lineWidth = s.size / 6;
      ctx.strokeStyle = isLight(s.color)
        ? 'rgba(0,0,0,.55)'
        : 'rgba(255,255,255,.9)';
      (s.text ?? '').split('\n').forEach((line, i) => {
        ctx.strokeText(line, x1, y1 + i * s.size * 1.25);
        ctx.fillText(line, x1, y1 + i * s.size * 1.25);
      });
      break;
    }
    case 'step': {
      ctx.shadowColor = 'rgba(0,0,0,.3)';
      ctx.shadowBlur = s.size / 2;
      ctx.beginPath();
      ctx.arc(x1, y1, s.size, 0, Math.PI * 2);
      ctx.fill();
      ctx.shadowColor = 'transparent';
      ctx.lineWidth = s.size * 0.14;
      ctx.strokeStyle = '#fff';
      ctx.stroke();
      ctx.fillStyle = isLight(s.color) ? '#111' : '#fff';
      ctx.font = `700 ${s.size * 1.05}px ui-sans-serif, system-ui, sans-serif`;
      ctx.textAlign = 'center';
      ctx.textBaseline = 'middle';
      ctx.fillText(s.text ?? '', x1, y1 + s.size * 0.06);
      break;
    }
  }
  ctx.restore();
}
 
/** Draws `region` (image px) of image + shapes at `k` canvas px per image px. Leaves that transform applied. */
export function render(
  canvas: HTMLCanvasElement,
  img: Source,
  shapes: Shape[],
  region: Rect,
  k = 1,
) {
  const w = Math.max(1, Math.round(region.w * k));
  const h = Math.max(1, Math.round(region.h * k));
  if (canvas.width !== w) canvas.width = w;
  if (canvas.height !== h) canvas.height = h;
  const ctx = canvas.getContext('2d')!;
  ctx.setTransform(k, 0, 0, k, -region.x * k, -region.y * k);
  ctx.clearRect(region.x, region.y, region.w, region.h);
  ctx.drawImage(
    img,
    region.x,
    region.y,
    region.w,
    region.h,
    region.x,
    region.y,
    region.w,
    region.h,
  );
  for (const s of shapes) drawShape(ctx, s, img);
  return ctx;
}
 
export function bounds(ctx: CanvasRenderingContext2D, s: Shape): Rect {
  if (s.kind === 'text') {
    ctx.save();
    ctx.font = font(s.size);
    const lines = (s.text ?? '').split('\n');
    const w = Math.max(...lines.map((l) => ctx.measureText(l).width));
    ctx.restore();
    return { x: s.pts[0], y: s.pts[1], w, h: lines.length * s.size * 1.25 };
  }
  if (s.kind === 'step')
    return {
      x: s.pts[0] - s.size,
      y: s.pts[1] - s.size,
      w: s.size * 2,
      h: s.size * 2,
    };
  const xs = s.pts.filter((_, i) => i % 2 === 0);
  const ys = s.pts.filter((_, i) => i % 2 === 1);
  const pad = s.kind === 'blur' ? 0 : s.size / 2;
  const x = Math.min(...xs) - pad;
  const y = Math.min(...ys) - pad;
  return { x, y, w: Math.max(...xs) + pad - x, h: Math.max(...ys) + pad - y };
}
 
function distToSegment(
  px: number,
  py: number,
  x1: number,
  y1: number,
  x2: number,
  y2: number,
) {
  const dx = x2 - x1;
  const dy = y2 - y1;
  const t =
    dx || dy
      ? Math.max(
          0,
          Math.min(1, ((px - x1) * dx + (py - y1) * dy) / (dx * dx + dy * dy)),
        )
      : 0;
  return Math.hypot(px - (x1 + t * dx), py - (y1 + t * dy));
}
 
export function hitTest(
  ctx: CanvasRenderingContext2D,
  shapes: Shape[],
  x: number,
  y: number,
  tolerance: number,
) {
  for (let i = shapes.length - 1; i >= 0; i--) {
    const s = shapes[i];
    if (
      s.kind === 'line' ||
      s.kind === 'arrow' ||
      s.kind === 'pen' ||
      s.kind === 'highlight'
    ) {
      for (let j = 0; j + 1 < s.pts.length; j += 2) {
        const [x2, y2] =
          j + 3 < s.pts.length
            ? [s.pts[j + 2], s.pts[j + 3]]
            : [s.pts[j], s.pts[j + 1]];
        if (
          distToSegment(x, y, s.pts[j], s.pts[j + 1], x2, y2) <=
          s.size / 2 + tolerance
        )
          return s;
      }
      continue;
    }
    const b = bounds(ctx, s);
    if (
      x >= b.x - tolerance &&
      x <= b.x + b.w + tolerance &&
      y >= b.y - tolerance &&
      y <= b.y + b.h + tolerance
    )
      return s;
  }
  return undefined;
}
 
/** Shift-drag: snap lines to 45° and boxes to squares. */
export function constrain(
  kind: Shape['kind'],
  x1: number,
  y1: number,
  x2: number,
  y2: number,
): [number, number] {
  const dx = x2 - x1;
  const dy = y2 - y1;
  if (kind === 'line' || kind === 'arrow') {
    const a = (Math.round(Math.atan2(dy, dx) / (Math.PI / 4)) * Math.PI) / 4;
    const len = Math.hypot(dx, dy);
    return [x1 + Math.cos(a) * len, y1 + Math.sin(a) * len];
  }
  const m = Math.max(Math.abs(dx), Math.abs(dy));
  return [x1 + Math.sign(dx || 1) * m, y1 + Math.sign(dy || 1) * m];
}