nacatamalon/pixels
#blitPixelsfunctionnacatamalon/pixels
blitPixels(target: TPixels, source: TPixels, x: number, y: number, options: { flipX, flipY }) => TPixels
Lays one picture over another, the way a sprite is drawn over a background: a transparent pixel
of source leaves what was there, an opaque one replaces it, and one in between mixes the two.
It is how a sprite is put together from parts (a car from a body, wheels and a driver) or stamped
many times over a bigger picture.
Parameters
targetTPixelsThe picture drawn on.
sourceTPixelsThe picture drawn.
xnumberWhere source's top-left corner lands in target.
ynumberWhere source's top-left corner lands in target.
options{ flipX, flipY }flipX and flipY mirror source as it is drawn.
View source · pixels/draw.ts:478#checkColorLimitfunctionnacatamalon/pixels
checkColorLimit(pixels: TPixels, options: TColorLimitOptions) => TColorLimitCell[]
Says which parts of a picture use more colours than they may, as the machines of the era limited
them: three and transparent in a NES sprite's 8 by 8, four in a Game Boy's.
It only looks. Nothing is warned about or changed: what comes back is for a test to check, or for
whoever is drawing to read and correct.
Parameters
pixelsTPixelsThe picture.
optionsTColorLimitOptionsHow many colours, and in what size of part: see TColorLimitOptions.
Example
declare const hero: TPixels;
const over = checkColorLimit(hero, { cell: 8, colors: 3 });
for (const cell of over) {
console.log(`At ${cell.x}, ${cell.y}: ${cell.colors.join(' ')}`);
}
View source · pixels/palette_colors.ts:193#clonePixelsfunctionnacatamalon/pixels
clonePixels(pixels: TPixels) => TPixels
A copy of a picture that can be painted on without touching the original.
Parameters
pixelsTPixelsThe picture to copy.
View source · pixels/edit.ts:15#createPixelsfunctionnacatamalon/pixels
createPixels(width: number, height: number, fill?: TColorOption) => TPixels
A new picture to paint on in code, transparent or filled with one colour: what to use instead of a
<canvas> whenever a picture has to be generated rather than drawn.
Drawn art (characters, tiles, backgrounds) is made in a pixel-art editor and loaded with
useLoadTexture. This is for the pictures nobody draws by hand: procedural variations, placeholders,
noise and masks for a shader, a picture that changes while the game runs, or art made by an agent
that has no image editor. A canvas only exists in a browser; this runs the same there, on the
native runtime and in a test.
It lives in memory only: paint it with fillRect, drawLine, fillGradient and the rest, then
make it a texture with createPixelTexture. It needs no game, so a build script or a test can make
one too.
Parameters
widthnumberIn pixels, at least 1.
heightnumberIn pixels, at least 1.
filloptionalTColorOptionThe colour every pixel starts as. Left out, fully transparent.
Example
const tile = createPixels(16, 16, '#2a6b3a');
fillRect(tile, 0, 15, 16, 1, '#1a4a28');
View source · pixels/create_pixels.ts:34#createPixelTexturefunctionnacatamalon/pixels
createPixelTexture(source: TPixels | TLoadedPixels, paint: TPixelPaint, options?: TPixelTextureOptions) => TTexture
createPixelTexture(source: TPixels | TLoadedPixels, options?: TPixelTextureOptions) => TTexture
Turns a picture into a texture a sprite, a model's material or a shader shows like any image loaded
from a file.
It is how a game turns a generated picture into something it can show, with no image file and no
page: a procedural sky, a rock texture from noise for a model, a mask for a shader, a placeholder
while the real art is drawn. Art someone drew is loaded with useLoadTexture instead. It works the
same in the browser and on the native runtime, because nothing here draws with the page. Use it
instead of a <canvas>: a canvas only exists in a browser.
The picture can be:
- One painted in code (
createPixels). The texture is ready at once and keeps reading from the
picture: paint on it again and call updatePixelTexture to show the change (a minimap filling in,
a floor with a crater in it). paint, if given, is applied to it first.
- A drawn picture loaded to be processed (
useLoadPixels). The texture comes back at once,
still loading, the way one from useLoadTexture does, and whatever shows it appears when the file
has arrived. paint then receives a copy of the picture, so the loaded one, shared by everyone
who loaded the file, is never changed: a palette swap, a white silhouette for a hit, an outline.
Asked again for a key it already made at the same size, it gives that texture back with the new
picture uploaded into it, so a scene that restarts does not pile up textures.
A scene document can name a texture made here but cannot carry it, since there is no file to fetch:
make it again under the same key before loading the document.
Parameters
sourceTPixels | TLoadedPixelsThe picture: one from createPixels, or one from useLoadPixels.
paintTPixelPaintChanges the picture before it is uploaded. With a loaded picture it gets a copy.
optionsoptionalTPixelTextureOptionskey to keep it by name, mipmaps to make its mipmaps.
Example
const Level = () => {
// Painted in code.
const art = createPixels(16, 16, '#1d2b53');
fillCircle(art, 8, 8, 6, '#ffec27');
createSprite({ texture: createPixelTexture(art), width: 64, height: 64 });
// A drawn sprite in another colour, made from the one file.
const hero = useLoadPixels({ src: '/assets/hero.png' });
const red = createPixelTexture(hero, (p) => swapColors(p, [['#3a7bff', '#e23d3d']]));
createSprite({ texture: red, width: 32, height: 32 });
return createScene();
};
View source · gameobjects/pixel_texture/create_pixel_texture.ts:141#drawCirclefunctionnacatamalon/pixels
drawCircle(pixels: TPixels, cx: number, cy: number, radius: number, color: TColorOption) => TPixels
Draws the outline of a circle, one pixel thick, with no gaps and no pixel drawn twice.
Parameters
pixelsTPixelsThe picture.
cxnumberIts centre, from the left.
cynumberIts centre, from the top.
colorTColorOptionIts colour.
View source · pixels/draw.ts:227#drawEllipsefunctionnacatamalon/pixels
drawEllipse(pixels: TPixels, cx: number, cy: number, rx: number, ry: number, color: TColorOption) => TPixels
Draws the outline of an ellipse, one pixel thick and with no gaps. With both radii equal it is
drawCircle, pixel for pixel.
Parameters
pixelsTPixelsThe picture.
cxnumberIts centre, from the left.
cynumberIts centre, from the top.
rxnumberHalf its width, in pixels.
rynumberHalf its height, in pixels.
colorTColorOptionIts colour.
View source · pixels/draw.ts:377#drawLinefunctionnacatamalon/pixels
drawLine(pixels: TPixels, x0: number, y0: number, x1: number, y1: number, color: TColorOption) => TPixels
Draws a line one pixel thick between two points, with the stepped edge of the era and no
smoothing: each pixel is either on the line or not.
Parameters
pixelsTPixelsThe picture.
x0numberWhere it starts, from the left.
y0numberWhere it starts, from the top.
x1numberWhere it ends, from the left.
y1numberWhere it ends, from the top.
colorTColorOptionIts colour.
View source · pixels/draw.ts:112#drawTextfunctionnacatamalon/pixels
drawText(pixels: TPixels, text: string, x: number, y: number, options: TDrawTextOptions) => TPixels
Writes text into a picture: the name on a sign, a number on a door, a label on a crate. Once it is
in the pixels it is part of the art, and turns, scales and lights with whatever shows it.
It lays the text out exactly as createText does, with the same fontSize, align, maxWidth
and spacing, and works with either kind of font: a bitmap font is copied pixel for pixel, and a vector
font is drawn from its outlines at the size asked for.
With no font it needs nothing at all: the font the engine carries is already inside it, so a
build script or a test can write text too. A loaded font has to have arrived. One loaded in an
earlier scene and named by its key is ready; in the scene that loads it, draw in a useWatch on
the font, which runs again once it is here, and send the change on with updatePixelTexture.
Until then this draws nothing and says nothing.
Parameters
pixelsTPixelsThe picture.
textstringWhat to write. \n starts a new line.
xnumberWhere it goes, from the left: the block's top-left corner unless anchor says another.
ynumberWhere it goes, from the top.
optionsTDrawTextOptionsThe font, the colour, the size and the spacing.
Example
const sign = createPixels(64, 20, '#2a1a3a');
drawText(sign, 'MOTEL', 32, 10, { anchor: { x: 0.5, y: 0.5 }, color: '#ff4fd8' });
createSprite({ texture: createPixelTexture(sign) });
Example
// A loaded font, in the scene that loads it.
const title = useLoadFont({ src: '/fonts/Bangers-Regular.ttf' });
const board = createPixels(128, 40);
const texture = createPixelTexture(board);
useWatch(() => {
drawText(board, 'SUNSET', 64, 20, { font: title, fontSize: 32, anchor: { x: 0.5, y: 0.5 } });
updatePixelTexture(texture);
}, [title]);
View source · pixels/draw_text.ts:221#fillCheckerfunctionnacatamalon/pixels
fillChecker(pixels: TPixels, size: number, a: TColorOption, b: TColorOption) => TPixels
Fills the picture with a checkerboard: the first texture every 3D scene is tried with, because it
shows at a glance whether a surface is stretched, mirrored or seen from the wrong side.
Parameters
pixelsTPixelsThe picture.
sizenumberHow many pixels wide one square is.
aTColorOptionThe colour of the top-left square.
bTColorOptionThe other colour.
View source · pixels/fill.ts:254#fillCirclefunctionnacatamalon/pixels
fillCircle(pixels: TPixels, cx: number, cy: number, radius: number, color: TColorOption) => TPixels
Fills a circle. Its edge is stepped, one pixel or none, the way a circle drawn by hand on a grid
looks, and it is exactly the edge drawCircle draws for the same radius.
Painted with a transparent colour, it cuts a round hole: the crater in a destructible floor.
Parameters
pixelsTPixelsThe picture.
cxnumberIts centre, from the left.
cynumberIts centre, from the top.
radiusnumberIn pixels. 0 is a single pixel.
colorTColorOptionWhat it is filled with.
Example
const sun = createPixels(33, 33);
fillCircle(sun, 16, 16, 15, '#ffd75a');
View source · pixels/draw.ts:173#fillDitherfunctionnacatamalon/pixels
fillDither(pixels: TPixels, x: number, y: number, width: number, height: number, a: TColorOption, b: TColorOption, amount: number) => TPixels
Fills a rectangle with two colours in the ordered pattern of the era: a shadow or a slope of light
on a picture with few colours, or a see-through patch when one of them is transparent.
The dots are laid out by where they are on the picture, so two patches side by side meet without a
seam, and in the same places the dither() effect and fillGradient put theirs.
Parameters
pixelsTPixelsThe picture.
xnumberThe rectangle's left edge.
widthnumberHow wide, in pixels.
heightnumberHow tall, in pixels.
aTColorOptionThe colour it is made of when amount is 0.
bTColorOptionThe colour it is made of when amount is 1.
amountnumberHow much of b, from 0 to 1, in sixteen steps. Default 0.5, one and one.
Example
const wall = createPixels(32, 32, '#6d4c8f');
// A shadow that thickens to the bottom.
fillDither(wall, 0, 16, 32, 8, '#6d4c8f', '#3a2a55', 0.25);
fillDither(wall, 0, 24, 32, 8, '#6d4c8f', '#3a2a55', 0.75);
View source · pixels/fill.ts:296#fillEllipsefunctionnacatamalon/pixels
fillEllipse(pixels: TPixels, cx: number, cy: number, rx: number, ry: number, color: TColorOption) => TPixels
Fills an ellipse: a circle stretched to rx across and ry down, for bodies, shadows under a
character, a lake seen from above. Its edge is stepped and exactly the one drawEllipse draws
for the same radii. With both radii equal it is fillCircle, pixel for pixel.
Parameters
pixelsTPixelsThe picture.
cxnumberIts centre, from the left.
cynumberIts centre, from the top.
rxnumberHalf its width, in pixels. 0 is a vertical line.
rynumberHalf its height, in pixels. 0 is a horizontal line.
colorTColorOptionWhat it is filled with.
Example
const sheep = createPixels(24, 16);
fillEllipse(sheep, 12, 8, 10, 6, '#f4f1e8');
View source · pixels/draw.ts:330#fillGradientfunctionnacatamalon/pixels
fillGradient(pixels: TPixels, options: TFillGradientOptions) => TPixels
Fills the picture with a gradient, smooth or cut into the flat bands of a sky from the era, with an
ordered dither between the bands if you want it.
Parameters
pixelsTPixelsThe picture.
optionsTFillGradientOptionsThe colours, the direction and how it is stepped.
Example
const sky = createPixels(320, 120);
fillGradient(sky, {
colors: ['#140a33', '#5a1667', '#e0446e', '#ffb36b'],
bands: 12,
dither: true,
});
View source · pixels/fill.ts:80#fillNoisefunctionnacatamalon/pixels
fillNoise(pixels: TPixels, options: TFillNoiseOptions) => TPixels
Fills the picture with smooth noise: clouds, rock, water, a height map for terrain, or a texture
for a shader to read.
It tiles without a seam: the right edge runs on into the left one and the bottom into the top,
so a model can repeat it (wrap: 'repeat') with no visible joins. For that, the blobs are fitted
to a whole number across the picture, so scale is followed as closely as that allows.
Parameters
pixelsTPixelsThe picture.
optionsTFillNoiseOptionsWhich noise, how big and between which colours.
Example
// A tiling rock texture for a model.
const rock = fillNoise(createPixels(64, 64), {
seed: 7, scale: 16, octaves: 4, from: '#2b2730', to: '#8a8090',
});
createMaterial({ name: 'rock', shader: 'mesh3d', texture: createPixelTexture(rock), wrap: 'repeat' });
View source · pixels/fill.ts:187#fillPolygonfunctionnacatamalon/pixels
fillPolygon(pixels: TPixels, points: typeOperator, color: TColorOption) => TPixels
Fills a polygon: a ramp, a slope of ground, a roof, a shard. A pixel is painted when its middle is
inside the outline or on it, so the edge is stepped, one pixel or none, a shape and its mirror image
are painted alike (a ramp up and a ramp down), and two polygons that share a side leave no gap
along it. An outline that crosses itself leaves the parts it wraps twice empty, as a star drawn in
one stroke does.
Parameters
pixelsTPixelsThe picture.
pointstypeOperatorIts corners in order, around either way, in pixels from the top-left corner. Fewer
than three paint nothing.
colorTColorOptionWhat it is filled with.
Example
// A ramp rising to the right, 96 wide and 48 high.
const ramp = createPixels(96, 48);
fillPolygon(ramp, [{ x: 0, y: 48 }, { x: 96, y: 0 }, { x: 96, y: 48 }], '#8a5a2b');
View source · pixels/draw.ts:426#fillRectfunctionnacatamalon/pixels
fillRect(pixels: TPixels, x: number, y: number, width: number, height: number, color: TColorOption) => TPixels
Fills a rectangle with one colour, cut to the picture's edges.
Parameters
pixelsTPixelsThe picture.
widthnumberHow wide, in pixels.
heightnumberHow tall, in pixels.
colorTColorOptionWhat it is filled with.
Example
const bar = createPixels(64, 6, '#200a0a');
fillRect(bar, 1, 1, 40, 4, '#e23d3d');
View source · pixels/draw.ts:82#getPixelfunctionnacatamalon/pixels
getPixel(pixels: TPixels, x: number, y: number) => TColor
Reads one pixel. Outside the picture it is fully transparent black.
Reading the picture is also how a game asks questions of it: whether a pixel of a destructible
floor is still solid, which colour a minimap shows somewhere.
Parameters
pixelsTPixelsThe picture.
xnumberFrom the left edge, in pixels.
ynumberFrom the top edge, in pixels.
View source · pixels/draw.ts:50#mapPixelsfunctionnacatamalon/pixels
mapPixels(pixels: TPixels, fn: (color: TColor, x: number, y: number) => TColor) => TPixels
Works out every pixel again from what it is and where it is: for anything the other functions do
not draw. A recolour, a shading by height, a pattern of your own.
Parameters
pixelsTPixelsThe picture.
fn(color: TColor, x: number, y: number) => TColorGiven each pixel's colour and position, returns what it becomes.
Example
declare const wall: TPixels;
declare const mortar: TColor;
declare const brick: TColor;
// Brick: rows of 8 pixels, every other row shifted by half a brick, with a darker mortar line.
mapPixels(wall, (color, x, y) =>
y % 8 === 0 || (x + (Math.floor(y / 8) % 2) * 8) % 16 === 0 ? mortar : brick);
View source · pixels/draw.ts:550#mirrorPixelsfunctionnacatamalon/pixels
mirrorPixels(pixels: TPixels, axis: 'x' | 'y', options?: TMirrorPixelsOptions) => TPixels
Copies one half of a picture onto the other, turned round: draw half of something symmetric, and
this draws the rest.
'x' copies the left half onto the right, 'y' the top half onto the bottom. What the half
written over had is replaced, transparent pixels included. On an odd size the middle row or column
stays as it is.
Parameters
pixelsTPixelsThe picture, changed in place.
axis'x' | 'y'Which way: 'x' left onto right, 'y' top onto bottom.
optionsoptionalTMirrorPixelsOptionsThe part of the picture: see TMirrorPixelsOptions.
Example
// The left half and the middle column of a robot. The right half is copied.
const robot = pixelsFromRows([
'....k....',
'.kkkk....',
'kwwkk....',
'kkkkk....',
], { k: '#14141f', w: '#ffffff' });
mirrorPixels(robot, 'x');
View source · pixels/edit.ts:212#normalsFromHeightfunctionnacatamalon/pixels
normalsFromHeight(heights: TPixels, options: TNormalsFromHeightOptions) => TPixels
Makes a normal map from a picture of heights: bright is high and dark is low, so the stone texture
itself, its mortar dark, gives the relief of its bricks. Give the result to a material's normalMap
and its surface catches the light as carved.
Each pixel is the way the surface faces there, worked out from how fast the height changes across
and up the picture, as the usual lilac maps hold it: red across, green up the picture, blue out of
the surface.
Parameters
heightsTPixelsThe picture of heights: any picture, read by its brightness.
optionsTNormalsFromHeightOptionsHow deep the relief is, and whether it repeats.
Example
import { createMesh, useCubeGeometry } from 'nacatamalon';
import { createPixelTexture, normalsFromHeight } from 'nacatamalon/pixels';
import type { TPixels } from 'nacatamalon/pixels';
declare const bricks: TPixels;
createMesh({
geometry: useCubeGeometry(),
texture: createPixelTexture(bricks),
normalMap: createPixelTexture(normalsFromHeight(bricks, { depth: 4 })),
});
View source · pixels/normals_from_height.ts:64#outlinePixelsfunctionnacatamalon/pixels
outlinePixels(pixels: TPixels, color: TColorOption, options?: TOutlinePixelsOptions) => TPixels
Draws a border one pixel wide around everything that is not fully transparent. It goes on the
transparent pixels around the shape, never over the shape, and is worked out from the picture as
it was, so it never outlines itself.
The picture does not grow: a shape touching the edge has no outline on that side, so draw it with
a pixel of room around it.
Parameters
pixelsTPixelsThe picture, changed in place.
colorTColorOptionThe border's colour.
optionsoptionalTOutlinePixelsOptionsIts corners, and the part of the picture: see TOutlinePixelsOptions.
Example
const heart = pixelsFromRows([
'.........',
'..rr.rr..',
'.rrrrrrr.',
'..rrrrr..',
'...rrr...',
'....r....',
'.........',
], { r: '#f80058' });
outlinePixels(heart, '#14141f');
View source · pixels/edit.ts:135#paintMatcapfunctionnacatamalon/pixels
paintMatcap(options: TPaintMatcapOptions) => TPixels
Paints a matcap: a ball lit by one light, seen from in front, the picture matcap colours a model
with. Each pixel of the disc is where a surface facing that way shows on the ball, worked out the
way the shader reads it back, so a model lit by it looks lit the same way. Soft light that wraps a
little round the dark side, a highlight, and an edge a touch darker; past the disc, its rim.
A matcap rendered in a 3D program, or a picture of one, does the same and can be anything (two
lights, a coloured rim, a hand-painted shine): this one is for a game that wants them without
files, or to try a colour quickly.
Parameters
optionsTPaintMatcapOptionsThe colours, how glossy, where the light comes from and how big. All optional.
Example
import { createMaterial, createMesh, matcap, useTorusGeometry } from 'nacatamalon';
import { createPixelTexture, paintMatcap } from 'nacatamalon/pixels';
const teal = createPixelTexture(paintMatcap({ base: '#3fb3b0', shade: '#123c4a', gloss: 0.7 }));
createMesh({ geometry: useTorusGeometry(), material: createMaterial(matcap(teal)) });
View source · pixels/paint_matcap.ts:87#pixelsFromRowsfunctionnacatamalon/pixels
pixelsFromRows(rows: typeOperator, palette: TPixelPalette, options?: TPixelRowsOptions) => TPixels
A picture drawn as rows of characters, one a pixel, and a palette saying what colour each one is:
the way pixel art is written in code. A grid reads in a diff, is edited by hand, and is far
harder to get wrong than working out the corners of a fillRect for every patch of colour.
Top row first, every row as wide. '.' and ' ' are left transparent unless the palette names
them. A character missing from the palette, or a row of another width, throws, saying which row
and column.
It needs no game, so a test or a build script can draw one too. Make it a texture with
createPixelTexture; several frames for an animation are sheetFromRows.
Parameters
rowstypeOperatorThe picture, top row first, one character a pixel.
paletteTPixelPaletteWhat colour each character is.
optionsoptionalTPixelRowsOptionsscale, to draw each character as a bigger square.
Example
const heart = pixelsFromRows([
'.rr.rr.',
'rRrrrrr',
'rrrrrrr',
'.rrrrr.',
'..rrr..',
'...r...',
], { r: '#f80058', R: '#fc98b8' });
createSprite({ texture: createPixelTexture(heart) });
View source · pixels/pixels_from_rows.ts:230#setPixelfunctionnacatamalon/pixels
setPixel(pixels: TPixels, x: number, y: number, color: TColorOption) => TPixels
Sets one pixel. Outside the picture it does nothing.
Parameters
pixelsTPixelsThe picture.
xnumberFrom the left edge, in pixels.
ynumberFrom the top edge, in pixels.
colorTColorOptionWhat it becomes, replacing what was there.
View source · pixels/draw.ts:25#shadeWithRampfunctionnacatamalon/pixels
shadeWithRamp(pixels: TPixels, ramps: typeOperator | typeOperator, options?: TShadeWithRampOptions) => TPixels
Gives flat colours a side lit and a side in shadow: each pixel on the edge of a shape that faces
the light moves one step up its ramp, and each one on the edge facing away moves one step down.
What is in between stays, and so does a pixel with an edge on both sides.
A ramp is a list of colours from darkest to lightest, and a colour in none of them is left alone,
so skin and cloth each shade along their own. The shape is everything that is not fully transparent.
Alpha stays as it was.
Parameters
pixelsTPixelsThe picture, changed in place.
rampstypeOperator | typeOperatorOne ramp, or a list of them, each colour as anything getColor reads. Colours are
matched exactly. A colour at the end of its ramp has nowhere further to go, and stays.
optionsoptionalTShadeWithRampOptionsWhere the light comes from, and the part of the picture: see
TShadeWithRampOptions.
Example
const ball = pixelsFromRows(['.mmm.', 'mmmmm', 'mmmmm', 'mmmmm', '.mmm.'], { m: '#c8553d' });
shadeWithRamp(ball, ['#7a2e22', '#c8553d', '#f0a37a']);
View source · pixels/palette_colors.ts:275#sheetFromRowsfunctionnacatamalon/pixels
sheetFromRows(frames: Readonly<Record<string, typeOperator>> | typeOperator, palette: TPixelPalette, options?: TPixelRowsOptions) => TPixelSheet
Several frames drawn as rows, as pixelsFromRows draws one, laid side by side in one picture:
a sheet ready to be cut by createSpriteAtlas.
Given by name ({ stand, walkA, walkB }), frames says which number each one is, so a sprite's
animations name them instead of counting: { walk: { frames: [hero.frames.walkA, hero.frames.walkB] } }. Given as a list, they are numbered in its order. Every frame is the same
size, and one that is not throws, naming it.
By name, they are laid in the order JavaScript keeps an object's names, which is the order they
were written in except that names which are whole numbers ({ 2: … }) come first. Frames that are
numbered are a list.
Parameters
framesReadonly<Record<string, typeOperator>> | typeOperatorEach frame's rows, by name or in order.
paletteTPixelPaletteWhat colour each character is, shared by every frame.
optionsoptionalTPixelRowsOptionsscale, to draw each character as a bigger square.
Example
declare const stand: string[], walkA: string[], walkB: string[];
const hero = sheetFromRows({ stand, walkA, walkB }, { h: '#3bceac', s: '#ffd23f' });
const sprite = createSprite({
atlas: createSpriteAtlas({ texture: createPixelTexture(hero.pixels), columns: hero.columns }),
});
useSpriteAnimation(sprite, { play: 'walk', clips: { walk: { frames: [hero.frames.walkA, hero.frames.walkB], fps: 6 } } });
View source · pixels/pixels_from_rows.ts:274#snapToPalettefunctionnacatamalon/pixels
snapToPalette(pixels: TPixels, palette: typeOperator, options?: TSnapToPaletteOptions) => TPixels
Moves every pixel to the nearest colour of a palette, by the same measure paletteMatch uses for
the screen. Alpha stays as it was, and what is fully transparent is left alone.
It is paletteMatch for one picture, while it is being made: a generated sprite that really has
only those colours, whatever it was drawn with.
Parameters
pixelsTPixelsThe picture, changed in place.
palettetypeOperatorThe colours, as anything getColor reads: BUILTIN.PALETTES.nes, or a list of
your own. The alpha of each is not looked at.
optionsoptionalTSnapToPaletteOptionsThe part of the picture: see TSnapToPaletteOptions.
Example
const sky = createPixels(64, 32);
fillGradient(sky, { from: '#203060', to: '#f0a070' });
snapToPalette(sky, BUILTIN.PALETTES.nes);
View source · pixels/palette_colors.ts:81#sphereFromPanoramafunctionnacatamalon/pixels
sphereFromPanorama(panorama: TPixels, options: TSphereFromPanoramaOptions) => TPixels
Turns a panorama, or a plain photo, into the round picture a material's reflection reads: the
surroundings as a ball of chrome would show them seen from in front.
The middle of the panorama lands in the middle of the round picture, which is what is behind the
camera and so what a surface facing it reflects: a ball of chrome shows the middle of the photo
where it faces you, and the rest round towards its rim. Up stays up, and left and right swap, as in
any mirror: what is on the photo's right is behind the camera on its left. A whole panorama (across: 360) goes all the way round; a photo covers what it covers, and the rest is it mirrored. Each pixel
is painted by the direction it reflects, worked out as the shader reads it back.
Parameters
panoramaTPixelsThe picture, a panorama or a photo, loaded (useLoadPixels) or painted.
optionsTSphereFromPanoramaOptionsHow far round it reaches, and how big the round picture is.
Example
import { createMaterial, createMesh, chrome, useSphereGeometry } from 'nacatamalon';
import { createPixelTexture, sphereFromPanorama, useLoadPixels } from 'nacatamalon/pixels';
const photo = useLoadPixels({ src: '/assets/canal.png' });
const surroundings = createPixelTexture(photo, (pixels) => sphereFromPanorama(pixels, { across: 120 }));
createMesh({ geometry: useSphereGeometry(), material: createMaterial(chrome({ environment: surroundings })) });
View source · pixels/sphere_from_panorama.ts:85#studioEnvironmentfunctionnacatamalon/pixels
studioEnvironment(options: TStudioEnvironmentOptions) => TPixels
Paints the surroundings of a photographer's studio as a round picture, the kind a material's
reflection reads: a bright sky above, a floor below, and white softboxes, the look of the glossy
plastic and chrome of the era's computer graphics.
The picture is the surroundings as a ball of chrome seen from in front would show them: its middle
is what is behind the camera, where the softboxes stand, and its rim what is behind the model. Each
pixel is painted by the direction it reflects, worked out the way the shader reads it back, so a
softbox lands where it should on the model and not stretched along the rim.
Parameters
optionsTStudioEnvironmentOptionsThe colours, how many softboxes and how big the picture is. All optional.
Example
import { createMesh, useSphereGeometry } from 'nacatamalon';
import { createPixelTexture, studioEnvironment } from 'nacatamalon/pixels';
const studio = createPixelTexture(studioEnvironment({ ground: '#3a2a22', panels: 3 }));
createMesh({ geometry: useSphereGeometry(), tint: '#c0c4cc', perPixel: true, reflection: { texture: studio, front: 0.9 } });
View source · pixels/studio_environment.ts:97#swapColorsfunctionnacatamalon/pixels
swapColors(pixels: TPixels, pairs: typeOperator) => TPixels
Replaces colours with others, exactly: every pixel that is one of the from colours, alpha
included, becomes its to. Every other pixel is left alone.
It is the palette swap of the consoles of the era: one drawn enemy, and the red, blue and gold
versions made from it, with no extra picture drawn. Swaps are worked out from the picture as it
was, so swapping red for blue and blue for red at once trades them rather than turning both blue.
Parameters
pixelsTPixelsThe picture, changed in place.
pairstypeOperatorEach colour to replace, and what replaces it.
Example
const hero = useLoadPixels({ src: '/assets/hero.png' });
const red = createPixelTexture(hero, (p) => swapColors(p, [['#3a7bff', '#e23d3d']]));
View source · pixels/edit.ts:44#updatePixelTexturefunctionnacatamalon/pixels
updatePixelTexture(texture: TTexture, region?: TPixelRegion) => void
Shows what has been painted on a picture since its texture was made or last updated.
Only the part asked for is sent to the graphics card, so a game that changes a little of a big
picture every frame (a crater, a revealed patch of map) passes region and stays cheap. Left out,
the whole picture is sent.
Parameters
textureTTextureA texture createPixelTexture made.
regionoptionalTPixelRegionThe rectangle that changed, in the picture's pixels. It is cut to the picture.
Example
declare const ground: TPixels;
declare const groundTexture: TTexture;
declare const hitX: number;
declare const hitY: number;
fillCircle(ground, hitX, hitY, 6, 'transparent');
updatePixelTexture(groundTexture, { x: hitX - 6, y: hitY - 6, width: 13, height: 13 });
View source · gameobjects/pixel_texture/create_pixel_texture.ts:308#useLoadPixelsfunctionhooknacatamalon/pixels
useLoadPixels(options: TUseLoadPixelsOptions) => TLoadedPixels
Loads a drawn picture as pixels, to process it in code: a palette swap, a white silhouette for
a hit, an outline, a collision mask taken from the drawing, a level read from an image.
To just show a drawn picture, use useLoadTexture: that is the normal way, and faster, because
the picture goes straight to the graphics card. This is for when the game has to read or change
the drawing first. It reads PNG files, decoded in plain JavaScript with no canvas, so it works the
same in the browser and on the native runtime.
It hands back a record at once, still loading, the way every loader does; useLoadProgress counts it.
Give it straight to createPixelTexture with a function that changes it: the texture appears by
itself when the file arrives, and the function works on a copy. Once status is 'ready', its
pixels can be read as well (with getPixel) but not painted on: the picture is shared by every
scene that loads the same file.
Parameters
optionsTUseLoadPixelsOptionsWhere the file is, and the name to keep it under.
Example
const Level = () => {
const hero = useLoadPixels({ src: '/assets/hero.png' });
const gold = createPixelTexture(hero, (p) => swapColors(p, [['#3a7bff', '#ffd75a']]));
createSprite({ texture: gold, width: 32, height: 32 });
return createScene();
};
View source · hooks/loaders/use_load_pixels.ts:62#TColorLimitCelltypenacatamalon/pixels
A part of a picture using more colours than it may.
Properties
xnumberWhere it starts, in pixels of the picture.
ynumberwidthnumberheightnumbercolorsstring[]The colours it uses, as #rrggbb, in the order they are met.
View source · pixels/palette_colors.ts:147#TColorLimitOptionstypenacatamalon/pixels
What checkColorLimit is asked to check.
Properties
colorsnumberHow many colours each part may use. Fully transparent pixels do not count, and any other is
counted by its red, green and blue.
celloptionalnumber | { width, height }The size of the parts: one number for a square, or { width, height }. Left out, the whole
picture is one. The last ones may be cut by the picture's edge, and are checked all the same.
View source · pixels/palette_colors.ts:127#TDrawTextOptionstypenacatamalon/pixels
TDrawTextOptions: TMeasureTextOptions & { color, anchor, smooth }
How drawText writes: the font, the colour, and the same size and spacing a text on screen takes.
The same font and style measured by measureText are what it draws.
Properties
coloroptionalTColorOptionWhat the letters are painted in. Default white. A bitmap font's own colours are multiplied by
it, the way a text's tint is.
anchoroptional{ x, y }Which point of the block of text lands on x, y, from 0, 0 (its top-left corner, the
default) to 1, 1 (its bottom-right). 0.5, 0.5 centres it on the point, which is what a
sign wants.
smoothoptionalbooleanFor a vector font: soft edges (the default) or hard ones, every pixel in or out, for a
picture that stays pixel art. A bitmap font is copied pixel for pixel either way.
fontoptionalTBitmapFont | TFont | stringA font from useLoadFont or useLoadBitmapFont, or the key one was loaded under. A key is
looked up in the scene body, where createText looks up its own. Left out, the font the
engine carries, which needs no game and no loading.
fontSizeoptionalnumberHow big the letters are, in pixels.
For a vector font (useLoadFont), the size of the font as anywhere else type is set: one em is
this many pixels, and the line is as tall as the font says. Default 16. Any size is sharp.
For a bitmap font, how tall one line is. Default: the font's own height, so the font as drawn. A pixel
font looks crisp at whole multiples of its height (16 or 24 for an 8 pixel font) and uneven in
between.
alignoptional'left' | 'center' | 'right'Where each line sits: inside maxWidth when there is one, inside the width of the longest
line when not. Default 'left'.
maxWidthoptionalnumberHow wide a line can be, in pixels, before its words go on to the next one: a dialogue box, a
panel's description. The text's block is this wide, so align and anchor place the lines
in the box rather than around the words. Measured before the text's scale, like the other
sizes here.
Lines break at spaces; a word wider than the whole width breaks between two letters. \n
still starts a line wherever it is. Changing the text or the size breaks it again on the next
frame. Left out or 0, lines are only broken by \n.
letterSpacingoptionalnumberExtra pixels between two characters. Default 0.
lineSpacingoptionalnumberExtra pixels between two lines. Default 0.
View source · pixels/draw_text.ts:25#TFillGradientOptionstypenacatamalon/pixels
What fillGradient is asked for.
Properties
fromoptionalTColorOptionThe colour it starts at. With to, a gradient between two colours.
tooptionalTColorOptionThe colour it ends at.
colorsoptionaltypeOperatorSeveral colours, evenly spaced, in place of from and to: a sunset from deep blue through
purple and red to orange.
directionoptional'down' | 'right''down' (default) runs from the top edge to the bottom one; 'right' from left to right.
bandsoptionalnumberHow many flat steps it is cut into, the way a sky was drawn when a game had few colours to
spare. Left out or below 2, it is smooth.
ditheroptionalbooleanBreaks the line between two steps with the ordered pattern the dither() effect uses, so the
steps blend into each other instead of meeting in a hard edge. Only with bands.
regionoptionalTPixelRegionOnly this rectangle is painted, and the gradient runs across it. Left out, the whole picture.
View source · pixels/fill.ts:24#TFillNoiseOptionstypenacatamalon/pixels
What fillNoise is asked for.
Properties
seedoptionalnumberWhich noise. The same seed always gives the same picture. Default 1.
scaleoptionalnumberHow big its blobs are, in pixels. Default 8.
octavesoptionalnumberHow many layers of finer detail are added, each half the size and half as strong. Default 1;
4 or so for clouds and rock.
fromoptionalTColorOptionThe colour of the lowest values. Default black.
tooptionalTColorOptionThe colour of the highest values. Default white.
View source · pixels/fill.ts:138#TLoadedPixelstypenacatamalon/pixels
A picture file loaded as pixels, to process in code: what useLoadPixels hands back.
pixels is null until status is 'ready'. Read it as much as you like (a collision mask, a
level drawn as an image); to change it, hand the record to createPixelTexture with a function,
which paints on a copy. The picture is shared by everyone who loaded the same file, so painting
on it directly would change it for all of them.
Properties
type'loaded-pixels'keystringWhat it is cached under in this game. The src unless a key was given.
srcstringWhere the file comes from.
pixelsTPixels | nullThe picture, once it has arrived.
View source · loaders/pixels/types/t_loaded_pixels.ts:16#TMirrorPixelsOptionstypenacatamalon/pixels
What mirrorPixels can be told.
Properties
regionoptionalTPixelRegionMirrors only this part of the picture, about its own middle: one frame of a sheet. What falls
off the picture is cut first, so the middle is that of what is left.
View source · pixels/edit.ts:175#TNormalsFromHeightOptionstypenacatamalon/pixels
What normalsFromHeight is asked for. Both optional.
Properties
depthoptionalnumberHow deep the relief is: how steeply a change of brightness leans the surface. Default 2; 6 is
deeply carved stone, 0.5 a faint grain.
wrapoptionalbooleanWhether the picture repeats, so its edges are worked out from the other side and a tiled wall has
no seams. Default true.
View source · pixels/normals_from_height.ts:12#TOutlinePixelsOptionstypenacatamalon/pixels
What outlinePixels can be told.
Properties
cornersoptionalbooleanFills the corners too, so a diagonal edge gets a thicker, closed border. Default false: only
the pixels right above, below and beside the shape, the outline pixel art usually has.
regionoptionalTPixelRegionKeeps it to this part of the picture, which is looked at as if it were the whole picture: on a
sheet, one frame's outline does not spill into the next, nor does the next one's art grow an
outline here.
View source · pixels/edit.ts:90#TPaintMatcapOptionstypenacatamalon/pixels
What paintMatcap is asked for. Everything has a default: a pale clay lit from up and to the left.
Properties
baseoptionalTColorOptionThe colour where the light falls full. Default '#d8cfc8', a pale clay.
shadeoptionalTColorOptionThe colour of the side the light does not reach. Default the base at a third of its brightness.
shineoptionalTColorOptionThe colour of the highlight. Default '#ffffff'.
glossoptionalnumberHow glossy, 0 to 1: 0 no highlight, matte as chalk; 1 a small hard one, as lacquer.
Default 0.5.
fromoptional{ x, y }Where the light comes from, as the camera sees it: x from -1 at the left to 1 at the right,
y from -1 below to 1 above. Default { x: -0.55, y: 0.6 }, up and to the left.
sizeoptionalnumberHow many pixels across, and down: the picture is square. Default 128; 64 is enough for a
game drawn small.
View source · pixels/paint_matcap.ts:13#TPixelPainttypenacatamalon/pixels
TPixelPaint: (pixels: TPixels) => TPixels | void
What createPixelTexture hands a picture to before uploading it: change it in place, or return a
picture of your own.
View source · gameobjects/pixel_texture/create_pixel_texture.ts:64#TPixelPalettetypenacatamalon/pixels
TPixelPalette: Record<string, TColorOption>
What each character of a drawing is: one character to one colour, as anything getColor reads
('#f80058', 'pink', 0xf80058, { r, g, b, a }), or 'transparent'.
'.' and ' ' are nothing unless the palette says otherwise, so a drawing does not have to name
its gaps.
View source · pixels/pixels_from_rows.ts:18#TPixelRegiontypenacatamalon/pixels
A rectangle of a picture, in its pixels, from its top-left corner.
Properties
xnumberynumberwidthnumberheightnumber
View source · pixels/types/t_pixels.ts:32#TPixelRowsOptionstypenacatamalon/pixels
How a drawing written as rows is laid into pixels, for pixelsFromRows and sheetFromRows.
Properties
scaleoptionalnumberEach character drawn as a square this many pixels across. A whole number, 1 by default.
View source · pixels/pixels_from_rows.ts:27#TPixelstypenacatamalon/pixels
A picture held in memory, to paint on in code and then hand to createPixelTexture: the engine's
replacement for drawing on a <canvas>, which only exists in a browser. Drawn art is loaded with
useLoadTexture instead; this is for pictures that are generated.
Four bytes a pixel (red, green, blue, alpha, each 0 to 255), row after row, with row 0 at the
top: the same way round as an image file and as the game's own y. Nothing in it touches the
graphics card, the page or a game, so the same picture can be painted in a scene, in a test, in a
build script or on the native runtime and comes out byte for byte the same.
data is yours to read and write directly when the painting functions are not enough; it is
width × height × 4 long and never replaced.
Properties
type'pixels'widthnumberheightnumberdataUint8Array
View source · pixels/types/t_pixels.ts:18#TPixelSheettypenacatamalon/pixels
A sheet of frames drawn from rows, side by side in one picture.
Properties
pixelsTPixelsEvery frame, left to right, in the order they were given.
columnsnumberHow many frames there are, which is what createSpriteAtlas asks for as columns.
framesRecord<string, number>Which frame each name is, counted from 0, for a sprite's animations: [hero.frames.walkA, hero.frames.walkB]. Frames given as a list have no names, and this is empty.
frameWidthnumberThe size of one frame, in pixels, after scale.
frameHeightnumber
View source · pixels/pixels_from_rows.ts:41#TPixelTextureOptionstypenacatamalon/pixels
What createPixelTexture is asked for.
Properties
keyoptionalstringA name, so the texture outlives the scene that made it and another scene can find it with
createSprite({ key }). Left out, it belongs to the part of the scene that made it and goes
when that goes.
mipmapsoptionalbooleanAlso make its mipmaps, smaller and smaller copies of it, for a model's surface that runs into
the distance (see useLoadTexture). updatePixelTexture works them out again every time,
all of them however little changed, so a picture painted every frame is cheaper without.
Default false.
View source · gameobjects/pixel_texture/create_pixel_texture.ts:73#TShadeWithRampOptionstypenacatamalon/pixels
What shadeWithRamp can be told.
Properties
lightoptional{ x, y }Where the light comes from, as a direction on the picture: { x: -1, y: -1 } is the top left,
the default and the usual one for pixel art, and { x: 1, y: 0 } the right.
regionoptionalTPixelRegionOnly this part of the picture, which is looked at as if it were the whole picture: one frame of
a sheet.
View source · pixels/palette_colors.ts:236#TSnapToPaletteOptionstypenacatamalon/pixels
What snapToPalette can be told.
Properties
regionoptionalTPixelRegionOnly this part of the picture.
View source · pixels/palette_colors.ts:50#TSphereFromPanoramaOptionstypenacatamalon/pixels
What sphereFromPanorama is asked for. Both optional.
Properties
acrossoptionalnumberHow many degrees round the picture covers, side to side. Default 360, a whole panorama (the
long 2:1 picture that goes all the way round). A photo covers less, 90 to 120; what it does
not cover is filled with it mirrored, so the reflection has no seam.
sizeoptionalnumberHow many pixels across the round picture is: it is square. Default 256.
View source · pixels/sphere_from_panorama.ts:12#TStudioEnvironmentOptionstypenacatamalon/pixels
What studioEnvironment is asked for. Everything has a default: a bright sky over a dark floor
and two white softboxes.
Properties
sizeoptionalnumberHow many pixels across, and down: the picture is square. Default 64, which reads as the era's
chrome; more is smoother.
topoptionalTColorOptionThe colour straight above. Default '#f4f7ff'.
horizonoptionalTColorOptionThe colour at the horizon, where the sky meets the floor. Default '#a9b4c8'.
groundoptionalTColorOptionThe colour of the floor below. Default '#2a201a'.
panelsoptionalnumberHow many softboxes, the bright panels a photographer lights a product with, from 0 to 4:
one up to the right, a second up to the left, a third straight above and a fourth behind the
model, which lights its rim. Default 2.
panelColoroptionalTColorOptionThe colour of the softboxes. Default '#ffffff'.
View source · pixels/studio_environment.ts:14#TUseLoadPixelsOptionstypenacatamalon/pixels
What useLoadPixels is asked for.
Properties
srcstringWhere the PNG is.
keyoptionalstringWhat to keep it under, so another scene can reach the same one. Defaults to src as the page
resolves it, so one file written two ways (on a page at the root, ./assets/a.png and
/assets/a.png) is kept once and found by either.
View source · hooks/loaders/use_load_pixels.ts:17