|
200 | 200 | contextMenu.style.borderRadius = "5px"; |
201 | 201 | contextMenu.style.zIndex = "1000"; |
202 | 202 |
|
203 | | - // on hover |
204 | | - contextMenu.addEventListener("mouseenter", () => { |
205 | | - contextMenu.style.backgroundColor = "#ededed"; |
206 | | - }); |
| 203 | + // Helper to add hover effect to menu options |
| 204 | + function addHoverEffect(optionDiv) { |
| 205 | + optionDiv.addEventListener("mouseenter", () => { |
| 206 | + optionDiv.style.backgroundColor = "#ededed"; |
| 207 | + }); |
| 208 | + optionDiv.addEventListener("mouseleave", () => { |
| 209 | + optionDiv.style.backgroundColor = "#fff"; |
| 210 | + }); |
| 211 | + } |
207 | 212 |
|
208 | | - contextMenu.addEventListener("mouseover", () => { |
209 | | - contextMenu.style.backgroundColor = "#ededed"; |
| 213 | + // Copy image to clipboard option |
| 214 | + const copyOption = document.createElement("div"); |
| 215 | + copyOption.textContent = "Copy image to clipboard"; |
| 216 | + copyOption.style.cursor = "pointer"; |
| 217 | + copyOption.style.padding = "3px"; |
| 218 | + addHoverEffect(copyOption); |
| 219 | + |
| 220 | + copyOption.addEventListener("click", async () => { |
| 221 | + copyOption.textContent = "Copying..."; |
| 222 | + try { |
| 223 | + // Use toBlob to get the image as a Blob |
| 224 | + offscreenCanvas.toBlob(async (blob) => { |
| 225 | + try { |
| 226 | + await navigator.clipboard.write([ |
| 227 | + new ClipboardItem({ |
| 228 | + "image/png": blob, |
| 229 | + }), |
| 230 | + ]); |
| 231 | + copyOption.textContent = "Copied!"; |
| 232 | + } catch (err) { |
| 233 | + copyOption.textContent = "Failed to copy"; |
| 234 | + } |
| 235 | + setTimeout(() => { |
| 236 | + copyOption.textContent = "Copy image to clipboard"; |
| 237 | + }, 1000); |
| 238 | + }, "image/png"); |
| 239 | + } catch (err) { |
| 240 | + copyOption.textContent = "Failed to copy"; |
| 241 | + setTimeout(() => { |
| 242 | + copyOption.textContent = "Copy image to clipboard"; |
| 243 | + }, 1000); |
| 244 | + } |
210 | 245 | }); |
211 | 246 |
|
212 | | - contextMenu.addEventListener("mouseout", () => { |
213 | | - contextMenu.style.backgroundColor = "#fff"; |
214 | | - }); |
| 247 | + contextMenu.appendChild(copyOption); |
215 | 248 |
|
| 249 | + // Save image as png option |
216 | 250 | const saveOption = document.createElement("div"); |
217 | 251 | saveOption.textContent = "Save image as png"; |
218 | 252 | saveOption.style.cursor = "pointer"; |
219 | 253 | saveOption.style.padding = "3px"; |
| 254 | + addHoverEffect(saveOption); |
220 | 255 |
|
221 | 256 | saveOption.addEventListener("click", () => { |
222 | 257 | console.time("saveOption"); |
|
0 commit comments