Triangle Area

Calculate and display the area of a Triangle geometry object as its vertices are modified by the mouse pointer.

Triangle Area, running
800 × 600
GitHub
class Example extends Phaser.Scene
{
    create ()
    {
        const graphics = this.add.graphics({ fillStyle: { color: 0xaaaa00 } });

        const triangle = new Phaser.Geom.Triangle(0, 0, 0, 600, 400, 300);

        const text = this.add.text(400, 50, '');

        this.input.on('pointermove', pointer =>
        {

            triangle.x3 = pointer.x;
            triangle.y3 = pointer.y;

            redraw();

        });

        redraw();

        function redraw ()
        {
            graphics.clear();

            graphics.fillTriangleShape(triangle);

            const area = Phaser.Geom.Triangle.Area(triangle);

            text.setText(`Triangle Area: ${area}`);
        }
    }
}

const config = {
    scale: { mode: Phaser.Scale.FIT },
    width: 800,
    height: 600,
    type: Phaser.AUTO,
    parent: 'game-container',
    scene: Example
};

const game = new Phaser.Game(config);

More in Triangle32 examples