Get Bounding Rectangle from Points
Demonstrates how to calculate a bounding box that encompasses a dynamic set of points using Phaser.Math.GetVec2Bounds.
800 × 600
class Example extends Phaser.Scene
{
create ()
{
const graphics = this.add.graphics({ lineStyle: { width: 2, color: 0x2266aa }, fillStyle: { color: 0x2266aa } });
const points = [
new Phaser.Math.Vector2(Math.random() * 400 + 200, Math.random() * 300 + 150),
new Phaser.Math.Vector2(Math.random() * 400 + 200, Math.random() * 300 + 150),
new Phaser.Math.Vector2(Math.random() * 400 + 200, Math.random() * 300 + 150)
];
this.input.on('pointermove', pointer =>
{
points[0].copy(pointer);
redraw();
});
this.input.on('pointerdown', pointer =>
{
points.push(new Phaser.Math.Vector2(pointer.x, pointer.y));
redraw();
});
redraw();
function redraw ()
{
graphics.clear();
const rect = Phaser.Math.GetVec2Bounds(points);
graphics.strokeRectShape(rect);
for (let i = 0; i < points.length; i++)
{
graphics.fillPointShape(points[i], 5);
}
}
}
}
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);