littlejsengine 1.18.21 → 1.18.22

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/README.md CHANGED
@@ -80,6 +80,12 @@ The code is very clean and well documented with many examples to get you started
80
80
  - [LittleJS AI Tools](https://github.com/KilledByAPixel/LittleJS-AI) - Templates, examples, and prompts tuned for AI + LittleJS workflows
81
81
  - [LittleJS GPT](https://chatgpt.com/g/g-67c7c080b5bc81919736bc8815836be6-littlejs-game-maker) - Build LittleJS games right inside ChatGPT
82
82
 
83
+ ### 🧊 3D with Three.js
84
+
85
+ - Optional [Three.js](https://threejs.org) plugin renders a 3D scene behind the LittleJS canvas
86
+ - 2D camera and 3D camera stay aligned so sprites and meshes share the same world space
87
+ - LittleJS physics drive Three.js meshes with `ThreeJSObject`
88
+
83
89
  ## How To Use LittleJS
84
90
 
85
91
  To get started download the latest LittleJS package from GitHub or install via npm:
@@ -1669,8 +1669,9 @@ declare module "littlejsengine" {
1669
1669
  * @param {TextureInfo} [textureInfo] - Texture info to use
1670
1670
  * @param {number} [padding] - How many pixels padding around all sides of each tile (increases grid size, does not affect tile size)
1671
1671
  * @param {number} [bleed] - How many pixels smaller to shrink UVS of tiles (does not affect grid size, only UVs)
1672
+ * @param {number} [columns] - How many frames per row for frame(), 0 to keep frames on a single row
1672
1673
  */
1673
- constructor(pos?: Vector2, size?: Vector2, textureInfo?: TextureInfo, padding?: number, bleed?: number);
1674
+ constructor(pos?: Vector2, size?: Vector2, textureInfo?: TextureInfo, padding?: number, bleed?: number, columns?: number);
1674
1675
  /** @property {Vector2} - Top left corner of tile in pixels */
1675
1676
  pos: Vector2;
1676
1677
  /** @property {Vector2} - Size of tile in pixels */
@@ -1681,16 +1682,24 @@ declare module "littlejsengine" {
1681
1682
  textureInfo: TextureInfo;
1682
1683
  /** @property {number} - Shrinks tile by this many pixels to prevent neighbors bleeding */
1683
1684
  bleed: number;
1685
+ /** @property {number} - How many frames per row for frame(), 0 to keep frames on a single row */
1686
+ columns: number;
1684
1687
  /** Returns a copy of this tile offset by a vector
1685
1688
  * @param {Vector2} offset - Offset to apply in pixels
1686
1689
  * @return {TileInfo}
1687
1690
  */
1688
1691
  offset(offset: Vector2): TileInfo;
1689
1692
  /** Returns a copy of this tile offset by a number of animation frames
1693
+ * Frames wrap down to the next row if columns is set
1690
1694
  * @param {number} frame - Offset to apply in animation frames
1691
1695
  * @return {TileInfo}
1692
1696
  */
1693
1697
  frame(frame: number): TileInfo;
1698
+ /** Set how many frames per row this tile uses, so frame() can wrap
1699
+ * @param {number} [columns] - Frames per row, 0 to keep frames on a single row
1700
+ * @return {TileInfo}
1701
+ */
1702
+ setColumns(columns?: number): TileInfo;
1694
1703
  /**
1695
1704
  * Returns a tile info for an index using this tile as reference
1696
1705
  * @param {Vector2|number} [index=0]
@@ -5772,4 +5781,125 @@ declare module "littlejsengine" {
5772
5781
  /** Copy this object's transform to the mesh */
5773
5782
  syncMesh(): void;
5774
5783
  }
5784
+ /**
5785
+ * LittleJS Texture Sheet Plugin
5786
+ * - Packs images into texture sheets as they are loaded
5787
+ * - Sprites are placed automatically, callers get a TileInfo
5788
+ * - Sheets are created and filled as needed
5789
+ * - Sheets fill in call order, images decode in parallel
5790
+ * - Animation frames keep layout and wrap across rows as needed
5791
+ * - WebGL textures upload once per batch of loads
5792
+ * - loadAtlas imports pre-packed atlases (TexturePacker and Aseprite json)
5793
+ * @namespace TextureSheets
5794
+ */
5795
+ /** Width and height in pixels of texture sheets created by loadSprite
5796
+ * @type {number}
5797
+ * @default
5798
+ * @memberof Settings */
5799
+ export let textureSheetSize: number;
5800
+ /** Default padding pixels around each frame packed by loadSprite
5801
+ * @type {number}
5802
+ * @default
5803
+ * @memberof Settings */
5804
+ export let textureSheetPadding: number;
5805
+ /** Set width and height in pixels of texture sheets created by loadSprite
5806
+ * @param {number} size
5807
+ * @memberof Settings */
5808
+ export function setTextureSheetSize(size: number): void;
5809
+ /** Set default padding pixels around each frame packed by loadSprite
5810
+ * @param {number} padding
5811
+ * @memberof Settings */
5812
+ export function setTextureSheetPadding(padding: number): void;
5813
+ /** Array of texture sheets created by loadSprite
5814
+ * @type {Array<TextureSheet>}
5815
+ * @memberof TextureSheets */
5816
+ export let textureSheets: Array<TextureSheet>;
5817
+ /**
5818
+ * Texture Sheet - A texture that images are packed into as they load
5819
+ * Uses shelf packing, images are placed left to right then wrap to a new row
5820
+ * @memberof TextureSheets
5821
+ */
5822
+ export class TextureSheet {
5823
+ /** Create a texture sheet, called automatically by loadSprite
5824
+ * @param {number} [size] - Width and height of the sheet in pixels */
5825
+ constructor(size?: number);
5826
+ /** @property {number} - Width and height of the sheet in pixels */
5827
+ size: number;
5828
+ /** @property {OffscreenCanvas} - Canvas holding the packed images */
5829
+ canvas: OffscreenCanvas;
5830
+ /** @property {OffscreenCanvasRenderingContext2D} - 2d context for the canvas */
5831
+ context: OffscreenCanvasRenderingContext2D;
5832
+ /** @property {TextureInfo} - The texture info for this sheet */
5833
+ textureInfo: TextureInfo;
5834
+ /** @property {Vector2} - Where the next image will be packed */
5835
+ cursor: Vector2;
5836
+ /** @property {number} - Height of the row being packed */
5837
+ rowHeight: number;
5838
+ /** @property {boolean} - Has the canvas changed since the last webgl upload? */
5839
+ glDirty: boolean;
5840
+ /** Find a spot for an image on this sheet without drawing it
5841
+ * @param {Vector2} imageSize - Size of the source image in pixels
5842
+ * @param {Vector2} [frameSize] - Size of each frame, or the whole image if not passed
5843
+ * @param {number} [padding] - How many pixels padding around each frame
5844
+ * @return {TileInfo} Tile for the packed image, or undefined if the sheet is full */
5845
+ tryAdd(imageSize: Vector2, frameSize?: Vector2, padding?: number): TileInfo;
5846
+ /** Draw an image into this sheet at a tile returned by tryAdd
5847
+ * @param {HTMLImageElement} image - Source image to copy from
5848
+ * @param {TileInfo} tileInfo - Where to put it, from tryAdd
5849
+ * @param {boolean} [update] - Upload to webgl now, pass false when batching */
5850
+ drawImage(image: HTMLImageElement, tileInfo: TileInfo, update?: boolean): void;
5851
+ /** Upload the canvas to webgl if it has changed since the last upload
5852
+ * Only needed after batching, drawImage uploads automatically by default */
5853
+ updateTexture(): void;
5854
+ }
5855
+ /** Load an image and pack it into a texture sheet
5856
+ * - Returns a TileInfo immediately which is filled in when the image loads
5857
+ * - Nothing is visible until it loads, use spritesReady to wait for it
5858
+ * - Pass frameSize for animations, then step through them with TileInfo.frame
5859
+ * - Grid images keep their layout and frames wrap down to the next row
5860
+ * @param {string} src - Image source path
5861
+ * @param {Vector2|number} [frameSize] - Size of each animation frame in pixels
5862
+ * @param {number} [padding] - How many pixels padding around each frame
5863
+ * @return {TileInfo}
5864
+ * @example
5865
+ * const playerTile = loadSprite('player.png'); // a single sprite
5866
+ * const runTile = loadSprite('run.png', vec2(16)); // a 16x16 frame animation
5867
+ * @memberof TextureSheets */
5868
+ export function loadSprite(src: string, frameSize?: Vector2 | number, padding?: number): TileInfo;
5869
+ /** Load a pre-packed texture atlas and repack it onto texture sheets
5870
+ * - Supports TexturePacker json (hash and array) and Aseprite json
5871
+ * - Returns an empty object which is filled with TileInfos when loaded
5872
+ * - Frames are named by the json, animations are grouped automatically
5873
+ * - Aseprite frame tags become animations, so do names like run_0, run_1
5874
+ * - Trimmed frames are restored to their full source size when packed
5875
+ * - Rotated frames are rotated back upright when packed
5876
+ * @param {string} imageSrc - Atlas image path
5877
+ * @param {string|Object} jsonSrc - Atlas json path, or already parsed json data
5878
+ * @param {number} [padding] - How many pixels padding around each frame
5879
+ * @return {Object} Object mapping frame and animation names to TileInfos
5880
+ * @example
5881
+ * const atlas = loadAtlas('sprites.png', 'sprites.json');
5882
+ * await spritesReady();
5883
+ * drawTile(pos, size, atlas.player); // a single frame
5884
+ * drawTile(pos, size, atlas.run.frame(2)); // frame 2 of the run animation
5885
+ * @memberof TextureSheets */
5886
+ export function loadAtlas(imageSrc: string, jsonSrc: string | any, padding?: number): any;
5887
+ /** Parse atlas json into a list of named frame groups, used by loadAtlas
5888
+ * - Accepts TexturePacker json (hash and array) and Aseprite json
5889
+ * - Frames tagged in Aseprite or named like run_0, run_1 group into animations
5890
+ * @param {Object} data - Parsed atlas json data
5891
+ * @return {Array<Object>} List of {name, frames} groups in atlas order
5892
+ * @memberof TextureSheets */
5893
+ export function parseAtlas(data: any): Array<any>;
5894
+ /** Wait for everything started by loadSprite and loadAtlas to finish packing
5895
+ * @return {Promise}
5896
+ * @example
5897
+ * async function gameInit()
5898
+ * {
5899
+ * playerTile = loadSprite('player.png');
5900
+ * runTile = loadSprite('run.png', vec2(16));
5901
+ * await spritesReady();
5902
+ * }
5903
+ * @memberof TextureSheets */
5904
+ export function spritesReady(): Promise<any>;
5775
5905
  }