speice.io/blog/2024-11-15-playing-with-fire/4-camera/camera.ts

41 lines
897 B
TypeScript
Raw Normal View History

2025-03-08 12:26:20 -05:00
export function camera(
x: number,
y: number,
width: number,
height: number,
2025-03-09 16:56:49 -04:00
positionX: number,
positionY: number,
2025-03-08 12:26:20 -05:00
rotate: number,
2025-03-09 16:56:49 -04:00
zoom: number,
scale: number,
2025-03-08 12:26:20 -05:00
): [number, number] {
2025-03-09 16:56:49 -04:00
// Position, rotation, and zoom are
2025-03-08 12:26:20 -05:00
// applied in IFS coordinates
[x, y] = [
2025-03-09 16:56:49 -04:00
(x - positionX),
(y - positionY),
2025-03-08 12:26:20 -05:00
];
[x, y] = [
x * Math.cos(rotate) -
y * Math.sin(rotate),
x * Math.sin(rotate) +
y * Math.cos(rotate),
2025-03-09 16:56:49 -04:00
];
[x, y] = [
x * Math.pow(2, zoom),
y * Math.pow(2, zoom)
];
2025-03-08 12:26:20 -05:00
2025-03-08 18:14:00 -05:00
// Scale transforms IFS coordinates
// to pixel coordinates. Shift by half
2025-03-08 12:26:20 -05:00
// the image width and height
2025-03-08 18:14:00 -05:00
// to compensate for IFS coordinates
// being symmetric around the origin
2025-03-08 12:26:20 -05:00
return [
Math.floor(x * scale + width / 2),
Math.floor(y * scale + height / 2)
];
}