SPB Git

Repository Public

zoom.ts 5.6 KB 128 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : MacLustr Vision8 * File             : web/src/zoom.ts9 * Module           : web10 * Repository       : https://git.spboucher.ai/maclustr-vision11 * Project Website  : https://camera.spboucher.ai12 * Created          : 2026-11-3013 * Last Modified    : 2026-11-3014 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: admin@spboucher.ai23 * Technical Contact: dev@spboucher.ai24 * Security/Admin   : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License          : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142// Zoom numérique sur une surface vidéo : molette ou pincement pour zoomer, glisser pour déplacer, double-clic pour revenir.43import { useCallback, useEffect, useRef, useState, type CSSProperties, type PointerEvent as RPointerEvent } from 'react';4445type View = { z: number; x: number; y: number };46const MAX = 8;4748export function useZoom() {49  const [v, setV] = useState<View>({ z: 1, x: 0, y: 0 });50  const ref = useRef(v);51  ref.current = v;52  const pts = useRef(new Map<number, { x: number; y: number }>());53  const pinch = useRef<{ d: number; z: number } | null>(null);54  const moved = useRef(false);5556  const clamp = (n: View, w: number, h: number): View => {57    const z = Math.max(1, Math.min(MAX, n.z));58    const mx = ((z - 1) * w) / 2;59    const my = ((z - 1) * h) / 2;60    return { z, x: Math.max(-mx, Math.min(mx, n.x)), y: Math.max(-my, Math.min(my, n.y)) };61  };6263  const zoomAt = useCallback((el: HTMLElement, cx: number, cy: number, factor: number) => {64    const r = el.getBoundingClientRect();65    const cur = ref.current;66    const z = Math.max(1, Math.min(MAX, cur.z * factor));67    // garder sous le pointeur le même point de l'image68    const px = cx - r.left - r.width / 2;69    const py = cy - r.top - r.height / 2;70    const k = z / cur.z;71    setV(clamp({ z, x: px - (px - cur.x) * k, y: py - (py - cur.y) * k }, r.width, r.height));72  }, []);7374  // molette : écouteur natif non passif, pour zoomer sans faire défiler la page75  const stage = useRef<HTMLDivElement | null>(null);76  useEffect(() => {77    const el = stage.current;78    if (!el) return;79    const onWheel = (e: WheelEvent) => {80      if (ref.current.z === 1 && e.deltaY > 0) return; // déjà à 1× : laisser la page défiler vers le bas81      e.preventDefault();82      zoomAt(el, e.clientX, e.clientY, Math.exp(-e.deltaY * 0.0022));83    };84    el.addEventListener('wheel', onWheel, { passive: false });85    return () => el.removeEventListener('wheel', onWheel);86  }, [zoomAt]);8788  const handlers = {89    onPointerDown: (e: RPointerEvent<HTMLElement>) => {90      pts.current.set(e.pointerId, { x: e.clientX, y: e.clientY });91      moved.current = false;92      if (pts.current.size === 2) {93        const [a, b] = [...pts.current.values()];94        pinch.current = { d: Math.hypot(a.x - b.x, a.y - b.y), z: ref.current.z };95      }96      if (ref.current.z > 1 || pts.current.size === 2) e.currentTarget.setPointerCapture(e.pointerId);97    },98    onPointerMove: (e: RPointerEvent<HTMLElement>) => {99      const prev = pts.current.get(e.pointerId);100      if (!prev) return;101      pts.current.set(e.pointerId, { x: e.clientX, y: e.clientY });102      const r = e.currentTarget.getBoundingClientRect();103      if (pts.current.size === 2 && pinch.current) {104        const [a, b] = [...pts.current.values()];105        const d = Math.hypot(a.x - b.x, a.y - b.y);106        const target = pinch.current.z * (d / Math.max(1, pinch.current.d));107        zoomAt(e.currentTarget, (a.x + b.x) / 2, (a.y + b.y) / 2, target / ref.current.z);108        moved.current = true;109      } else if (ref.current.z > 1) {110        const cur = ref.current;111        if (Math.abs(e.clientX - prev.x) + Math.abs(e.clientY - prev.y) > 1) moved.current = true;112        setV(clamp({ z: cur.z, x: cur.x + e.clientX - prev.x, y: cur.y + e.clientY - prev.y }, r.width, r.height));113      }114    },115    onPointerUp: (e: RPointerEvent<HTMLElement>) => {116      pts.current.delete(e.pointerId);117      if (pts.current.size < 2) pinch.current = null;118    },119    onPointerCancel: (e: RPointerEvent<HTMLElement>) => {120      pts.current.delete(e.pointerId);121      pinch.current = null;122    },123    onDoubleClick: () => setV({ z: 1, x: 0, y: 0 }),124  };125  const style: CSSProperties = { transform: `translate3d(${v.x}px, ${v.y}px, 0) scale(${v.z})`, cursor: v.z > 1 ? 'grab' : undefined, touchAction: v.z > 1 ? 'none' : 'pan-y' };126  return { zoom: v.z, style, handlers, stage, reset: () => setV({ z: 1, x: 0, y: 0 }), step: (el: HTMLElement | null, f: number) => el && zoomAt(el, el.getBoundingClientRect().left + el.clientWidth / 2, el.getBoundingClientRect().top + el.clientHeight / 2, f), wasDrag: () => moved.current };127}128
Files main
Menu

Theme