One of 3 main API entry points. This one provides you with the drawing capabilities. Its methods are supposed to be called from either inside BeetPx.setOnStarted or BeetPx.setOnDraw callback.

BeetPx.setOnDraw(() => {
BeetPxDraw.rectFilled($v(10), $v(20), $rgb_red);
});

General

  • Fill the entire canvas with a given color. It's a method which you would typically call as the very first inside BeetPx.setOnDraw in order to not clear the canvas from what was drawn in the previous game loop iteration.

    Parameters

    Returns void

  • Sets a new XY (left-top corner) of a camera's viewport

    Parameters

    Returns BpxVector2d

    Previous camera's XY.

    const prevCameraXy = $d.setCameraXy($v(50,50));
    // draw something that requires the camera to be moved to (50,50)
    $d.setCameraXy(prevCameraXy);
  • Sets a drawing pattern to use. A drawing pattern is a 4x4 definition of which pixels should be drawn with the primary and which with the secondary of BpxPatternColors.

    Parameters

    Returns BpxDrawingPattern

    Previously used pattern.

    const prevPattern = $d.setDrawingPattern(BpxDrawingPattern.from(`
    ##--
    ##--
    --##
    --##
    `));
    $d.rectFilled($v(10), $v(20), BpxPatternColors.of($rgb_red, $rgb_blue));
    $d.setDrawingPattern(prevPattern);

Shapes

  • Draws pixels based on a visual 2d representation, defined by BpxPixels. Helpful for quickly drawing a shape that you don't have a sprite for in your spritesheet.

    Parameters

    • pixels: BpxPixels
    • xy: BpxVector2d
    • color: BpxRgbColor
    • Optionalopts: {
          centerXy?: [boolean, boolean];
          flipXy?: [boolean, boolean];
          scaleXy?: BpxVector2d;
      }
      • OptionalcenterXy?: [boolean, boolean]
      • OptionalflipXy?: [boolean, boolean]
      • OptionalscaleXy?: BpxVector2d

    Returns void

    $d.pixels(BpxPixels.from(`
    #####
    #-#-#
    #-#-#
    #####
    `, $v(10,20), $rgb_red);

Sprites

  • Allows to define a color mapping from the sprite colors to the desired ones.

    Parameters

    Returns BpxSpriteColorMapping

    Previously used color mapping

    const prevMapping = $d.setSpriteColorMapping(BpxSpriteColorMapping.from([
    [$rgb_red, $rgb_blue],
    ]));
    $d.sprite(mySprite, $v(10));
    $d.setSpriteColorMapping(prevMapping);
  • Draws a given sprite.

    Parameters

    Returns void

    const mySprite = $spr("spritesheet.png")(8,8,0,0);
    $d.sprite(mySprite, $v(10));

Text

  • Measures the space that would be occupied by the text if it was drawn on the canvas.

    Parameters

    • text: string
    • Optionalopts: {
          centerXy?: [boolean, boolean];
          scaleXy?: BpxVector2d;
      }
      • OptionalcenterXy?: [boolean, boolean]
      • OptionalscaleXy?: BpxVector2d

    Returns BpxTextMeasurement

    const line1Wh = $d.measureText(textLine1).wh;
    const line2Wh = $d.measureText(textLine2).wh;
    const line3Wh = $d.measureText(textLine3).wh;
    const totalW = Math.max(line1Wh.x, line2Wh.x, line3Wh.x);
    const totalH = line1Wh.y + line2Wh.y + line3Wh.y;
    const leftTop = $x.canvasSize.div(2).sub(totalW / 2, totalH / 2)
  • Sets color markers to be used for subsequent text drawing. Color markers are used inside text to indicate places where a color should change to another one.

    Parameters

    Returns BpxTextColorMarkers

    Previously used color markers

    const prevMarkers = $d.setTextColorMarkers({
    red_theBest: $rgb_red,
    b: $rgb_blue,
    });
    $d.text("colors are: green, [b]blue, [red_theBest]red", $v(10), $rgb_green);
    $d.setTextColorMarkers(prevMarkers);
  • Draws a text.

    Parameters

    Returns void

    $d.text("hello!\nThe 2nd line", $v(10), $rgb_red);
    

    Use \n to split the text into multiple lines (aligned to the left).