All files / src/x x-line.ts

0% Statements 0/28
0% Branches 0/6
0% Functions 0/3
0% Lines 0/28

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                                                                                                                                                                                                                                       
import * as func from '../func/func';
 
import {XGameObject, type XGameObjectConfig} from './x-game-object';
 
interface XLineConfig extends XGameObjectConfig {
  strokeStyle: string;
  lineWidth: number;
  startX: number;
  startY: number;
  endX: number;
  endY: number;
  gradientStops?: Array<Object>;
  linearGradient?: Object;
  radialGradient?: Object;
}
 
/**
 * A single line in the x-engine.
 * @unstable
 */
export class XLine extends XGameObject {
  private strokeStyle: string;
  private lineWidth: number;
  private lineJoin: string;
  private startX: number;
  private startY: number;
  private endX: number;
  private endY: number;
 
  /**
   * An array of gradient stops.
   * Example:
   * [
   *    {
   *      stop: 0, color: 'red'
   *     },
   *    {
   *      stop: 1, color: 'green'
   *     }
   * ]
   */
  private gradientStops: Array<Object> | null;
  /**
   * An Object defining startX, startY, endX and endY for the linear gradient.
   * TODO (uxder): Clean this up.
   */
  private linearGradient: Record<string, number> | null = null;
  private radialGradient: Record<string, number> | null = null;
 
  constructor(config: XLineConfig) {
    super(config);
 
    this.strokeStyle = func.setDefault(config.strokeStyle, 'red');
    this.lineWidth = func.setDefault(config.lineWidth, 1);
    this.startX = func.setDefault(config.startX, 0);
    this.startY = func.setDefault(config.startY, 0);
    this.endX = func.setDefault(config.endX, 0);
    this.endY = func.setDefault(config.endY, 0);
    this.gradientStops = func.setDefault(config.gradientStops, null);
    this.linearGradient = func.setDefault(config.linearGradient, null);
 
    //Options are "round", "mitre" and "bevel".
    this.lineJoin = 'round';
  }
 
  render(ctx: CanvasRenderingContext2D) {
    ctx.strokeStyle = this.strokeStyle;
 
    if (this.gradientStops) {
      let grad = ctx.createLinearGradient(
        this.startX,
        this.startY,
        this.endX,
        this.endY
      );
 
      // Override linear gradient poitns.
      if (this.linearGradient) {
        grad = ctx.createLinearGradient(
          this.linearGradient.startX,
          this.linearGradient.startY,
          this.linearGradient.endX,
          this.linearGradient.endY
        );
      }
      // Override linear gradient poitns.
      if (this.radialGradient) {
        grad = ctx.createRadialGradient(
          this.radialGradient.x0,
          this.radialGradient.y0,
          this.radialGradient.r0,
          this.radialGradient.x1,
          this.radialGradient.y1,
          this.radialGradient.r1
        );
      }
 
      // eslint-disable-next-line @typescript-eslint/no-explicit-any
      this.gradientStops.forEach((stop: any) => {
        grad.addColorStop(stop.stop, stop.color);
      });
      ctx.strokeStyle = grad;
    }
 
    ctx.lineWidth = this.lineWidth;
    ctx.beginPath();
    ctx.lineJoin = 'round';
    ctx.moveTo(this.startX, this.startY);
    ctx.lineTo(this.endX, this.endY);
    //ctx.closePath();
    ctx.stroke();
    // if (this..strokeStyle !== "none") ctx.stroke();
    // if (o.fillStyle !== "none") ctx.fill();
  }
}