File size: 7,034 Bytes
eebc40f 6bda4a6 eebc40f 6bda4a6 eebc40f 6bda4a6 eebc40f c7c94aa eebc40f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
import { useRef, useCallback } from 'react';
import * as d3 from 'd3';
/**
* Hook spécialisé pour la gestion du zoom et pan
* Centralise toute la logique de zoom/pan et les fonctions globales
*/
export const useZoom = (svgRef, darkMode, useCSSTransform = false) => {
const zoomRef = useRef();
// Fonction pour créer et configurer le zoom
const setupZoom = useCallback((svg, viewportGroup, uiGroup, width, height) => {
if (!svg || !viewportGroup) return null;
// Configuration de zoom - anti-pixellisation avec matrix
const zoom = d3.zoom()
.scaleExtent([0.6, 5.0])
.filter(function(event) {
return !event.ctrlKey || event.type === 'wheel';
})
.on('start', function() {
svg.classed('zooming', true);
})
.on('zoom', (event) => {
const { transform } = event;
if (useCSSTransform) {
// Utiliser CSS transform pour éviter la pixellisation
viewportGroup.style('transform', `matrix(${transform.k}, 0, 0, ${transform.k}, ${transform.x}, ${transform.y})`);
viewportGroup.attr('transform', null);
} else {
// Utiliser SVG transform (approche par défaut)
const matrix = transform.toString();
viewportGroup.attr('transform', matrix);
viewportGroup.style('transform', null);
}
// Mise à jour de l'indicateur de zoom
const scaleValue = transform.k;
uiGroup.select('.zoom-indicator')
.text(`Zoom: ${Math.round(scaleValue * 100)}%`);
// Mise à jour des tooltips
if (window.updateTooltipTransform) {
window.updateTooltipTransform(transform);
}
if (window.updateTooltipPositions) {
window.updateTooltipPositions();
}
})
.on('end', function() {
svg.classed('zooming', false);
});
// Appliquer le zoom au SVG
svg.call(zoom);
// Initialisation du zoom par défaut à 80% avec centrage
const centerX = width / 2;
const centerY = height / 2;
const scale = 0.8;
// Calculer la translation pour centrer la vue
const translateX = centerX * (1 - scale);
const translateY = centerY * (1 - scale);
const initialTransform = d3.zoomIdentity
.translate(translateX, translateY)
.scale(scale);
svg.call(zoom.transform, initialTransform);
// Initialisation de l'indicateur de zoom
uiGroup.select('.zoom-indicator')
.text(`Zoom: 80%`);
// Stocker la référence du zoom
zoomRef.current = zoom;
return zoom;
}, [useCSSTransform]);
// Fonctions de zoom globales
const setupGlobalZoomFunctions = useCallback((svg) => {
window.zoomIn = () => {
if (zoomRef.current) {
svg.transition().duration(200).call(zoomRef.current.scaleBy, 1.5);
}
};
window.zoomOut = () => {
if (zoomRef.current) {
svg.transition().duration(200).call(zoomRef.current.scaleBy, 1 / 1.5);
}
};
window.resetZoom = () => {
if (zoomRef.current) {
// Obtenir les dimensions actuelles du SVG
const currentWidth = svg.attr('width') || svg.node().clientWidth;
const currentHeight = svg.attr('height') || svg.node().clientHeight;
// Centrer la vue à 80%
const centerX = currentWidth / 2;
const centerY = currentHeight / 2;
const scale = 0.8;
const translateX = centerX * (1 - scale);
const translateY = centerY * (1 - scale);
const resetTransform = d3.zoomIdentity
.translate(translateX, translateY)
.scale(scale);
svg.transition().duration(300).call(zoomRef.current.transform, resetTransform);
}
};
}, []);
// Fonction pour centrer sur une police sélectionnée
const centerOnFont = useCallback((selectedFont, positions, visualStateRef, setIsTransitioning) => {
if (!selectedFont || !positions?.length || !zoomRef.current) return;
// Utiliser les positions déjà calculées (pas de recalcul !)
const selectedPosition = positions.find(p => p.name === selectedFont.name);
if (!selectedPosition) return;
const svg = d3.select(svgRef.current);
// Récupérer les dimensions du SVG
const svgElement = svgRef.current;
const width = svgElement.clientWidth;
const height = svgElement.clientHeight;
// Démarrer la transition
setIsTransitioning(true);
visualStateRef.current.isTransitioning = true;
const scale = 2.5;
const centerX = width / 2;
const centerY = height / 2;
const translateX = centerX - (selectedPosition.x * scale);
const translateY = centerY - (selectedPosition.y * scale);
const transform = d3.zoomIdentity
.translate(translateX, translateY)
.scale(scale);
// Animation avec gestion de la fin de transition
svg.transition()
.duration(800)
.ease(d3.easeCubicInOut)
.call(zoomRef.current.transform, transform)
.on('end', () => {
// Finir la transition
setIsTransitioning(false);
visualStateRef.current.isTransitioning = false;
});
}, [svgRef]);
// Fonction pour créer l'indicateur de zoom et de navigation
const createZoomIndicator = useCallback((uiGroup, width, height, canNavigate = false) => {
if (!uiGroup) return;
// Créer ou mettre à jour l'indicateur de zoom
const existingZoomIndicator = uiGroup.select('.zoom-indicator');
if (!existingZoomIndicator.empty()) {
existingZoomIndicator
.attr('y', Math.max(height - 20, 20))
.style('fill', darkMode ? '#ffffff' : '#000000');
} else {
uiGroup.append('text')
.attr('class', 'zoom-indicator')
.attr('x', 20)
.attr('y', Math.max(height - 20, 20))
.style('font-size', '12px')
.style('fill', darkMode ? '#ffffff' : '#000000')
.style('opacity', 0.7)
.text('Zoom: 100%');
}
// Créer ou mettre à jour l'indicateur de navigation
const existingNavIndicator = uiGroup.select('.navigation-indicator');
if (canNavigate) {
if (!existingNavIndicator.empty()) {
existingNavIndicator
.attr('y', Math.max(height - 40, 40))
.style('fill', darkMode ? '#ffffff' : '#000000')
.style('opacity', 0.7);
} else {
uiGroup.append('text')
.attr('class', 'navigation-indicator')
.attr('x', 20)
.attr('y', Math.max(height - 40, 40))
.style('font-size', '12px')
.style('fill', darkMode ? '#ffffff' : '#000000')
.style('opacity', 0.7)
.text('↑↓←→ to navigate');
}
} else {
// Supprimer l'indicateur de navigation s'il existe
existingNavIndicator.remove();
}
return existingZoomIndicator;
}, [darkMode]);
return {
zoomRef,
setupZoom,
setupGlobalZoomFunctions,
centerOnFont,
createZoomIndicator
};
};
|