All files / src/x x-rectangle.ts

0% Statements 0/11
0% Branches 0/4
0% Functions 0/2
0% Lines 0/10

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                                                                                     
import * as func from '../func/func';
import {XGameObject, type XGameObjectConfig} from './x-game-object';
 
interface XRectangleConfig extends XGameObjectConfig {
  fillStyle?: string;
  strokeStyle?: string;
  lineWidth?: number;
}
 
/**
 * @unstable
 */
export class XRectangle extends XGameObject {
  private fillStyle: string;
  private strokeStyle: string;
  private lineWidth: number;
 
  constructor(config: XRectangleConfig) {
    super(config);
 
    this.strokeStyle = func.setDefault(config.strokeStyle, null);
    this.fillStyle = func.setDefault(config.fillStyle, null);
    this.lineWidth = func.setDefault(config.lineWidth, 1);
  }
 
  render(ctx: CanvasRenderingContext2D) {
    ctx.fillStyle = this.fillStyle;
    ctx.strokeStyle = this.strokeStyle;
    ctx.rect(
      -this.naturalWidth * this.anchorX,
      -this.naturalHeight * this.anchorY,
      this.naturalWidth,
      this.naturalHeight
    );
 
    if (this.strokeStyle) {
      ctx.stroke();
    }
 
    if (this.fillStyle) ctx.fill();
  }
}