@like2d/like
    Preparing search index...

    Interface Graphics

    LIKE's way of drawing to the screen.

    More specifically: a system for wrapping canvas draw calls conveniently.

    • Reduces state in calls -- no setColor, etc. Everything is passed in.
    • Abstracts away common drawing operations.

    Draw a spinning symbol in the center of the screen using transforms.

    function drawSpinningYinYang(like: Like) {
    const color1 = "black";
    const color2 = "white";
    const size = 50;
    const pos = Vec2.div(like.canvas.getSize(), 2); // calc center of screen
    const speed = 0.5; // rotations per second

    like.gfx.withTransform(() => {
    like.gfx.translate(pos);
    like.gfx.rotate(like.timer.getTime() * Math.PI * 2.0 * speed);
    like.gfx.scale(size);
    like.gfx.circle("fill", color1, [0, 0], 2);
    // use the arc parameter to fill in a semicircle. Note that it's clockwise from {x:1, y:0}.
    like.gfx.circle("fill", color2, [0, 0], 2, { arc: [Math.PI/2, Math.PI*3/2] });
    like.gfx.circle("fill", color2, [0, -1], 1);
    like.gfx.circle("fill", color1, [0, 1], 1);
    like.gfx.circle("fill", color2, [0, 1], 1/3);
    like.gfx.circle("fill", color1, [0, -1], 1/3);
    })
    }

    like.draw = (like: Like) => {
    drawSpinningYinYang(like);
    }
    interface Graphics {
        circle(
            mode: DrawMode,
            color: Color,
            position: Vector2,
            radius: number,
            props?: {
                lineCap?: CanvasLineCap;
                lineJoin?: CanvasLineJoin;
                lineWidth?: number;
                miterLimit?: number;
            } & TransformProps & { arc?: [number, number]; center?: boolean },
        ): void;
        clear(color?: Color): void;
        draw(handle: ImageHandle, position: Vector2, props?: DrawProps): void;
        line(color: Color, points: Vector2[], props?: ShapeProps): void;
        newImage(path: string): ImageHandle;
        points(color: Color, pts: Vector2[], props?: TransformProps): void;
        polygon(
            mode: DrawMode,
            color: Color,
            position: Vector2,
            points: Vector2[],
            props?: ShapeProps,
        ): void;
        pop(): void;
        print(
            mode: DrawMode,
            color: Color,
            text: string,
            position: Vector2,
            props?: PrintProps,
        ): void;
        push(): void;
        rectangle(
            mode: DrawMode,
            color: Color,
            rect: Rectangle,
            props?: ShapeProps,
        ): void;
        rotate(angle: number): void;
        scale(factor: number | Vector2): void;
        translate(offset: Vector2): void;
        withRenderTarget(
            context: CanvasRenderingContext2D,
            callback: () => void,
        ): void;
        withTransform(callback: () => void): void;
    }
    Index

    Methods

    • Draws a circle. For ellipse, set props.scale to a Vector2.

      Parameters

      • mode: DrawMode

        Fill or line.

      • color: Color

        Fill or stroke color.

      • position: Vector2

        Center position.

      • radius: number
      • Optionalprops: {
            lineCap?: CanvasLineCap;
            lineJoin?: CanvasLineJoin;
            lineWidth?: number;
            miterLimit?: number;
        } & TransformProps & { arc?: [number, number]; center?: boolean }

        Optional arc, center, and stroke properties. Center is true by default.

      Returns void

    • Loads an image from a path. Unlike fetch, this pretends to be synchronous.

      Parameters

      • path: string

        Image file path.

      Returns ImageHandle

      ImageHandle for use with draw.

    • Draws text at a position.

      Align works browser-style: if you align center, your text draws to the left and right of its position. If you align right, your position becomes the upper-right corner of the text.

      Parameters

      • mode: DrawMode

        Fill or line.

      • color: Color

        Fill or stroke color.

      • text: string

        Text string.

      • position: Vector2

        Top-left position.

      • Optionalprops: PrintProps

        PrintProps Optional font, text limit, or alignment.

      Returns void

    • Applies a rotation.

      Parameters

      • angle: number

        Rotation in radians.

      Returns void

    • The idiomatic way to render to an external canvas, which has to be managed manually using browser APIs.

      Within this scope, the target canvas has changed.

      Outside, it stays the same.

      Parameters

      • context: CanvasRenderingContext2D

        The context to call when drawing.

      • callback: () => void

        Functions that will be called while drawing to the target.

      Returns void

    • For Expressive Purposes

      A simple wrapper around push/pop (save/restore) that clearly allows a set of statements to have their own transform matrix.

      In other words, any scale, translate, etc. performed in this block does not affect the outside world.

      Parameters

      • callback: () => void

        the drawing logic.

      Returns void