| | |
| | | // ], |
| | | closeContextMenuListenr: undefined, |
| | | // 右键选中的图片 |
| | | selectedFileElement: undefined, |
| | | selectedFile: undefined, |
| | | selectedIndex: undefined, |
| | | selectedTypeId: undefined |
| | |
| | | }; |
| | | }); |
| | | return [ |
| | | // { label: '复制图片', action: 'copy' }, |
| | | // { label: '复制到剪贴板', action: 'copy' }, |
| | | { label: '移动到', children: items } |
| | | ]; |
| | | } |
| | |
| | | left: `${event.clientX}px`, |
| | | top: `${event.clientY}px` |
| | | }; |
| | | this.selectedFileElement = event.target; |
| | | this.selectedTypeId = typeId; |
| | | this.selectedIndex = index; |
| | | this.selectedFile = this.typesMap.get(typeId)[index]; |
| | |
| | | handleMenuItem(item) { |
| | | switch (item.action) { |
| | | case 'copy': |
| | | this.drawImageToCanvas(); |
| | | break; |
| | | case 'move': |
| | | this.selectedFile.data.businesstypeid = item.value; |
| | |
| | | // } |
| | | // this.selectedFile.loading = false; |
| | | // }, 2000); |
| | | }, |
| | | drawImageToCanvas() { |
| | | const canvas = document.createElement('canvas'); |
| | | const ctx = canvas.getContext('2d'); |
| | | const img = this.selectedFileElement; // 获取DOM引用 |
| | | // img.crossOrigin = 'Anonymous'; |
| | | canvas.width = img.naturalWidth; |
| | | canvas.height = img.naturalHeight; |
| | | ctx.drawImage(img, 0, 0, canvas.width, canvas.height); |
| | | // this.copyCanvasToClipboard(canvas); |
| | | const dataUrl = canvas.toDataURL('image/png'); // 可以选择其他格式如'image/jpeg' |
| | | // 创建一个临时的textarea元素来复制文本 |
| | | const tempTextArea = document.createElement('textarea'); |
| | | document.body.appendChild(tempTextArea); |
| | | tempTextArea.value = dataUrl; |
| | | tempTextArea.select(); |
| | | document.execCommand('copy'); |
| | | document.body.removeChild(tempTextArea); |
| | | }, |
| | | copyCanvasToClipboard(canvas) { |
| | | canvas.toBlob((blob) => { |
| | | const item = new ClipboardItem({ 'image/png': blob }); // 你可以根据需要改变格式,如 "image/jpeg" |
| | | navigator.clipboard.write([item]).then( |
| | | () => { |
| | | console.log('Image copied to clipboard'); |
| | | }, |
| | | (err) => { |
| | | console.error('Could not copy image: ', err); |
| | | } |
| | | ); |
| | | }, 'image/png'); // 同样,这里也可以指定其他格式,如 'image/jpeg' |
| | | } |
| | | } |
| | | }; |