MCPcopy Create free account
hub / github.com/Useful-Scripts-Extension/useful-script / enableDragAndZoom

Function enableDragAndZoom

scripts/magnify_image.js:1258–1399  ·  view source on GitHub ↗
(element, container, onUpdateCallback)

Source from the content-addressed store, hash-verified

1256 return () => target.removeEventListener(event, callback, options);
1257}
1258function enableDragAndZoom(element, container, onUpdateCallback) {
1259 // set style
1260 const className = "ufs-drag-and-zoom";
1261 element.classList.add(className);
1262
1263 let style = document.createElement("style");
1264 style.textContent = `
1265 .${className} {
1266 cursor: grab;
1267 position: relative !important;
1268 -webkit-user-select: none !important;
1269 -moz-user-select: none !important;
1270 -ms-user-select: none !important;
1271 user-select: none !important;
1272 -khtml-user-select: none;
1273 max-width: unset !important;
1274 max-height: unset !important;
1275 min-width: unset !important;
1276 min-height: unset !important;
1277 -webkit-user-drag: none !important;
1278 }`;
1279 (container || element).appendChild(style);
1280
1281 // config
1282 const lerpSpeed = 0.3;
1283 const last = { x: 0, y: 0 };
1284 const mouse = { x: 0, y: 0 };
1285 const animTarget = {
1286 left: parseFloat(element.style.left),
1287 top: parseFloat(element.style.top),
1288 width: parseFloat(element.style.width),
1289 height: parseFloat(element.style.height),
1290 };
1291
1292 let run = true;
1293 function animate() {
1294 let updated = false;
1295 let updatedValue = {};
1296 for (let prop in animTarget) {
1297 const currentValue = parseFloat(element.style[prop]);
1298 const targetValue = animTarget[prop];
1299 let del = Math.abs(targetValue - currentValue);
1300
1301 if (del > 0.1) {
1302 const newValue =
1303 del < 1 ? targetValue : lerp(currentValue, targetValue, lerpSpeed);
1304 element.style[prop] = newValue + "px";
1305 updatedValue[prop] = newValue;
1306 updated = true;
1307 }
1308 }
1309 if (updated) onUpdateCallback?.(updatedValue);
1310 if (run) requestAnimationFrame(animate);
1311 }
1312
1313 animate();
1314
1315 // Mouse down event listener

Callers 1

createPreviewFunction · 0.85

Calls 5

animateFunction · 0.70
addEventListenerFunction · 0.70
lFunction · 0.50
addMethod · 0.45
removeMethod · 0.45

Tested by

no test coverage detected