Files
nage/engine/Rect.ts
T
2026-03-22 17:49:12 +01:00

61 lines
1.1 KiB
TypeScript

import { copyVector, type Vector } from "#/engine/Vector.ts";
export type Rect = {
position: Vector;
width: number;
height: number;
};
export function createRect(
position: Vector,
width: number,
height: number,
): Rect {
return {
position,
width,
height,
};
}
export function fromMinMax(min: Vector, max: Vector): Rect {
return {
position: copyVector(min),
width: max.x - min.x,
height: max.y - min.y,
};
}
export function strokeRect(
ctx: CanvasRenderingContext2D,
rect: Rect,
color: string = "#fff",
alpha: number = 1.0,
lineWidth: number = 1,
): void {
ctx.save();
ctx.strokeStyle = color;
ctx.globalAlpha = alpha;
ctx.lineWidth = lineWidth;
ctx.strokeRect(
rect.position.x + 0.5,
rect.position.y + 0.5,
rect.width - 1,
rect.height - 1,
);
ctx.restore();
}
export function fillRect(
ctx: CanvasRenderingContext2D,
rect: Rect,
color: string = "#fff",
alpha: number = 1.0,
): void {
ctx.save();
ctx.fillStyle = color;
ctx.globalAlpha = alpha;
ctx.fillRect(rect.position.x, rect.position.y, rect.width, rect.height);
ctx.restore();
}