๐งฉ SpriteComponent
The SpriteComponent is used to render 2D images and sprite sheets in your game. It supports positioning, scaling, rotation, flipping, transparency, and sprite sheet animations.
๐ฆ Import
js
import { SpriteComponent } from "kernelplay-js";
โ๏ธ Usage
Attach it to an entity to display an image:
js
entity.addComponent("renderer", new SpriteComponent({
image: "./assets/player.png",
width: 64,
height: 64
}));
๐งพ Properties
๐ผ๏ธ Image
image(string | Image)
Path to the image or an already loadedImageobject.
๐ Size
width(number) โ Render widthheight(number) โ Render height
๐๏ธ Sprite Sheet
sourceX(number) โ X position inside the sprite sheetsourceY(number) โ Y position inside the sprite sheetsourceWidth(number) โ Width of a framesourceHeight(number) โ Height of a frame
๐ If sourceWidth or sourceHeight is not set, the full image is used.
โ Anchor (Pivot)
anchor({x, y})
Controls the origin point of the sprite:
markdown
| Value | Meaning |
|------|--------|
| `{x: 0, y: 0}` | Top-left |
| `{x: 0.5, y: 0.5}` | Center (default) |
| `{x: 1, y: 1}` | Bottom-right |
๐ Flip
flipX(boolean) โ Flip horizontallyflipY(boolean) โ Flip vertically
๐จ Appearance
alpha(number) โ Transparency (0 to 1)tint(string) โ Color tint (optional)
๐งฑ Layering
zIndex(number) โ Controls render order
๐๏ธ Sprite Sheet Example
js
const sprite = new SpriteComponent({
image: "./assets/player_sheet.png",
sourceWidth: 64,
sourceHeight: 64,
width: 50,
height: 50
});
๐ฌ Changing Frames
Use setFrame() to display a specific frame from a sprite sheet:
js
sprite.setFrame(64, 0, 64, 64);
๐ Flip Example
js
sprite.flipX = true; // Mirror character
๐ป Transparency Example
js
sprite.alpha = 0.5; // 50% transparent
๐ฏ Full Example
js
entity.addComponent("renderer", new SpriteComponent({
image: "./assets/player_sheet.png",
sourceWidth: 64,
sourceHeight: 64,
width: 50,
height: 50,
anchor: { x: 0.5, y: 0.5 },
alpha: 1,
flipX: false,
zIndex: 10
}));
๐ก Tips
- Use sprite sheets for animations (with an animator system)
- Keep
anchorat{0.5, 0.5}for easier rotation - Match
sourceWidth/Heightwith your sprite sheet grid - Use
flipXinstead of separate left/right sprites