Rapid supports two complementary clipping mechanisms: stencil masking for arbitrary shapes drawn with any geometry command, and scissor testing for simple rectangular regions. Stencil masking is powered by the WebGL stencil buffer and supports both “draw inside the shape” and “draw outside the shape” modes.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/Nightre/Rapid.js/llms.txt
Use this file to discover all available pages before exploring further.
Stencil Masking
The stencil mask workflow has three phases: draw the mask geometry into the stencil buffer (invisible to the screen), render content constrained to that stencil, then reset the stencil for the next frame.withMask (Recommended)
withMask handles the entire lifecycle in a single call. The first callback draws the mask shape; the second callback draws the content that should be clipped to it.
MaskType
The optional third argument towithMask (and enterMask) controls which pixels are visible:
| Value | Behaviour |
|---|---|
MaskType.EQUAL | Render only where the stencil value equals the reference (inside the mask shape). This is the default. |
MaskType.NOT_EQUAL | Render only where the stencil value does not equal the reference (outside the mask shape). |
Masking with a Texture’s Alpha Channel
drawMaskImage draws a texture’s alpha channel into the stencil buffer, letting you use any pre-made PNG as a mask shape. Pixels where the texture is fully transparent will not write to the stencil.
Manual Mask API
For situations where you need more control — such as overlapping mask regions or conditional masking — you can drive the stencil phases directly:| Method | Description |
|---|---|
clearMask() | Clears the stencil buffer. Call this before each new mask to avoid bleed from previous frames. |
startDrawMask(ref?) | Disables color writes and configures the stencil to write ref (default 1) at every covered pixel. |
endDrawMask() | Re-enables color writes and locks the stencil buffer against further writes. |
enterMask(type, ref?) | Configures the stencil test to pass only where the buffer equals (or does not equal) ref. |
exitMask() | Resets the stencil test to always-pass, restoring unrestricted rendering. |
withMask calls clearMask automatically before drawing the mask geometry, so you do not need to clear the stencil manually when using the convenience wrapper.Scissor Clipping
Scissor clipping restricts all rendering to a given rectangle in logical pixel coordinates. It is implemented directly in WebGL hardware and carries no stencil-buffer overhead.withScissor (Recommended)
withScissor enables the scissor rectangle, runs a callback, then disables it automatically:
(x, y, width, height, callback) in logical pixel coordinates with a top-left origin.
Manual startScissor / endScissor
logicWidth/logicHeight and physicsWidth/physicsHeight. The Y axis is also flipped to match WebGL’s bottom-left origin internally, so you always supply coordinates in your game’s top-left coordinate system.
Scissor and stencil masking can be active simultaneously. Scissor takes effect before the stencil test in the WebGL pipeline, so rendering is first restricted to the scissor rectangle and then further constrained by the stencil mask.