zoom.ts 654 B

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