All files / src/dom dom-canvas.ts

0% Statements 0/29
0% Branches 0/2
0% Functions 0/12
0% Lines 0/29

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                                                                                                                                                                                                                                                                                   
/**
 * Very basic HTML5 canvas helper methods.
 */
 
import {type ColorRGB, rgbToHex} from '../mathf/color';
import type {Vector} from '../mathf/vector';
 
export function setFillColor(
  context: CanvasRenderingContext2D,
  fillColor: string
) {
  context.fillStyle = fillColor;
}
 
export function setStrokeColor(
  context: CanvasRenderingContext2D,
  strokeColor: string
) {
  context.strokeStyle = strokeColor;
}
 
export function line(
  context: CanvasRenderingContext2D,
  x1: number,
  y1: number,
  x2: number,
  y2: number
) {
  context.beginPath();
  context.moveTo(x1, y1);
  context.lineTo(x2, y2);
  context.stroke();
}
 
/**
 * Display text at given coordinates.   Quick because this method is
 * just if you want to output something quickly without concent to
 * font-size style.
 * @param context
 * @param text
 */
export function quickText(
  context: CanvasRenderingContext2D,
  text: string,
  x: number,
  y: number
) {
  context.font = '12px sans-serif';
  context.strokeText(text, x, y);
}
 
export function point(
  context: CanvasRenderingContext2D,
  x: number,
  y: number,
  size = 5
) {
  context.fillRect(x, y, size, size);
}
 
export function vectorLine(
  context: CanvasRenderingContext2D,
  v1: Vector,
  v2: Vector
) {
  line(context, v1.x, v1.y, v2.x, v2.y);
}
 
export function vectorPoint(
  context: CanvasRenderingContext2D,
  v: Vector,
  size = 5
) {
  point(context, v.x, v.y, size);
}
 
export function vectorQuadrilateral(
  context: CanvasRenderingContext2D,
  v1: Vector,
  v2: Vector,
  v3: Vector,
  v4: Vector
) {
  context.beginPath();
  context.moveTo(v1.x, v1.y);
  context.lineTo(v2.x, v2.y);
  context.lineTo(v3.x, v3.y);
  context.lineTo(v4.x, v4.y);
  context.closePath();
  context.fill();
}
 
/**
 * Given a list of Vectors, draws lines from start to end.
 */
export function drawVectors(
  context: CanvasRenderingContext2D,
  vectors: Array<Vector>
) {
  context.beginPath();
  context.moveTo(vectors[0].x, vectors[0].y);
  vectors.forEach((v) => {
    context.lineTo(v.x, v.y);
  });
  context.closePath();
  context.fill();
}
 
/**
 * Acquires the pixel color of given coordinates on the canvas.
 */
export function getColorAtPointAsRgb(
  context: CanvasRenderingContext2D,
  coords: Vector
): ColorRGB {
  const pixelData = context.getImageData(coords.x, coords.y, 1, 1).data;
  const rgb = {
    r: pixelData[0],
    g: pixelData[1],
    b: pixelData[2],
  };
 
  return rgb;
}
 
/**
 * Acquires the pixel color of given coordinates on the canvas
 * as returns it as a hex value.
 */
export function getColorAtPointAsHex(
  context: CanvasRenderingContext2D,
  coords: Vector
): string {
  const rgb = getColorAtPointAsRgb(context, coords);
 
  return rgbToHex(rgb);
}