Browse Source

Set handle left/top as percentages so that they adjust to changes in
zoom level automatically.

Andrew Klopper 6 years ago
parent
commit
437270eab9
1 changed files with 6 additions and 9 deletions
  1. 6 9
      index.html

+ 6 - 9
index.html

@@ -164,18 +164,16 @@
164 164
                                 context.stroke();
165 165
                             }
166 166
 
167
-                            function repositionHandle(handle) {
168
-                                let scale = scaledWidth / imageWidth;
169
-                                handle.style.left = (parseFloat(handle.dataset.left) * scale) + 'px';
170
-                                handle.style.top = (parseFloat(handle.dataset.top) * scale) + 'px';
171
-                            }
172
-
173 167
                             function resize() {
174 168
                                 sourceImage.style.width = scaledWidth + 'px';
175
-                                handles.forEach(repositionHandle);
176 169
                                 redrawOverlay();
177 170
                             }
178 171
 
172
+                            function repositionHandle(handle) {
173
+                                handle.style.left = (parseFloat(handle.dataset.left) / imageWidth * 100) + '%';
174
+                                handle.style.top = (parseFloat(handle.dataset.top) / imageHeight * 100) + '%';
175
+                            }
176
+
179 177
                             function dragMoveListener(event) {
180 178
                                 let target = event.target;
181 179
                                 let scale = scaledWidth / imageWidth;
@@ -270,8 +268,7 @@
270 268
                                 handle.className = 'handle';
271 269
                                 handle.dataset.left = corner.x;
272 270
                                 handle.dataset.top = corner.y;
273
-                                handle.style.left = handle.dataset.left + 'px';
274
-                                handle.style.top = handle.dataset.top + 'px';
271
+                                repositionHandle(handle);
275 272
                                 sourceImage.appendChild(handle);
276 273
                                 interact(handle).draggable({
277 274
                                     onmove: dragMoveListener