zoom.ts 639 B

1234567891011121314151617181920212223
  1. export function getZoomOrigin(canvas: HTMLCanvasElement | null, scale: number) {
  2. if (canvas === null) {
  3. return { x: 0, y: 0 };
  4. }
  5. const context = canvas.getContext("2d");
  6. if (context === null) {
  7. return { x: 0, y: 0 };
  8. }
  9. const normalizedCanvasWidth = canvas.width / scale;
  10. const normalizedCanvasHeight = canvas.height / scale;
  11. return {
  12. x: normalizedCanvasWidth / 2,
  13. y: normalizedCanvasHeight / 2,
  14. };
  15. }
  16. export function getNormalizedZoom(zoom: number): number {
  17. const normalizedZoom = parseFloat(zoom.toFixed(2));
  18. const clampedZoom = Math.max(0.1, Math.min(normalizedZoom, 2));
  19. return clampedZoom;
  20. }