zoom.js 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  1. onUiLoaded(async() => {
  2. const elementIDs = {
  3. img2imgTabs: "#mode_img2img .tab-nav",
  4. inpaint: "#img2maskimg",
  5. inpaintSketch: "#inpaint_sketch",
  6. rangeGroup: "#img2img_column_size",
  7. sketch: "#img2img_sketch"
  8. };
  9. const tabNameToElementId = {
  10. "Inpaint sketch": elementIDs.inpaintSketch,
  11. "Inpaint": elementIDs.inpaint,
  12. "Sketch": elementIDs.sketch
  13. };
  14. // Helper functions
  15. // Get active tab
  16. /**
  17. * Waits for an element to be present in the DOM.
  18. */
  19. const waitForElement = (id) => new Promise(resolve => {
  20. const checkForElement = () => {
  21. const element = document.querySelector(id);
  22. if (element) return resolve(element);
  23. setTimeout(checkForElement, 100);
  24. };
  25. checkForElement();
  26. });
  27. function getActiveTab(elements, all = false) {
  28. const tabs = elements.img2imgTabs.querySelectorAll("button");
  29. if (all) return tabs;
  30. for (let tab of tabs) {
  31. if (tab.classList.contains("selected")) {
  32. return tab;
  33. }
  34. }
  35. }
  36. // Get tab ID
  37. function getTabId(elements) {
  38. const activeTab = getActiveTab(elements);
  39. return tabNameToElementId[activeTab.innerText];
  40. }
  41. // Wait until opts loaded
  42. async function waitForOpts() {
  43. for (; ;) {
  44. if (window.opts && Object.keys(window.opts).length) {
  45. return window.opts;
  46. }
  47. await new Promise(resolve => setTimeout(resolve, 100));
  48. }
  49. }
  50. // Detect whether the element has a horizontal scroll bar
  51. function hasHorizontalScrollbar(element) {
  52. return element.scrollWidth > element.clientWidth;
  53. }
  54. // Function for defining the "Ctrl", "Shift" and "Alt" keys
  55. function isModifierKey(event, key) {
  56. switch (key) {
  57. case "Ctrl":
  58. return event.ctrlKey;
  59. case "Shift":
  60. return event.shiftKey;
  61. case "Alt":
  62. return event.altKey;
  63. default:
  64. return false;
  65. }
  66. }
  67. // Check if hotkey is valid
  68. function isValidHotkey(value) {
  69. const specialKeys = ["Ctrl", "Alt", "Shift", "Disable"];
  70. return (
  71. (typeof value === "string" &&
  72. value.length === 1 &&
  73. /[a-z]/i.test(value)) ||
  74. specialKeys.includes(value)
  75. );
  76. }
  77. // Normalize hotkey
  78. function normalizeHotkey(hotkey) {
  79. return hotkey.length === 1 ? "Key" + hotkey.toUpperCase() : hotkey;
  80. }
  81. // Format hotkey for display
  82. function formatHotkeyForDisplay(hotkey) {
  83. return hotkey.startsWith("Key") ? hotkey.slice(3) : hotkey;
  84. }
  85. // Create hotkey configuration with the provided options
  86. function createHotkeyConfig(defaultHotkeysConfig, hotkeysConfigOpts) {
  87. const result = {}; // Resulting hotkey configuration
  88. const usedKeys = new Set(); // Set of used hotkeys
  89. // Iterate through defaultHotkeysConfig keys
  90. for (const key in defaultHotkeysConfig) {
  91. const userValue = hotkeysConfigOpts[key]; // User-provided hotkey value
  92. const defaultValue = defaultHotkeysConfig[key]; // Default hotkey value
  93. // Apply appropriate value for undefined, boolean, or object userValue
  94. if (
  95. userValue === undefined ||
  96. typeof userValue === "boolean" ||
  97. typeof userValue === "object" ||
  98. userValue === "disable"
  99. ) {
  100. result[key] =
  101. userValue === undefined ? defaultValue : userValue;
  102. } else if (isValidHotkey(userValue)) {
  103. const normalizedUserValue = normalizeHotkey(userValue);
  104. // Check for conflicting hotkeys
  105. if (!usedKeys.has(normalizedUserValue)) {
  106. usedKeys.add(normalizedUserValue);
  107. result[key] = normalizedUserValue;
  108. } else {
  109. console.error(
  110. `Hotkey: ${formatHotkeyForDisplay(
  111. userValue
  112. )} for ${key} is repeated and conflicts with another hotkey. The default hotkey is used: ${formatHotkeyForDisplay(
  113. defaultValue
  114. )}`
  115. );
  116. result[key] = defaultValue;
  117. }
  118. } else {
  119. console.error(
  120. `Hotkey: ${formatHotkeyForDisplay(
  121. userValue
  122. )} for ${key} is not valid. The default hotkey is used: ${formatHotkeyForDisplay(
  123. defaultValue
  124. )}`
  125. );
  126. result[key] = defaultValue;
  127. }
  128. }
  129. return result;
  130. }
  131. // Disables functions in the config object based on the provided list of function names
  132. function disableFunctions(config, disabledFunctions) {
  133. // Bind the hasOwnProperty method to the functionMap object to avoid errors
  134. const hasOwnProperty =
  135. Object.prototype.hasOwnProperty.bind(functionMap);
  136. // Loop through the disabledFunctions array and disable the corresponding functions in the config object
  137. disabledFunctions.forEach(funcName => {
  138. if (hasOwnProperty(funcName)) {
  139. const key = functionMap[funcName];
  140. config[key] = "disable";
  141. }
  142. });
  143. // Return the updated config object
  144. return config;
  145. }
  146. /**
  147. * The restoreImgRedMask function displays a red mask around an image to indicate the aspect ratio.
  148. * If the image display property is set to 'none', the mask breaks. To fix this, the function
  149. * temporarily sets the display property to 'block' and then hides the mask again after 300 milliseconds
  150. * to avoid breaking the canvas. Additionally, the function adjusts the mask to work correctly on
  151. * very long images.
  152. */
  153. function restoreImgRedMask(elements) {
  154. const mainTabId = getTabId(elements);
  155. if (!mainTabId) return;
  156. const mainTab = gradioApp().querySelector(mainTabId);
  157. const img = mainTab.querySelector("img");
  158. const imageARPreview = gradioApp().querySelector("#imageARPreview");
  159. if (!img || !imageARPreview) return;
  160. imageARPreview.style.transform = "";
  161. if (parseFloat(mainTab.style.width) > 865) {
  162. const transformString = mainTab.style.transform;
  163. const scaleMatch = transformString.match(
  164. /scale\(([-+]?[0-9]*\.?[0-9]+)\)/
  165. );
  166. let zoom = 1; // default zoom
  167. if (scaleMatch && scaleMatch[1]) {
  168. zoom = Number(scaleMatch[1]);
  169. }
  170. imageARPreview.style.transformOrigin = "0 0";
  171. imageARPreview.style.transform = `scale(${zoom})`;
  172. }
  173. if (img.style.display !== "none") return;
  174. img.style.display = "block";
  175. setTimeout(() => {
  176. img.style.display = "none";
  177. }, 400);
  178. }
  179. const hotkeysConfigOpts = await waitForOpts();
  180. // Default config
  181. const defaultHotkeysConfig = {
  182. canvas_hotkey_zoom: "Alt",
  183. canvas_hotkey_adjust: "Ctrl",
  184. canvas_hotkey_reset: "KeyR",
  185. canvas_hotkey_fullscreen: "KeyS",
  186. canvas_hotkey_move: "KeyF",
  187. canvas_hotkey_overlap: "KeyO",
  188. canvas_hotkey_shrink_brush: "KeyQ",
  189. canvas_hotkey_grow_brush: "KeyW",
  190. canvas_disabled_functions: [],
  191. canvas_show_tooltip: true,
  192. canvas_auto_expand: true,
  193. canvas_blur_prompt: false,
  194. };
  195. const functionMap = {
  196. "Zoom": "canvas_hotkey_zoom",
  197. "Adjust brush size": "canvas_hotkey_adjust",
  198. "Hotkey shrink brush": "canvas_hotkey_shrink_brush",
  199. "Hotkey enlarge brush": "canvas_hotkey_grow_brush",
  200. "Moving canvas": "canvas_hotkey_move",
  201. "Fullscreen": "canvas_hotkey_fullscreen",
  202. "Reset Zoom": "canvas_hotkey_reset",
  203. "Overlap": "canvas_hotkey_overlap"
  204. };
  205. // Loading the configuration from opts
  206. const preHotkeysConfig = createHotkeyConfig(
  207. defaultHotkeysConfig,
  208. hotkeysConfigOpts
  209. );
  210. // Disable functions that are not needed by the user
  211. const hotkeysConfig = disableFunctions(
  212. preHotkeysConfig,
  213. preHotkeysConfig.canvas_disabled_functions
  214. );
  215. let isMoving = false;
  216. let mouseX, mouseY;
  217. let activeElement;
  218. const elements = Object.fromEntries(
  219. Object.keys(elementIDs).map(id => [
  220. id,
  221. gradioApp().querySelector(elementIDs[id])
  222. ])
  223. );
  224. const elemData = {};
  225. // Apply functionality to the range inputs. Restore redmask and correct for long images.
  226. const rangeInputs = elements.rangeGroup ?
  227. Array.from(elements.rangeGroup.querySelectorAll("input")) :
  228. [
  229. gradioApp().querySelector("#img2img_width input[type='range']"),
  230. gradioApp().querySelector("#img2img_height input[type='range']")
  231. ];
  232. for (const input of rangeInputs) {
  233. input?.addEventListener("input", () => restoreImgRedMask(elements));
  234. }
  235. function applyZoomAndPan(elemId, isExtension = true) {
  236. const targetElement = gradioApp().querySelector(elemId);
  237. if (!targetElement) {
  238. console.log("Element not found");
  239. return;
  240. }
  241. targetElement.style.transformOrigin = "0 0";
  242. elemData[elemId] = {
  243. zoom: 1,
  244. panX: 0,
  245. panY: 0
  246. };
  247. let fullScreenMode = false;
  248. // Create tooltip
  249. function createTooltip() {
  250. const toolTipElement =
  251. targetElement.querySelector(".image-container");
  252. const tooltip = document.createElement("div");
  253. tooltip.className = "canvas-tooltip";
  254. // Creating an item of information
  255. const info = document.createElement("i");
  256. info.className = "canvas-tooltip-info";
  257. info.textContent = "";
  258. // Create a container for the contents of the tooltip
  259. const tooltipContent = document.createElement("div");
  260. tooltipContent.className = "canvas-tooltip-content";
  261. // Define an array with hotkey information and their actions
  262. const hotkeysInfo = [
  263. {
  264. configKey: "canvas_hotkey_zoom",
  265. action: "Zoom canvas",
  266. keySuffix: " + wheel"
  267. },
  268. {
  269. configKey: "canvas_hotkey_adjust",
  270. action: "Adjust brush size",
  271. keySuffix: " + wheel"
  272. },
  273. {configKey: "canvas_hotkey_reset", action: "Reset zoom"},
  274. {
  275. configKey: "canvas_hotkey_fullscreen",
  276. action: "Fullscreen mode"
  277. },
  278. {configKey: "canvas_hotkey_move", action: "Move canvas"},
  279. {configKey: "canvas_hotkey_overlap", action: "Overlap"}
  280. ];
  281. // Create hotkeys array with disabled property based on the config values
  282. const hotkeys = hotkeysInfo.map(info => {
  283. const configValue = hotkeysConfig[info.configKey];
  284. const key = info.keySuffix ?
  285. `${configValue}${info.keySuffix}` :
  286. configValue.charAt(configValue.length - 1);
  287. return {
  288. key,
  289. action: info.action,
  290. disabled: configValue === "disable"
  291. };
  292. });
  293. for (const hotkey of hotkeys) {
  294. if (hotkey.disabled) {
  295. continue;
  296. }
  297. const p = document.createElement("p");
  298. p.innerHTML = `<b>${hotkey.key}</b> - ${hotkey.action}`;
  299. tooltipContent.appendChild(p);
  300. }
  301. // Add information and content elements to the tooltip element
  302. tooltip.appendChild(info);
  303. tooltip.appendChild(tooltipContent);
  304. // Add a hint element to the target element
  305. toolTipElement.appendChild(tooltip);
  306. }
  307. //Show tool tip if setting enable
  308. if (hotkeysConfig.canvas_show_tooltip) {
  309. createTooltip();
  310. }
  311. // In the course of research, it was found that the tag img is very harmful when zooming and creates white canvases. This hack allows you to almost never think about this problem, it has no effect on webui.
  312. function fixCanvas() {
  313. const activeTab = getActiveTab(elements).textContent.trim();
  314. if (activeTab !== "img2img") {
  315. const img = targetElement.querySelector(`${elemId} img`);
  316. if (img && img.style.display !== "none") {
  317. img.style.display = "none";
  318. img.style.visibility = "hidden";
  319. }
  320. }
  321. }
  322. // Reset the zoom level and pan position of the target element to their initial values
  323. function resetZoom() {
  324. elemData[elemId] = {
  325. zoomLevel: 1,
  326. panX: 0,
  327. panY: 0
  328. };
  329. if (isExtension) {
  330. targetElement.style.overflow = "hidden";
  331. }
  332. targetElement.isZoomed = false;
  333. fixCanvas();
  334. targetElement.style.transform = `scale(${elemData[elemId].zoomLevel}) translate(${elemData[elemId].panX}px, ${elemData[elemId].panY}px)`;
  335. const canvas = gradioApp().querySelector(
  336. `${elemId} canvas[key="interface"]`
  337. );
  338. toggleOverlap("off");
  339. fullScreenMode = false;
  340. const closeBtn = targetElement.querySelector("button[aria-label='Remove Image']");
  341. if (closeBtn) {
  342. closeBtn.addEventListener("click", resetZoom);
  343. }
  344. if (canvas && isExtension) {
  345. const parentElement = targetElement.closest('[id^="component-"]');
  346. if (
  347. canvas &&
  348. parseFloat(canvas.style.width) > parentElement.offsetWidth &&
  349. parseFloat(targetElement.style.width) > parentElement.offsetWidth
  350. ) {
  351. fitToElement();
  352. return;
  353. }
  354. }
  355. if (
  356. canvas &&
  357. !isExtension &&
  358. parseFloat(canvas.style.width) > 865 &&
  359. parseFloat(targetElement.style.width) > 865
  360. ) {
  361. fitToElement();
  362. return;
  363. }
  364. targetElement.style.width = "";
  365. }
  366. // Toggle the zIndex of the target element between two values, allowing it to overlap or be overlapped by other elements
  367. function toggleOverlap(forced = "") {
  368. const zIndex1 = "0";
  369. const zIndex2 = "998";
  370. targetElement.style.zIndex =
  371. targetElement.style.zIndex !== zIndex2 ? zIndex2 : zIndex1;
  372. if (forced === "off") {
  373. targetElement.style.zIndex = zIndex1;
  374. } else if (forced === "on") {
  375. targetElement.style.zIndex = zIndex2;
  376. }
  377. }
  378. // Adjust the brush size based on the deltaY value from a mouse wheel event
  379. function adjustBrushSize(
  380. elemId,
  381. deltaY,
  382. withoutValue = false,
  383. percentage = 5
  384. ) {
  385. const input =
  386. gradioApp().querySelector(
  387. `${elemId} input[aria-label='Brush radius']`
  388. ) ||
  389. gradioApp().querySelector(
  390. `${elemId} button[aria-label="Use brush"]`
  391. );
  392. if (input) {
  393. input.click();
  394. if (!withoutValue) {
  395. const maxValue =
  396. parseFloat(input.getAttribute("max")) || 100;
  397. const changeAmount = maxValue * (percentage / 100);
  398. const newValue =
  399. parseFloat(input.value) +
  400. (deltaY > 0 ? -changeAmount : changeAmount);
  401. input.value = Math.min(Math.max(newValue, 0), maxValue);
  402. input.dispatchEvent(new Event("change"));
  403. }
  404. }
  405. }
  406. // Reset zoom when uploading a new image
  407. const fileInput = gradioApp().querySelector(
  408. `${elemId} input[type="file"][accept="image/*"].svelte-116rqfv`
  409. );
  410. fileInput.addEventListener("click", resetZoom);
  411. // Update the zoom level and pan position of the target element based on the values of the zoomLevel, panX and panY variables
  412. function updateZoom(newZoomLevel, mouseX, mouseY) {
  413. newZoomLevel = Math.max(0.1, Math.min(newZoomLevel, 15));
  414. elemData[elemId].panX +=
  415. mouseX - (mouseX * newZoomLevel) / elemData[elemId].zoomLevel;
  416. elemData[elemId].panY +=
  417. mouseY - (mouseY * newZoomLevel) / elemData[elemId].zoomLevel;
  418. targetElement.style.transformOrigin = "0 0";
  419. targetElement.style.transform = `translate(${elemData[elemId].panX}px, ${elemData[elemId].panY}px) scale(${newZoomLevel})`;
  420. toggleOverlap("on");
  421. if (isExtension) {
  422. targetElement.style.overflow = "visible";
  423. }
  424. return newZoomLevel;
  425. }
  426. // Change the zoom level based on user interaction
  427. function changeZoomLevel(operation, e) {
  428. if (isModifierKey(e, hotkeysConfig.canvas_hotkey_zoom)) {
  429. e.preventDefault();
  430. let zoomPosX, zoomPosY;
  431. let delta = 0.2;
  432. if (elemData[elemId].zoomLevel > 7) {
  433. delta = 0.9;
  434. } else if (elemData[elemId].zoomLevel > 2) {
  435. delta = 0.6;
  436. }
  437. zoomPosX = e.clientX;
  438. zoomPosY = e.clientY;
  439. fullScreenMode = false;
  440. elemData[elemId].zoomLevel = updateZoom(
  441. elemData[elemId].zoomLevel +
  442. (operation === "+" ? delta : -delta),
  443. zoomPosX - targetElement.getBoundingClientRect().left,
  444. zoomPosY - targetElement.getBoundingClientRect().top
  445. );
  446. targetElement.isZoomed = true;
  447. }
  448. }
  449. /**
  450. * This function fits the target element to the screen by calculating
  451. * the required scale and offsets. It also updates the global variables
  452. * zoomLevel, panX, and panY to reflect the new state.
  453. */
  454. function fitToElement() {
  455. //Reset Zoom
  456. targetElement.style.transform = `translate(${0}px, ${0}px) scale(${1})`;
  457. let parentElement;
  458. if (isExtension) {
  459. parentElement = targetElement.closest('[id^="component-"]');
  460. } else {
  461. parentElement = targetElement.parentElement;
  462. }
  463. // Get element and screen dimensions
  464. const elementWidth = targetElement.offsetWidth;
  465. const elementHeight = targetElement.offsetHeight;
  466. const screenWidth = parentElement.clientWidth;
  467. const screenHeight = parentElement.clientHeight;
  468. // Get element's coordinates relative to the parent element
  469. const elementRect = targetElement.getBoundingClientRect();
  470. const parentRect = parentElement.getBoundingClientRect();
  471. const elementX = elementRect.x - parentRect.x;
  472. // Calculate scale and offsets
  473. const scaleX = screenWidth / elementWidth;
  474. const scaleY = screenHeight / elementHeight;
  475. const scale = Math.min(scaleX, scaleY);
  476. const transformOrigin =
  477. window.getComputedStyle(targetElement).transformOrigin;
  478. const [originX, originY] = transformOrigin.split(" ");
  479. const originXValue = parseFloat(originX);
  480. const originYValue = parseFloat(originY);
  481. const offsetX =
  482. (screenWidth - elementWidth * scale) / 2 -
  483. originXValue * (1 - scale);
  484. const offsetY =
  485. (screenHeight - elementHeight * scale) / 2.5 -
  486. originYValue * (1 - scale);
  487. // Apply scale and offsets to the element
  488. targetElement.style.transform = `translate(${offsetX}px, ${offsetY}px) scale(${scale})`;
  489. // Update global variables
  490. elemData[elemId].zoomLevel = scale;
  491. elemData[elemId].panX = offsetX;
  492. elemData[elemId].panY = offsetY;
  493. fullScreenMode = false;
  494. toggleOverlap("off");
  495. }
  496. /**
  497. * This function fits the target element to the screen by calculating
  498. * the required scale and offsets. It also updates the global variables
  499. * zoomLevel, panX, and panY to reflect the new state.
  500. */
  501. // Fullscreen mode
  502. function fitToScreen() {
  503. const canvas = gradioApp().querySelector(
  504. `${elemId} canvas[key="interface"]`
  505. );
  506. if (!canvas) return;
  507. if (canvas.offsetWidth > 862 || isExtension) {
  508. targetElement.style.width = (canvas.offsetWidth + 2) + "px";
  509. }
  510. if (isExtension) {
  511. targetElement.style.overflow = "visible";
  512. }
  513. if (fullScreenMode) {
  514. resetZoom();
  515. fullScreenMode = false;
  516. return;
  517. }
  518. //Reset Zoom
  519. targetElement.style.transform = `translate(${0}px, ${0}px) scale(${1})`;
  520. // Get scrollbar width to right-align the image
  521. const scrollbarWidth =
  522. window.innerWidth - document.documentElement.clientWidth;
  523. // Get element and screen dimensions
  524. const elementWidth = targetElement.offsetWidth;
  525. const elementHeight = targetElement.offsetHeight;
  526. const screenWidth = window.innerWidth - scrollbarWidth;
  527. const screenHeight = window.innerHeight;
  528. // Get element's coordinates relative to the page
  529. const elementRect = targetElement.getBoundingClientRect();
  530. const elementY = elementRect.y;
  531. const elementX = elementRect.x;
  532. // Calculate scale and offsets
  533. const scaleX = screenWidth / elementWidth;
  534. const scaleY = screenHeight / elementHeight;
  535. const scale = Math.min(scaleX, scaleY);
  536. // Get the current transformOrigin
  537. const computedStyle = window.getComputedStyle(targetElement);
  538. const transformOrigin = computedStyle.transformOrigin;
  539. const [originX, originY] = transformOrigin.split(" ");
  540. const originXValue = parseFloat(originX);
  541. const originYValue = parseFloat(originY);
  542. // Calculate offsets with respect to the transformOrigin
  543. const offsetX =
  544. (screenWidth - elementWidth * scale) / 2 -
  545. elementX -
  546. originXValue * (1 - scale);
  547. const offsetY =
  548. (screenHeight - elementHeight * scale) / 2 -
  549. elementY -
  550. originYValue * (1 - scale);
  551. // Apply scale and offsets to the element
  552. targetElement.style.transform = `translate(${offsetX}px, ${offsetY}px) scale(${scale})`;
  553. // Update global variables
  554. elemData[elemId].zoomLevel = scale;
  555. elemData[elemId].panX = offsetX;
  556. elemData[elemId].panY = offsetY;
  557. fullScreenMode = true;
  558. toggleOverlap("on");
  559. }
  560. // Handle keydown events
  561. function handleKeyDown(event) {
  562. // Disable key locks to make pasting from the buffer work correctly
  563. if ((event.ctrlKey && event.code === 'KeyV') || (event.ctrlKey && event.code === 'KeyC') || event.code === "F5") {
  564. return;
  565. }
  566. // before activating shortcut, ensure user is not actively typing in an input field
  567. if (!hotkeysConfig.canvas_blur_prompt) {
  568. if (event.target.nodeName === 'TEXTAREA' || event.target.nodeName === 'INPUT') {
  569. return;
  570. }
  571. }
  572. const hotkeyActions = {
  573. [hotkeysConfig.canvas_hotkey_reset]: resetZoom,
  574. [hotkeysConfig.canvas_hotkey_overlap]: toggleOverlap,
  575. [hotkeysConfig.canvas_hotkey_fullscreen]: fitToScreen,
  576. [hotkeysConfig.canvas_hotkey_shrink_brush]: () => adjustBrushSize(elemId, 10),
  577. [hotkeysConfig.canvas_hotkey_grow_brush]: () => adjustBrushSize(elemId, -10)
  578. };
  579. const action = hotkeyActions[event.code];
  580. if (action) {
  581. event.preventDefault();
  582. action(event);
  583. }
  584. if (
  585. isModifierKey(event, hotkeysConfig.canvas_hotkey_zoom) ||
  586. isModifierKey(event, hotkeysConfig.canvas_hotkey_adjust)
  587. ) {
  588. event.preventDefault();
  589. }
  590. }
  591. // Get Mouse position
  592. function getMousePosition(e) {
  593. mouseX = e.offsetX;
  594. mouseY = e.offsetY;
  595. }
  596. // Simulation of the function to put a long image into the screen.
  597. // We detect if an image has a scroll bar or not, make a fullscreen to reveal the image, then reduce it to fit into the element.
  598. // We hide the image and show it to the user when it is ready.
  599. targetElement.isExpanded = false;
  600. function autoExpand() {
  601. const canvas = document.querySelector(`${elemId} canvas[key="interface"]`);
  602. if (canvas) {
  603. if (hasHorizontalScrollbar(targetElement) && targetElement.isExpanded === false) {
  604. targetElement.style.visibility = "hidden";
  605. setTimeout(() => {
  606. fitToScreen();
  607. resetZoom();
  608. targetElement.style.visibility = "visible";
  609. targetElement.isExpanded = true;
  610. }, 10);
  611. }
  612. }
  613. }
  614. targetElement.addEventListener("mousemove", getMousePosition);
  615. //observers
  616. // Creating an observer with a callback function to handle DOM changes
  617. const observer = new MutationObserver((mutationsList, observer) => {
  618. for (let mutation of mutationsList) {
  619. // If the style attribute of the canvas has changed, by observation it happens only when the picture changes
  620. if (mutation.type === 'attributes' && mutation.attributeName === 'style' &&
  621. mutation.target.tagName.toLowerCase() === 'canvas') {
  622. targetElement.isExpanded = false;
  623. setTimeout(resetZoom, 10);
  624. }
  625. }
  626. });
  627. // Apply auto expand if enabled
  628. if (hotkeysConfig.canvas_auto_expand) {
  629. targetElement.addEventListener("mousemove", autoExpand);
  630. // Set up an observer to track attribute changes
  631. observer.observe(targetElement, {attributes: true, childList: true, subtree: true});
  632. }
  633. // Handle events only inside the targetElement
  634. let isKeyDownHandlerAttached = false;
  635. function handleMouseMove() {
  636. if (!isKeyDownHandlerAttached) {
  637. document.addEventListener("keydown", handleKeyDown);
  638. isKeyDownHandlerAttached = true;
  639. activeElement = elemId;
  640. }
  641. }
  642. function handleMouseLeave() {
  643. if (isKeyDownHandlerAttached) {
  644. document.removeEventListener("keydown", handleKeyDown);
  645. isKeyDownHandlerAttached = false;
  646. activeElement = null;
  647. }
  648. }
  649. // Add mouse event handlers
  650. targetElement.addEventListener("mousemove", handleMouseMove);
  651. targetElement.addEventListener("mouseleave", handleMouseLeave);
  652. // Reset zoom when click on another tab
  653. elements.img2imgTabs.addEventListener("click", resetZoom);
  654. elements.img2imgTabs.addEventListener("click", () => {
  655. // targetElement.style.width = "";
  656. if (parseInt(targetElement.style.width) > 865) {
  657. setTimeout(fitToElement, 0);
  658. }
  659. });
  660. targetElement.addEventListener("wheel", e => {
  661. // change zoom level
  662. const operation = e.deltaY > 0 ? "-" : "+";
  663. changeZoomLevel(operation, e);
  664. // Handle brush size adjustment with ctrl key pressed
  665. if (isModifierKey(e, hotkeysConfig.canvas_hotkey_adjust)) {
  666. e.preventDefault();
  667. // Increase or decrease brush size based on scroll direction
  668. adjustBrushSize(elemId, e.deltaY);
  669. }
  670. });
  671. // Handle the move event for pan functionality. Updates the panX and panY variables and applies the new transform to the target element.
  672. function handleMoveKeyDown(e) {
  673. // Disable key locks to make pasting from the buffer work correctly
  674. if ((e.ctrlKey && e.code === 'KeyV') || (e.ctrlKey && event.code === 'KeyC') || e.code === "F5") {
  675. return;
  676. }
  677. // before activating shortcut, ensure user is not actively typing in an input field
  678. if (!hotkeysConfig.canvas_blur_prompt) {
  679. if (e.target.nodeName === 'TEXTAREA' || e.target.nodeName === 'INPUT') {
  680. return;
  681. }
  682. }
  683. if (e.code === hotkeysConfig.canvas_hotkey_move) {
  684. if (!e.ctrlKey && !e.metaKey && isKeyDownHandlerAttached) {
  685. e.preventDefault();
  686. document.activeElement.blur();
  687. isMoving = true;
  688. }
  689. }
  690. }
  691. function handleMoveKeyUp(e) {
  692. if (e.code === hotkeysConfig.canvas_hotkey_move) {
  693. isMoving = false;
  694. }
  695. }
  696. document.addEventListener("keydown", handleMoveKeyDown);
  697. document.addEventListener("keyup", handleMoveKeyUp);
  698. // Detect zoom level and update the pan speed.
  699. function updatePanPosition(movementX, movementY) {
  700. let panSpeed = 2;
  701. if (elemData[elemId].zoomLevel > 8) {
  702. panSpeed = 3.5;
  703. }
  704. elemData[elemId].panX += movementX * panSpeed;
  705. elemData[elemId].panY += movementY * panSpeed;
  706. // Delayed redraw of an element
  707. requestAnimationFrame(() => {
  708. targetElement.style.transform = `translate(${elemData[elemId].panX}px, ${elemData[elemId].panY}px) scale(${elemData[elemId].zoomLevel})`;
  709. toggleOverlap("on");
  710. });
  711. }
  712. function handleMoveByKey(e) {
  713. if (isMoving && elemId === activeElement) {
  714. updatePanPosition(e.movementX, e.movementY);
  715. targetElement.style.pointerEvents = "none";
  716. if (isExtension) {
  717. targetElement.style.overflow = "visible";
  718. }
  719. } else {
  720. targetElement.style.pointerEvents = "auto";
  721. }
  722. }
  723. // Prevents sticking to the mouse
  724. window.onblur = function() {
  725. isMoving = false;
  726. };
  727. // Checks for extension
  728. function checkForOutBox() {
  729. const parentElement = targetElement.closest('[id^="component-"]');
  730. if (parentElement.offsetWidth < targetElement.offsetWidth && !targetElement.isExpanded) {
  731. resetZoom();
  732. targetElement.isExpanded = true;
  733. }
  734. if (parentElement.offsetWidth < targetElement.offsetWidth && elemData[elemId].zoomLevel == 1) {
  735. resetZoom();
  736. }
  737. if (parentElement.offsetWidth < targetElement.offsetWidth && targetElement.offsetWidth * elemData[elemId].zoomLevel > parentElement.offsetWidth && elemData[elemId].zoomLevel < 1 && !targetElement.isZoomed) {
  738. resetZoom();
  739. }
  740. }
  741. if (isExtension) {
  742. targetElement.addEventListener("mousemove", checkForOutBox);
  743. }
  744. window.addEventListener('resize', (e) => {
  745. resetZoom();
  746. if (isExtension) {
  747. targetElement.isExpanded = false;
  748. targetElement.isZoomed = false;
  749. }
  750. });
  751. gradioApp().addEventListener("mousemove", handleMoveByKey);
  752. }
  753. applyZoomAndPan(elementIDs.sketch, false);
  754. applyZoomAndPan(elementIDs.inpaint, false);
  755. applyZoomAndPan(elementIDs.inpaintSketch, false);
  756. // Make the function global so that other extensions can take advantage of this solution
  757. const applyZoomAndPanIntegration = async(id, elementIDs) => {
  758. const mainEl = document.querySelector(id);
  759. if (id.toLocaleLowerCase() === "none") {
  760. for (const elementID of elementIDs) {
  761. const el = await waitForElement(elementID);
  762. if (!el) break;
  763. applyZoomAndPan(elementID);
  764. }
  765. return;
  766. }
  767. if (!mainEl) return;
  768. mainEl.addEventListener("click", async() => {
  769. for (const elementID of elementIDs) {
  770. const el = await waitForElement(elementID);
  771. if (!el) break;
  772. applyZoomAndPan(elementID);
  773. }
  774. }, {once: true});
  775. };
  776. window.applyZoomAndPan = applyZoomAndPan; // Only 1 elements, argument elementID, for example applyZoomAndPan("#txt2img_controlnet_ControlNet_input_image")
  777. window.applyZoomAndPanIntegration = applyZoomAndPanIntegration; // for any extension
  778. /*
  779. The function `applyZoomAndPanIntegration` takes two arguments:
  780. 1. `id`: A string identifier for the element to which zoom and pan functionality will be applied on click.
  781. If the `id` value is "none", the functionality will be applied to all elements specified in the second argument without a click event.
  782. 2. `elementIDs`: An array of string identifiers for elements. Zoom and pan functionality will be applied to each of these elements on click of the element specified by the first argument.
  783. If "none" is specified in the first argument, the functionality will be applied to each of these elements without a click event.
  784. Example usage:
  785. applyZoomAndPanIntegration("#txt2img_controlnet", ["#txt2img_controlnet_ControlNet_input_image"]);
  786. In this example, zoom and pan functionality will be applied to the element with the identifier "txt2img_controlnet_ControlNet_input_image" upon clicking the element with the identifier "txt2img_controlnet".
  787. */
  788. // More examples
  789. // Add integration with ControlNet txt2img One TAB
  790. // applyZoomAndPanIntegration("#txt2img_controlnet", ["#txt2img_controlnet_ControlNet_input_image"]);
  791. // Add integration with ControlNet txt2img Tabs
  792. // applyZoomAndPanIntegration("#txt2img_controlnet",Array.from({ length: 10 }, (_, i) => `#txt2img_controlnet_ControlNet-${i}_input_image`));
  793. // Add integration with Inpaint Anything
  794. // applyZoomAndPanIntegration("None", ["#ia_sam_image", "#ia_sel_mask"]);
  795. });