All files / player-ui/src/playfield canvas-element.ts

93.18% Statements 41/44
71.42% Branches 25/35
100% Functions 8/8
97.5% Lines 39/40

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                      49x                         49x       8x             1x 1x         1x 1x 1x 1x 2x 2x 2x 2x 2x 2x   1x 1x             1x 1x 1x       2x       1x       2x       4x 4x 3x 2x 2x   3x 3x 3x 3x 3x 3x 3x 2x 2x 2x 2x      
import { css, html, type CSSResultGroup, type PropertyDeclarations, type TemplateResult } from 'lit';
import type { BeMusicRect } from '@be-music/player-skin-sdk';
import { DataElement } from '../base.ts';
import type { BeMusicPlayfieldPaintContext } from './paint.ts';
 
/**
 * A playfield component drawing on its own canvas. `rect` is the part of the stage it covers, in design pixels: the
 * element is sized by its layout, and its canvas matches the device pixels of that box, so painters keep drawing in
 * stage coordinates whatever the element's size on screen.
 */
export abstract class BeMusicPlayfieldCanvasElement<Data> extends DataElement<Data> {
  static override styles: CSSResultGroup = css`
    :host {
      display: block;
      position: relative;
      pointer-events: none;
    }
    canvas {
      position: absolute;
      inset: 0;
      width: 100%;
      height: 100%;
    }
  `;
  static override properties: PropertyDeclarations = {
    rect: {
      attribute: false,
      hasChanged: (next: BeMusicRect | undefined, previous: BeMusicRect | undefined) =>
        next?.x !== previous?.x || next?.y !== previous?.y || next?.w !== previous?.w || next?.h !== previous?.h,
    },
  };
  declare rect: BeMusicRect | undefined;
  private canvas: HTMLCanvasElement | undefined;
  private context: CanvasRenderingContext2D | null | undefined;
  private observer: ResizeObserver | undefined;
  private pixelWidth = 0;
  private pixelHeight = 0;
 
  protected abstract paint(context: BeMusicPlayfieldPaintContext, data: Data): void;
 
  override connectedCallback(): void {
    super.connectedCallback();
    const Observer = this.ownerDocument.defaultView?.ResizeObserver;
    Iif (!Observer) return;
    this.observer = new Observer(([entry]) => {
      const device = entry?.devicePixelContentBoxSize?.[0];
      const ratio = this.ownerDocument.defaultView?.devicePixelRatio ?? 1;
      const box = entry?.contentRect;
      this.pixelWidth = Math.round(device?.inlineSize ?? (box?.width ?? 0) * ratio);
      this.pixelHeight = Math.round(device?.blockSize ?? (box?.height ?? 0) * ratio);
      this.draw();
    });
    try {
      this.observer.observe(this, { box: 'device-pixel-content-box' });
    } catch {
      this.observer.observe(this);
    }
  }
 
  override disconnectedCallback(): void {
    super.disconnectedCallback();
    this.observer?.disconnect();
    this.observer = undefined;
  }
 
  protected override render(): TemplateResult {
    return html`<canvas part="canvas" aria-hidden="true"></canvas>`;
  }
 
  protected override firstUpdated(): void {
    this.canvas = this.renderRoot.querySelector('canvas') ?? undefined;
  }
 
  protected override updated(): void {
    this.draw();
  }
 
  private draw(): void {
    const canvas = this.canvas;
    if (!canvas || this.pixelWidth <= 0 || this.pixelHeight <= 0) return;
    if (canvas.width !== this.pixelWidth || canvas.height !== this.pixelHeight) {
      canvas.width = this.pixelWidth;
      canvas.height = this.pixelHeight;
    }
    this.context ??= canvas.getContext('2d');
    const ctx = this.context;
    Iif (!ctx) return;
    ctx.setTransform(1, 0, 0, 1, 0, 0);
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    const rect = this.rect;
    if (!rect || rect.w <= 0 || rect.h <= 0 || this.data === undefined) return;
    const scaleX = canvas.width / rect.w;
    const scaleY = canvas.height / rect.h;
    ctx.setTransform(scaleX, 0, 0, scaleY, -rect.x * scaleX, -rect.y * scaleY);
    this.paint(ctx, this.data);
  }
}