(svg, clickHandler)
| 1 | // Make svg pannable and zoomable. |
| 2 | // Call clickHandler(t) if a click event is caught by the pan event handlers. |
| 3 | function initPanAndZoom(svg, clickHandler) { |
| 4 | 'use strict'; |
| 5 | |
| 6 | // Current mouse/touch handling mode |
| 7 | const IDLE = 0; |
| 8 | const MOUSEPAN = 1; |
| 9 | const TOUCHPAN = 2; |
| 10 | const TOUCHZOOM = 3; |
| 11 | let mode = IDLE; |
| 12 | |
| 13 | // State needed to implement zooming. |
| 14 | let currentScale = 1.0; |
| 15 | const initWidth = svg.viewBox.baseVal.width; |
| 16 | const initHeight = svg.viewBox.baseVal.height; |
| 17 | |
| 18 | // State needed to implement panning. |
| 19 | let panLastX = 0; // Last event X coordinate |
| 20 | let panLastY = 0; // Last event Y coordinate |
| 21 | let moved = false; // Have we seen significant movement |
| 22 | let touchid = null; // Current touch identifier |
| 23 | |
| 24 | // State needed for pinch zooming |
| 25 | let touchid2 = null; // Second id for pinch zooming |
| 26 | let initGap = 1.0; // Starting gap between two touches |
| 27 | let initScale = 1.0; // currentScale when pinch zoom started |
| 28 | let centerPoint = null; // Center point for scaling |
| 29 | |
| 30 | // Convert event coordinates to svg coordinates. |
| 31 | function toSvg(x, y) { |
| 32 | const p = svg.createSVGPoint(); |
| 33 | p.x = x; |
| 34 | p.y = y; |
| 35 | let m = svg.getCTM(); |
| 36 | if (m == null) m = svg.getScreenCTM(); // Firefox workaround. |
| 37 | return p.matrixTransform(m.inverse()); |
| 38 | } |
| 39 | |
| 40 | // Change the scaling for the svg to s, keeping the point denoted |
| 41 | // by u (in svg coordinates]) fixed at the same screen location. |
| 42 | function rescale(s, u) { |
| 43 | // Limit to a good range. |
| 44 | if (s < 0.2) s = 0.2; |
| 45 | if (s > 10.0) s = 10.0; |
| 46 | |
| 47 | currentScale = s; |
| 48 | |
| 49 | // svg.viewBox defines the visible portion of the user coordinate |
| 50 | // system. So to magnify by s, divide the visible portion by s, |
| 51 | // which will then be stretched to fit the viewport. |
| 52 | const vb = svg.viewBox; |
| 53 | const w1 = vb.baseVal.width; |
| 54 | const w2 = initWidth / s; |
| 55 | const h1 = vb.baseVal.height; |
| 56 | const h2 = initHeight / s; |
| 57 | vb.baseVal.width = w2; |
| 58 | vb.baseVal.height = h2; |
| 59 | |
| 60 | // We also want to adjust vb.baseVal.x so that u.x remains at same |
no outgoing calls
no test coverage detected
searching dependent graphs…