@blorkfield/overlay-core 0.5.10 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -5,7 +5,8 @@ interface OverlaySceneConfig {
5
5
  gravity?: number;
6
6
  wrapHorizontal?: boolean;
7
7
  debug?: boolean;
8
- background?: string;
8
+ /** Background configuration with color, image, and transparency layers */
9
+ background?: BackgroundConfig;
9
10
  /** @deprecated Use floorConfig instead. Pressure threshold for the floor boundary. */
10
11
  floorThreshold?: number;
11
12
  /** Distance below floor (as fraction of container height) at which objects despawn. Default: 1.0 (100%) */
@@ -69,6 +70,53 @@ interface FloorConfig {
69
70
  */
70
71
  segmentWidths?: number[];
71
72
  }
73
+ /**
74
+ * Image sizing mode for background images.
75
+ */
76
+ type BackgroundImageSizing = 'stretch' | 'center' | 'tile' | 'cover' | 'contain';
77
+ /**
78
+ * Configuration for the background image layer.
79
+ */
80
+ interface BackgroundImageConfig {
81
+ /** Image URL or local file path (supports same formats as entity images) */
82
+ url: string;
83
+ /** How to size/position the image. Default: 'cover' */
84
+ sizing?: BackgroundImageSizing;
85
+ }
86
+ /**
87
+ * Configuration for the transparency/frosted glass overlay layer.
88
+ * This layer renders on top of everything (including physics objects).
89
+ */
90
+ interface BackgroundTransparencyConfig {
91
+ /**
92
+ * Opacity of the overlay (0-1).
93
+ * 0 = fully transparent (no overlay)
94
+ * 0.3 = light frosted glass
95
+ * 1 = fully opaque overlay
96
+ */
97
+ opacity: number;
98
+ /**
99
+ * Tint color for the overlay (CSS color string).
100
+ * If not provided, defaults to white for frosted glass effect.
101
+ */
102
+ tintColor?: string;
103
+ }
104
+ /**
105
+ * Full background configuration with three layers (bottom to top):
106
+ * 1. Color layer (BOTTOM) - solid background color
107
+ * 2. Image layer (MIDDLE) - background image
108
+ * 3. Transparency layer (TOP) - frosted glass effect with optional tint
109
+ *
110
+ * Layers render in order: color → image → physics objects → transparency
111
+ */
112
+ interface BackgroundConfig {
113
+ /** Base background color (bottom layer). Default: 'transparent' */
114
+ color?: string;
115
+ /** Background image configuration (middle layer, behind physics objects) */
116
+ image?: BackgroundImageConfig;
117
+ /** Transparency/frosted glass effect (top layer, above physics objects) */
118
+ transparency?: BackgroundTransparencyConfig;
119
+ }
72
120
  interface Bounds {
73
121
  top: number;
74
122
  bottom: number;
@@ -476,6 +524,7 @@ declare class OverlayScene {
476
524
  private floorSegments;
477
525
  private floorSegmentPressure;
478
526
  private collapsedSegments;
527
+ private backgroundManager;
479
528
  static createContainer(parent: HTMLElement, options?: ContainerOptions): {
480
529
  canvas: HTMLCanvasElement;
481
530
  bounds: Bounds;
@@ -485,6 +534,16 @@ declare class OverlayScene {
485
534
  private handleStartDrag;
486
535
  /** Handle canvas clicks for click-to-fall behavior */
487
536
  private handleCanvasClick;
537
+ /**
538
+ * Handler for Matter.js beforeRender event.
539
+ * Draws base background layers (color + image) before physics objects.
540
+ */
541
+ private handleBeforeRender;
542
+ /**
543
+ * Handler for Matter.js afterRender event.
544
+ * Draws transparency/frosted glass layer after physics objects.
545
+ */
546
+ private handleAfterRender;
488
547
  /** Get a display name for an obstacle (letter char or short ID) */
489
548
  private getObstacleDisplayName;
490
549
  /** Update pressure tracking - check which dynamic objects rest on static obstacles */
@@ -519,6 +578,10 @@ declare class OverlayScene {
519
578
  stop(): void;
520
579
  destroy(): void;
521
580
  setDebug(enabled: boolean): void;
581
+ /**
582
+ * Update the background configuration at runtime.
583
+ */
584
+ setBackground(config: BackgroundConfig | undefined): Promise<void>;
522
585
  resize(width: number, height: number): void;
523
586
  /**
524
587
  * Spawn an object synchronously.
@@ -741,6 +804,7 @@ declare class OverlayScene {
741
804
  private fireUpdateCallbacks;
742
805
  }
743
806
 
807
+ /** Log level for controlling console output verbosity */
744
808
  type LogLevel = 'debug' | 'info' | 'warn' | 'error';
745
809
  declare function setLogLevel(level: LogLevel): void;
746
810
  declare function getLogLevel(): LogLevel;
@@ -751,6 +815,7 @@ declare const logger: {
751
815
  error(prefix: string, message: string, data?: unknown): void;
752
816
  };
753
817
 
818
+ /** 2D vector representing a point in screen space */
754
819
  interface Vector2D {
755
820
  x: number;
756
821
  y: number;
@@ -793,4 +858,113 @@ declare function measureText(loadedFont: LoadedFont, text: string, fontSize: num
793
858
  */
794
859
  declare function clearFontCache(): void;
795
860
 
796
- export { type BaseEffectConfig, type Bounds, type BurstEffectConfig, type ClickToFallConfig, type ContainerOptions, type DespawnEffectConfig, type DynamicObject, type EffectConfig, type EffectObjectConfig, type EffectType, type FloorConfig, type FontInfo, type FontManifest, type GlyphData, type LoadedFont, type ObjectConfig, OverlayScene, type OverlaySceneConfig, type PressureThresholdConfig, type RainEffectConfig, type ShadowConfig, type ShapeConfig, type ShapePreset, type StreamEffectConfig, type TTFTextObstacleConfig, type TextAlign, type TextBounds, type TextObstacleConfig, type TextObstacleResult, type UpdateCallback, type UpdateCallbackData, type WeightConfig, clearFontCache, getGlyphData, getKerning, getLogLevel, loadFont, logger, measureText, setLogLevel };
861
+ interface BackgroundManagerConfig {
862
+ canvas: HTMLCanvasElement;
863
+ width: number;
864
+ height: number;
865
+ }
866
+ /**
867
+ * Manages three-layer background rendering:
868
+ * 1. Color layer (bottom) - solid background color
869
+ * 2. Image layer (middle) - background image with sizing options
870
+ * 3. Transparency layer (top) - frosted glass effect with optional tint
871
+ *
872
+ * The color and image layers render BEFORE physics objects (in beforeRender).
873
+ * The transparency layer renders AFTER physics objects (in afterRender).
874
+ *
875
+ * Performance optimization: Base layers (color + image) are pre-rendered to an
876
+ * offscreen canvas and only re-rendered when the config changes. Each frame
877
+ * just blits the cached canvas (very fast).
878
+ */
879
+ declare class BackgroundManager {
880
+ private canvas;
881
+ private ctx;
882
+ private width;
883
+ private height;
884
+ private config;
885
+ private loadedImage;
886
+ private baseLayerCanvas;
887
+ private baseLayerCtx;
888
+ private baseLayerDirty;
889
+ constructor(managerConfig: BackgroundManagerConfig);
890
+ /**
891
+ * Create or recreate the offscreen canvas for base layer caching.
892
+ */
893
+ private createOffscreenCanvas;
894
+ /**
895
+ * Set the background configuration.
896
+ */
897
+ setConfig(config: BackgroundConfig | undefined): Promise<void>;
898
+ /**
899
+ * Load and cache a background image.
900
+ */
901
+ private loadBackgroundImage;
902
+ /**
903
+ * Update the canvas dimensions (call on resize).
904
+ */
905
+ resize(width: number, height: number): void;
906
+ /**
907
+ * Get the current background config.
908
+ */
909
+ getConfig(): BackgroundConfig | null;
910
+ /**
911
+ * Check if we have custom layers to render.
912
+ * Returns true if we need to handle background ourselves.
913
+ */
914
+ hasCustomLayers(): boolean;
915
+ /**
916
+ * Check if background is fully transparent (no color, no image, opacity = 0 or no transparency config).
917
+ */
918
+ isFullyTransparent(): boolean;
919
+ /**
920
+ * Pre-render the base layers (color + image) to the offscreen canvas.
921
+ * Only called when the config changes (baseLayerDirty is true).
922
+ */
923
+ private prerenderBaseLayers;
924
+ /**
925
+ * Render the base layers (color + image).
926
+ * Called BEFORE Matter.js renders physics objects.
927
+ * Uses cached offscreen canvas for performance.
928
+ */
929
+ renderBaseLayers(): void;
930
+ /**
931
+ * Render the transparency/frosted glass layer.
932
+ * Called AFTER Matter.js renders physics objects.
933
+ * This must render every frame since it overlays moving objects.
934
+ */
935
+ renderOverlay(): void;
936
+ /**
937
+ * Render the background image to a given context with specified sizing.
938
+ */
939
+ private renderImageLayerToContext;
940
+ /**
941
+ * Draw image centered at original size.
942
+ */
943
+ private drawCenter;
944
+ /**
945
+ * Draw image tiled (repeating pattern).
946
+ */
947
+ private drawTile;
948
+ /**
949
+ * Draw image to cover entire area (may crop, maintains aspect ratio).
950
+ */
951
+ private drawCover;
952
+ /**
953
+ * Draw image to fit within area (may have gaps, maintains aspect ratio).
954
+ * Gaps are filled by the color layer underneath.
955
+ */
956
+ private drawContain;
957
+ /**
958
+ * Render the transparency/frosted glass layer.
959
+ * opacity = 0 means fully transparent (nothing drawn)
960
+ * opacity = 1 means fully opaque overlay
961
+ * opacity = 0.3 means light frosted glass
962
+ */
963
+ private renderTransparencyLayer;
964
+ /**
965
+ * Clear the background image cache.
966
+ */
967
+ static clearCache(): void;
968
+ }
969
+
970
+ export { type BackgroundConfig, type BackgroundImageConfig, type BackgroundImageSizing, BackgroundManager, type BackgroundTransparencyConfig, type BaseEffectConfig, type Bounds, type BurstEffectConfig, type ClickToFallConfig, type ContainerOptions, type DespawnEffectConfig, type DynamicObject, type EffectConfig, type EffectObjectConfig, type EffectType, type FloorConfig, type FontInfo, type FontManifest, type GlyphData, type LoadedFont, type LogLevel, type ObjectConfig, OverlayScene, type OverlaySceneConfig, type PressureThresholdConfig, type RainEffectConfig, type ShadowConfig, type ShapeConfig, type ShapePreset, type StreamEffectConfig, type TTFTextObstacleConfig, type TextAlign, type TextBounds, type TextObstacleConfig, type TextObstacleResult, type UpdateCallback, type UpdateCallbackData, type WeightConfig, clearFontCache, getGlyphData, getKerning, getLogLevel, loadFont, logger, measureText, setLogLevel };
package/dist/index.d.ts CHANGED
@@ -5,7 +5,8 @@ interface OverlaySceneConfig {
5
5
  gravity?: number;
6
6
  wrapHorizontal?: boolean;
7
7
  debug?: boolean;
8
- background?: string;
8
+ /** Background configuration with color, image, and transparency layers */
9
+ background?: BackgroundConfig;
9
10
  /** @deprecated Use floorConfig instead. Pressure threshold for the floor boundary. */
10
11
  floorThreshold?: number;
11
12
  /** Distance below floor (as fraction of container height) at which objects despawn. Default: 1.0 (100%) */
@@ -69,6 +70,53 @@ interface FloorConfig {
69
70
  */
70
71
  segmentWidths?: number[];
71
72
  }
73
+ /**
74
+ * Image sizing mode for background images.
75
+ */
76
+ type BackgroundImageSizing = 'stretch' | 'center' | 'tile' | 'cover' | 'contain';
77
+ /**
78
+ * Configuration for the background image layer.
79
+ */
80
+ interface BackgroundImageConfig {
81
+ /** Image URL or local file path (supports same formats as entity images) */
82
+ url: string;
83
+ /** How to size/position the image. Default: 'cover' */
84
+ sizing?: BackgroundImageSizing;
85
+ }
86
+ /**
87
+ * Configuration for the transparency/frosted glass overlay layer.
88
+ * This layer renders on top of everything (including physics objects).
89
+ */
90
+ interface BackgroundTransparencyConfig {
91
+ /**
92
+ * Opacity of the overlay (0-1).
93
+ * 0 = fully transparent (no overlay)
94
+ * 0.3 = light frosted glass
95
+ * 1 = fully opaque overlay
96
+ */
97
+ opacity: number;
98
+ /**
99
+ * Tint color for the overlay (CSS color string).
100
+ * If not provided, defaults to white for frosted glass effect.
101
+ */
102
+ tintColor?: string;
103
+ }
104
+ /**
105
+ * Full background configuration with three layers (bottom to top):
106
+ * 1. Color layer (BOTTOM) - solid background color
107
+ * 2. Image layer (MIDDLE) - background image
108
+ * 3. Transparency layer (TOP) - frosted glass effect with optional tint
109
+ *
110
+ * Layers render in order: color → image → physics objects → transparency
111
+ */
112
+ interface BackgroundConfig {
113
+ /** Base background color (bottom layer). Default: 'transparent' */
114
+ color?: string;
115
+ /** Background image configuration (middle layer, behind physics objects) */
116
+ image?: BackgroundImageConfig;
117
+ /** Transparency/frosted glass effect (top layer, above physics objects) */
118
+ transparency?: BackgroundTransparencyConfig;
119
+ }
72
120
  interface Bounds {
73
121
  top: number;
74
122
  bottom: number;
@@ -476,6 +524,7 @@ declare class OverlayScene {
476
524
  private floorSegments;
477
525
  private floorSegmentPressure;
478
526
  private collapsedSegments;
527
+ private backgroundManager;
479
528
  static createContainer(parent: HTMLElement, options?: ContainerOptions): {
480
529
  canvas: HTMLCanvasElement;
481
530
  bounds: Bounds;
@@ -485,6 +534,16 @@ declare class OverlayScene {
485
534
  private handleStartDrag;
486
535
  /** Handle canvas clicks for click-to-fall behavior */
487
536
  private handleCanvasClick;
537
+ /**
538
+ * Handler for Matter.js beforeRender event.
539
+ * Draws base background layers (color + image) before physics objects.
540
+ */
541
+ private handleBeforeRender;
542
+ /**
543
+ * Handler for Matter.js afterRender event.
544
+ * Draws transparency/frosted glass layer after physics objects.
545
+ */
546
+ private handleAfterRender;
488
547
  /** Get a display name for an obstacle (letter char or short ID) */
489
548
  private getObstacleDisplayName;
490
549
  /** Update pressure tracking - check which dynamic objects rest on static obstacles */
@@ -519,6 +578,10 @@ declare class OverlayScene {
519
578
  stop(): void;
520
579
  destroy(): void;
521
580
  setDebug(enabled: boolean): void;
581
+ /**
582
+ * Update the background configuration at runtime.
583
+ */
584
+ setBackground(config: BackgroundConfig | undefined): Promise<void>;
522
585
  resize(width: number, height: number): void;
523
586
  /**
524
587
  * Spawn an object synchronously.
@@ -741,6 +804,7 @@ declare class OverlayScene {
741
804
  private fireUpdateCallbacks;
742
805
  }
743
806
 
807
+ /** Log level for controlling console output verbosity */
744
808
  type LogLevel = 'debug' | 'info' | 'warn' | 'error';
745
809
  declare function setLogLevel(level: LogLevel): void;
746
810
  declare function getLogLevel(): LogLevel;
@@ -751,6 +815,7 @@ declare const logger: {
751
815
  error(prefix: string, message: string, data?: unknown): void;
752
816
  };
753
817
 
818
+ /** 2D vector representing a point in screen space */
754
819
  interface Vector2D {
755
820
  x: number;
756
821
  y: number;
@@ -793,4 +858,113 @@ declare function measureText(loadedFont: LoadedFont, text: string, fontSize: num
793
858
  */
794
859
  declare function clearFontCache(): void;
795
860
 
796
- export { type BaseEffectConfig, type Bounds, type BurstEffectConfig, type ClickToFallConfig, type ContainerOptions, type DespawnEffectConfig, type DynamicObject, type EffectConfig, type EffectObjectConfig, type EffectType, type FloorConfig, type FontInfo, type FontManifest, type GlyphData, type LoadedFont, type ObjectConfig, OverlayScene, type OverlaySceneConfig, type PressureThresholdConfig, type RainEffectConfig, type ShadowConfig, type ShapeConfig, type ShapePreset, type StreamEffectConfig, type TTFTextObstacleConfig, type TextAlign, type TextBounds, type TextObstacleConfig, type TextObstacleResult, type UpdateCallback, type UpdateCallbackData, type WeightConfig, clearFontCache, getGlyphData, getKerning, getLogLevel, loadFont, logger, measureText, setLogLevel };
861
+ interface BackgroundManagerConfig {
862
+ canvas: HTMLCanvasElement;
863
+ width: number;
864
+ height: number;
865
+ }
866
+ /**
867
+ * Manages three-layer background rendering:
868
+ * 1. Color layer (bottom) - solid background color
869
+ * 2. Image layer (middle) - background image with sizing options
870
+ * 3. Transparency layer (top) - frosted glass effect with optional tint
871
+ *
872
+ * The color and image layers render BEFORE physics objects (in beforeRender).
873
+ * The transparency layer renders AFTER physics objects (in afterRender).
874
+ *
875
+ * Performance optimization: Base layers (color + image) are pre-rendered to an
876
+ * offscreen canvas and only re-rendered when the config changes. Each frame
877
+ * just blits the cached canvas (very fast).
878
+ */
879
+ declare class BackgroundManager {
880
+ private canvas;
881
+ private ctx;
882
+ private width;
883
+ private height;
884
+ private config;
885
+ private loadedImage;
886
+ private baseLayerCanvas;
887
+ private baseLayerCtx;
888
+ private baseLayerDirty;
889
+ constructor(managerConfig: BackgroundManagerConfig);
890
+ /**
891
+ * Create or recreate the offscreen canvas for base layer caching.
892
+ */
893
+ private createOffscreenCanvas;
894
+ /**
895
+ * Set the background configuration.
896
+ */
897
+ setConfig(config: BackgroundConfig | undefined): Promise<void>;
898
+ /**
899
+ * Load and cache a background image.
900
+ */
901
+ private loadBackgroundImage;
902
+ /**
903
+ * Update the canvas dimensions (call on resize).
904
+ */
905
+ resize(width: number, height: number): void;
906
+ /**
907
+ * Get the current background config.
908
+ */
909
+ getConfig(): BackgroundConfig | null;
910
+ /**
911
+ * Check if we have custom layers to render.
912
+ * Returns true if we need to handle background ourselves.
913
+ */
914
+ hasCustomLayers(): boolean;
915
+ /**
916
+ * Check if background is fully transparent (no color, no image, opacity = 0 or no transparency config).
917
+ */
918
+ isFullyTransparent(): boolean;
919
+ /**
920
+ * Pre-render the base layers (color + image) to the offscreen canvas.
921
+ * Only called when the config changes (baseLayerDirty is true).
922
+ */
923
+ private prerenderBaseLayers;
924
+ /**
925
+ * Render the base layers (color + image).
926
+ * Called BEFORE Matter.js renders physics objects.
927
+ * Uses cached offscreen canvas for performance.
928
+ */
929
+ renderBaseLayers(): void;
930
+ /**
931
+ * Render the transparency/frosted glass layer.
932
+ * Called AFTER Matter.js renders physics objects.
933
+ * This must render every frame since it overlays moving objects.
934
+ */
935
+ renderOverlay(): void;
936
+ /**
937
+ * Render the background image to a given context with specified sizing.
938
+ */
939
+ private renderImageLayerToContext;
940
+ /**
941
+ * Draw image centered at original size.
942
+ */
943
+ private drawCenter;
944
+ /**
945
+ * Draw image tiled (repeating pattern).
946
+ */
947
+ private drawTile;
948
+ /**
949
+ * Draw image to cover entire area (may crop, maintains aspect ratio).
950
+ */
951
+ private drawCover;
952
+ /**
953
+ * Draw image to fit within area (may have gaps, maintains aspect ratio).
954
+ * Gaps are filled by the color layer underneath.
955
+ */
956
+ private drawContain;
957
+ /**
958
+ * Render the transparency/frosted glass layer.
959
+ * opacity = 0 means fully transparent (nothing drawn)
960
+ * opacity = 1 means fully opaque overlay
961
+ * opacity = 0.3 means light frosted glass
962
+ */
963
+ private renderTransparencyLayer;
964
+ /**
965
+ * Clear the background image cache.
966
+ */
967
+ static clearCache(): void;
968
+ }
969
+
970
+ export { type BackgroundConfig, type BackgroundImageConfig, type BackgroundImageSizing, BackgroundManager, type BackgroundTransparencyConfig, type BaseEffectConfig, type Bounds, type BurstEffectConfig, type ClickToFallConfig, type ContainerOptions, type DespawnEffectConfig, type DynamicObject, type EffectConfig, type EffectObjectConfig, type EffectType, type FloorConfig, type FontInfo, type FontManifest, type GlyphData, type LoadedFont, type LogLevel, type ObjectConfig, OverlayScene, type OverlaySceneConfig, type PressureThresholdConfig, type RainEffectConfig, type ShadowConfig, type ShapeConfig, type ShapePreset, type StreamEffectConfig, type TTFTextObstacleConfig, type TextAlign, type TextBounds, type TextObstacleConfig, type TextObstacleResult, type UpdateCallback, type UpdateCallbackData, type WeightConfig, clearFontCache, getGlyphData, getKerning, getLogLevel, loadFont, logger, measureText, setLogLevel };