seal.vue 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486
  1. <template>
  2. <div class="app-container">
  3. <div class="tool-card">
  4. <div class="converter-layout">
  5. <!-- 右侧: 预览 & Canvas 结果 -->
  6. <div id="captureTarget" style="position: fixed; left: -100000px">
  7. <div v-html="editableHtml"> </div>
  8. </div>
  9. <div class="preview-panel">
  10. <div class="canvas-result">
  11. <canvas id="outputCanvas" v-show="canvasGenerated"></canvas>
  12. <!-- <div class="canvas-buttons" v-if="canvasGenerated">
  13. <button @click="downloadCanvasAsImage" class="download">
  14. 📥 下载图片
  15. </button>
  16. <button @click="exportToPDF" class="pdf"> 📄 导出PDF </button>
  17. <button @click="printCanvas" class="print"> 🖨️ 打印 </button>
  18. </div> -->
  19. </div>
  20. </div>
  21. </div>
  22. </div>
  23. <!-- 图片素材区域 -->
  24. <div class="images-panel">
  25. <!-- 公用章 -->
  26. <div class="seal-section">
  27. <div class="panel-title">公用章</div>
  28. <div class="images-grid">
  29. <div
  30. v-for="(img, index) in publicImages"
  31. :key="'public-' + index"
  32. class="drag-item"
  33. draggable="true"
  34. @dragstart="handleDragStart($event, img, index, 'public')"
  35. >
  36. <img :src="img.imgUrl" :alt="img.name" />
  37. <div class="drag-item-name">{{ img.name }}</div>
  38. </div>
  39. <div v-if="!publicImages.length" class="no-data">暂无公用章</div>
  40. </div>
  41. </div>
  42. <!-- 个人章 -->
  43. <div class="seal-section">
  44. <div class="panel-title">个人章</div>
  45. <div class="images-grid">
  46. <div
  47. v-for="(img, index) in privateImages"
  48. :key="'private-' + index"
  49. class="drag-item"
  50. draggable="true"
  51. @dragstart="handleDragStart($event, img, index, 'private')"
  52. >
  53. <img :src="img.imgUrl" :alt="img.name" />
  54. <div class="drag-item-name">{{ img.name }}</div>
  55. </div>
  56. <div v-if="!privateImages.length" class="no-data">暂无个人章</div>
  57. </div>
  58. </div>
  59. </div>
  60. </div>
  61. </template>
  62. <script>
  63. import {
  64. getSealPage,
  65. getPrivateSealPage
  66. } from '@/api/bpm/components/sealManagement';
  67. import { getUserDetail } from '@/api/system/organization/index';
  68. // 引入 html2canvas 和 jsPDF
  69. import html2canvas from 'html2canvas';
  70. import jsPDF from 'jspdf';
  71. // const imageUrl = [
  72. // require('@/assets/0.jpg'),
  73. // require('@/assets/1.jpg'),
  74. // require('@/assets/2.jpg'),
  75. // require('@/assets/3.jpg'),
  76. // require('@/assets/4.jpg'),
  77. // require('@/assets/5.jpg')
  78. // ]; // 使用相对路径或别名路径
  79. // // 默认精美的示例HTML + CSS (内联样式丰富,展示能力)
  80. // const DEFAULT_HTML = `
  81. // <div class="container">
  82. // <div class="img-area">
  83. // <img class="my-photo" alt="loading" src="${imageUrl[0]}">
  84. // </div>
  85. // <div class="img-area">
  86. // <img class="my-photo" alt="loading" src="${imageUrl[1]}">
  87. // </div>
  88. // <div class="img-area">
  89. // <img class="my-photo" alt="loading" src="${imageUrl[2]}">
  90. // </div> <div class="img-area">
  91. // <img class="my-photo" alt="loading" src="${imageUrl[3]}">
  92. // </div> <div class="img-area">
  93. // <img class="my-photo" alt="loading" src="${imageUrl[4]}">
  94. // </div> <div class="img-area">
  95. // <img class="my-photo" alt="loading" src="${imageUrl[5]}">
  96. // </div>
  97. // </div>
  98. // `;
  99. export default {
  100. name: 'HtmlToCanvas',
  101. data() {
  102. return {
  103. editableHtml: '',
  104. canvasGenerated: false,
  105. publicImages: [],
  106. privateImages: [],
  107. droppedImages: [],
  108. selectedImage: null,
  109. baseCanvasData: null, // 缓存基础内容的 Canvas 数据
  110. _isMoving: false, // 是否正在移动图片
  111. _currentMoveImageId: null, // 当前正在移动的图片ID
  112. _moveHandler: null, // 移动事件处理函数引用
  113. _mouseUpHandler: null, // 鼠标释放事件处理函数引用
  114. _dragOverHandler: null, // dragover事件处理函数引用
  115. _dropHandler: null, // drop事件处理函数引用
  116. _contextMenuHandler: null // contextmenu事件处理函数引用
  117. };
  118. },
  119. created() {
  120. // 竞态控制字段(非响应式,挂在实例上避免响应式开销)
  121. this._renderSeq = 0; // 渲染序号,用于竞态控制
  122. this._lastRenderPromise = null; // 最近一次渲染完成的 Promise
  123. this._renderResolve = null; // 最近一次渲染的 resolve 回调
  124. this._renderReject = null; // 最近一次渲染的 reject 回调
  125. // 获取公用章列表
  126. getSealPage({
  127. size: 999,
  128. pageNum: 1,
  129. status: 1,
  130. approvalStatus: 2,
  131. sealHolderId: this.$store.state.user.info.userId
  132. }).then((res) => {
  133. this.publicImages = res.list;
  134. });
  135. // 获取个人章列表
  136. getUserDetail(this.$store.state.user.info.userId).then((res) => {
  137. if (res.signature?.[0]) {
  138. this.privateImages = [
  139. {
  140. imgUrl: res.signature[0].url,
  141. name: res.name
  142. }
  143. ];
  144. console.log(this.privateImages, 'this.privateImages ');
  145. }
  146. });
  147. },
  148. mounted() {
  149. // 组件挂载后自动渲染一次
  150. this.$nextTick(() => {
  151. this.renderToCanvas();
  152. });
  153. // 保存组件实例引用,用于事件处理器
  154. window._aaComponentInstance = this;
  155. // 创建全局安全移除函数
  156. window._safeRemoveElement = function (element) {
  157. try {
  158. if (
  159. element &&
  160. element.parentNode &&
  161. document.body.contains(element)
  162. ) {
  163. element.parentNode.removeChild(element);
  164. return true;
  165. }
  166. } catch (err) {
  167. console.warn('安全移除元素时发生警告:', err);
  168. }
  169. return false;
  170. };
  171. },
  172. beforeDestroy() {
  173. // 清理移动事件监听器
  174. this._cancelMove();
  175. // 清理 Canvas 事件监听器
  176. const canvas = document.getElementById('outputCanvas');
  177. if (canvas) {
  178. if (this._dragOverHandler) {
  179. canvas.removeEventListener('dragover', this._dragOverHandler);
  180. }
  181. if (this._dropHandler) {
  182. canvas.removeEventListener('drop', this._dropHandler);
  183. }
  184. if (this._contextMenuHandler) {
  185. canvas.removeEventListener('contextmenu', this._contextMenuHandler);
  186. }
  187. }
  188. // 清理引用
  189. if (window._aaComponentInstance === this) {
  190. window._aaComponentInstance = null;
  191. }
  192. if (window._safeRemoveElement) {
  193. window._safeRemoveElement = null;
  194. }
  195. },
  196. methods: {
  197. // 获取组件实例
  198. getComponentInstance() {
  199. return window._aaComponentInstance || this;
  200. },
  201. // 安全的移除DOM元素
  202. safeRemoveElement(elementId) {
  203. try {
  204. const element = document.getElementById(elementId);
  205. if (window._safeRemoveElement) {
  206. return window._safeRemoveElement(element);
  207. }
  208. // 回退方案
  209. if (
  210. element &&
  211. element.parentNode &&
  212. document.body.contains(element)
  213. ) {
  214. element.parentNode.removeChild(element);
  215. return true;
  216. }
  217. return false;
  218. } catch (err) {
  219. console.warn(`移除元素 ${elementId} 时发生警告:`, err);
  220. return false;
  221. }
  222. },
  223. // 安全移除所有菜单
  224. safeRemoveAllMenus() {
  225. try {
  226. const existingMenus = document.querySelectorAll('[id^="menu_"]');
  227. // 使用倒序循环,避免在移除时影响NodeList
  228. for (let i = existingMenus.length - 1; i >= 0; i--) {
  229. const menu = existingMenus[i];
  230. if (menu && document.body.contains(menu) && menu.parentNode) {
  231. try {
  232. menu.parentNode.removeChild(menu);
  233. } catch (err) {
  234. console.warn('移除菜单时发生警告:', err);
  235. }
  236. }
  237. }
  238. } catch (err) {
  239. console.warn('移除所有菜单时发生错误:', err);
  240. }
  241. },
  242. // 设置 Canvas 拖放区域和点击事件
  243. setupCanvasDropZone() {
  244. const canvas = document.getElementById('outputCanvas');
  245. if (!canvas) {
  246. console.log('Canvas 元素不存在');
  247. return;
  248. }
  249. console.log('设置拖放区域和点击事件,Canvas ID:', canvas.id);
  250. console.log('Canvas 尺寸:', canvas.width, 'x', canvas.height);
  251. console.log(
  252. 'Canvas 可见性:',
  253. canvas.style.display,
  254. canvas.style.visibility
  255. );
  256. console.log('Canvas 位置:', canvas.getBoundingClientRect());
  257. // 先移除旧的事件监听器,防止重复绑定
  258. if (this._dragOverHandler) {
  259. canvas.removeEventListener('dragover', this._dragOverHandler);
  260. }
  261. if (this._dropHandler) {
  262. canvas.removeEventListener('drop', this._dropHandler);
  263. }
  264. if (this._contextMenuHandler) {
  265. canvas.removeEventListener('contextmenu', this._contextMenuHandler);
  266. }
  267. // 创建事件处理函数并保存引用
  268. this._dragOverHandler = (e) => {
  269. e.preventDefault();
  270. e.dataTransfer.dropEffect = 'copy';
  271. };
  272. this._dropHandler = (e) => {
  273. e.preventDefault();
  274. console.log('触发 drop 事件');
  275. const imageData = e.dataTransfer.getData('imageData');
  276. console.log('获取到的 imageData:', imageData);
  277. if (imageData) {
  278. const data = JSON.parse(imageData);
  279. console.log('解析后的数据:', data);
  280. // 计算 Canvas 坐标到实际像素坐标的比例
  281. const rect = canvas.getBoundingClientRect();
  282. const scaleX = canvas.width / rect.width;
  283. const scaleY = canvas.height / rect.height;
  284. // 计算点击位置相对于 Canvas 的坐标(实际像素坐标)
  285. const x = e.offsetX * scaleX;
  286. const y = e.offsetY * scaleY;
  287. this.addImageToCanvas(data, x, y);
  288. }
  289. };
  290. this._contextMenuHandler = (e) => {
  291. e.preventDefault();
  292. this.handleCanvasRightClick(e);
  293. };
  294. // 添加新的事件监听器
  295. canvas.addEventListener('dragover', this._dragOverHandler);
  296. canvas.addEventListener('drop', this._dropHandler);
  297. canvas.addEventListener('contextmenu', this._contextMenuHandler);
  298. },
  299. // 处理拖拽开始
  300. handleDragStart(e, img, index, type) {
  301. const data = {
  302. url: img.imgUrl,
  303. name: img.name,
  304. version: img.version,
  305. imgId: type == 'public' ? img.id : '',
  306. sealType: type // 传递签章类型
  307. };
  308. e.dataTransfer.setData('imageData', JSON.stringify(data));
  309. e.dataTransfer.effectAllowed = 'copy';
  310. console.log('拖拽开始:', data);
  311. },
  312. // 添加图片到 Canvas
  313. addImageToCanvas(data, x, y) {
  314. const canvas = document.getElementById('outputCanvas');
  315. if (!canvas || !this.canvasGenerated) {
  316. console.log('请先渲染 Canvas');
  317. return;
  318. }
  319. console.log('Canvas 尺寸:', canvas.width, canvas.height);
  320. console.log('拖放位置:', x, y);
  321. const ctx = canvas.getContext('2d');
  322. const img = new Image();
  323. img.crossOrigin = 'anonymous';
  324. img.onload = () => {
  325. console.log('图片加载成功,原始尺寸:', img.width, img.height);
  326. // 根据签章类型设置尺寸:公用章 120x120,个人章 120x40
  327. let width, height;
  328. if (data.sealType === 'private') {
  329. width = 210;
  330. height = 70;
  331. } else {
  332. width = 200;
  333. height = 200;
  334. }
  335. const posX = x - width / 2;
  336. const posY = y - height / 2;
  337. console.log('绘制位置:', posX, posY, '尺寸:', width, height);
  338. // 绘制图片
  339. ctx.drawImage(img, posX, posY, width, height);
  340. const imageId = `img_${Date.now()}_${Math.random()
  341. .toString(36)
  342. .substr(2, 9)}`;
  343. this.droppedImages.push({
  344. id: imageId,
  345. imgId: data.imgId,
  346. imgUrl: data.url,
  347. version: data.version,
  348. name:data.name,
  349. x: posX,
  350. y: posY,
  351. width,
  352. height,
  353. originalWidth: img.width,
  354. originalHeight: img.height,
  355. sealType: data.sealType
  356. });
  357. console.log('已添加的图片数量:', this.droppedImages.length);
  358. this.$message({
  359. showClose: true,
  360. message: '操作成功',
  361. type: 'success'
  362. });
  363. // 设置图片可再次拖动和调整(通过右键菜单)
  364. // 不再创建覆盖层,仅通过右键菜单操作
  365. };
  366. img.onerror = (err) => {
  367. console.error('图片加载失败:', err);
  368. this.$message({
  369. showClose: true,
  370. message: '操作失败',
  371. type: 'error'
  372. });
  373. // this.showMessage('⚠️ 图片加载失败', 'error');
  374. };
  375. img.src = data.url;
  376. },
  377. // 重新绘制整个 Canvas
  378. redrawCanvas() {
  379. const canvas = document.getElementById('outputCanvas');
  380. if (!canvas || !this.canvasGenerated) return;
  381. const ctx = canvas.getContext('2d');
  382. try {
  383. // 清空 Canvas
  384. ctx.clearRect(0, 0, canvas.width, canvas.height);
  385. // 如果有缓存的基础内容,直接绘制
  386. if (this.baseCanvasData) {
  387. ctx.drawImage(this.baseCanvasData, 0, 0);
  388. } else {
  389. // 如果没有缓存,重新渲染基础内容
  390. console.warn('没有缓存的基础内容,可能需要重新渲染');
  391. return;
  392. }
  393. // 重新绘制所有拖入的图片
  394. this.droppedImages.forEach((image) => {
  395. const img = new Image();
  396. img.onload = () => {
  397. ctx.drawImage(img, image.x, image.y, image.width, image.height);
  398. };
  399. img.src = image.imgUrl;
  400. });
  401. console.log(
  402. 'Canvas 已快速重绘,包含基础内容和',
  403. this.droppedImages.length,
  404. '张图片'
  405. );
  406. // 确保事件监听仍然有效
  407. setTimeout(() => {
  408. this.setupCanvasDropZone();
  409. }, 100);
  410. } catch (error) {
  411. console.error('重新绘制 Canvas 失败:', error);
  412. }
  413. },
  414. // 显示图片控制菜单
  415. showImageControls(imageId, x, y) {
  416. console.log('显示图片控制菜单,位置:', x, y, '图片ID:', imageId);
  417. // 保存组件实例引用
  418. const self = this;
  419. // 先移除可能存在的其他菜单
  420. this.safeRemoveAllMenus();
  421. // 定义菜单选项(使用局部变量 self)
  422. const options = [
  423. { text: '放大', action: () => self.resizeImage(imageId, 1.2) },
  424. { text: '缩小', action: () => self.resizeImage(imageId, 0.8) },
  425. { text: '移动', action: () => self.startMoveImage(imageId, x, y) },
  426. { text: '删除', action: () => self.removeImage(imageId) }
  427. ];
  428. const menu = document.createElement('div');
  429. menu.id = `menu_${imageId}`;
  430. menu.style.position = 'fixed';
  431. // 调整位置,避免菜单超出屏幕边界
  432. const menuWidth = 120;
  433. const menuHeight = options.length * 40 + 16; // 估算高度
  434. let left = x;
  435. let top = y;
  436. // 如果菜单会超出右侧边界,向左移动
  437. if (left + menuWidth > window.innerWidth) {
  438. left = window.innerWidth - menuWidth - 10;
  439. }
  440. // 如果菜单会超出底部边界,向上移动
  441. if (top + menuHeight > window.innerHeight) {
  442. top = window.innerHeight - menuHeight - 10;
  443. }
  444. menu.style.left = `${left}px`;
  445. menu.style.top = `${top}px`;
  446. menu.style.background = 'white';
  447. menu.style.border = '1px solid #e2e8f0';
  448. menu.style.borderRadius = '8px';
  449. menu.style.padding = '8px 0';
  450. menu.style.boxShadow = '0 4px 12px rgba(0, 0, 0, 0.1)';
  451. menu.style.zIndex = '9999'; // 使用更高的 z-index
  452. menu.style.minWidth = `${menuWidth}px`;
  453. menu.style.opacity = '1';
  454. menu.style.visibility = 'visible';
  455. menu.style.display = 'block';
  456. console.log('菜单位置 - 原始:', x, y, '调整后:', left, top);
  457. options.forEach((option) => {
  458. const item = document.createElement('div');
  459. item.innerText = option.text;
  460. item.style.padding = '8px 16px';
  461. item.style.cursor = 'pointer';
  462. item.style.fontSize = '14px';
  463. item.style.color = '#374151';
  464. item.addEventListener('mouseenter', () => {
  465. item.style.background = '#f3f4f6';
  466. });
  467. item.addEventListener('mouseleave', () => {
  468. item.style.background = 'white';
  469. });
  470. item.addEventListener('click', () => {
  471. option.action();
  472. // 直接移除当前菜单
  473. const menuToRemove = document.getElementById(`menu_${imageId}`);
  474. if (window._safeRemoveElement) {
  475. window._safeRemoveElement(menuToRemove);
  476. }
  477. });
  478. menu.appendChild(item);
  479. });
  480. document.body.appendChild(menu);
  481. console.log('菜单已添加到 DOM,父元素:', menu.parentElement?.tagName);
  482. // 点击其他地方关闭菜单
  483. const closeMenu = (e) => {
  484. console.log('closeMenu 被调用,目标:', e.target);
  485. const currentMenu = document.getElementById(`menu_${imageId}`);
  486. if (!currentMenu || !document.body.contains(currentMenu)) {
  487. // 菜单已经不存在,移除监听器
  488. document.removeEventListener('click', closeMenu);
  489. return;
  490. }
  491. if (e.target !== currentMenu && !currentMenu.contains(e.target)) {
  492. // 使用全局安全函数移除菜单
  493. if (window._safeRemoveElement) {
  494. window._safeRemoveElement(currentMenu);
  495. }
  496. document.removeEventListener('click', closeMenu);
  497. }
  498. };
  499. setTimeout(() => {
  500. console.log('添加全局点击监听器');
  501. document.addEventListener('click', closeMenu);
  502. }, 0);
  503. },
  504. // 调整图片大小
  505. resizeImage(imageId, factor) {
  506. const imageIndex = this.droppedImages.findIndex(
  507. (img) => img.id === imageId
  508. );
  509. if (imageIndex === -1) return;
  510. const image = this.droppedImages[imageIndex];
  511. image.width *= factor;
  512. image.height *= factor;
  513. this.redrawCanvas();
  514. },
  515. // 处理 Canvas 右键点击
  516. handleCanvasRightClick(e) {
  517. console.log('Canvas 右键点击事件触发');
  518. const canvas = document.getElementById('outputCanvas');
  519. if (!canvas) {
  520. console.log('Canvas 元素不存在');
  521. return;
  522. }
  523. console.log(
  524. 'Canvas 元素存在,droppedImages 数量:',
  525. this.droppedImages.length
  526. );
  527. if (this.droppedImages.length === 0) {
  528. console.log('没有已添加的图片');
  529. return;
  530. }
  531. const rect = canvas.getBoundingClientRect();
  532. console.log('Canvas 边界矩形:', rect);
  533. const scaleX = canvas.width / rect.width;
  534. const scaleY = canvas.height / rect.height;
  535. // 计算点击位置相对于 Canvas 的坐标
  536. const clickX = (e.clientX - rect.left) * scaleX;
  537. const clickY = (e.clientY - rect.top) * scaleY;
  538. console.log('右键点击位置 - 屏幕:', e.clientX, e.clientY);
  539. console.log('右键点击位置 - Canvas相对:', clickX, clickY);
  540. // 查找点击到的图片
  541. let clickedImage = null;
  542. for (const image of this.droppedImages) {
  543. console.log(
  544. '检查图片:',
  545. image.id,
  546. '位置:',
  547. image.x,
  548. image.y,
  549. '尺寸:',
  550. image.width,
  551. image.height
  552. );
  553. if (
  554. clickX >= image.x &&
  555. clickX <= image.x + image.width &&
  556. clickY >= image.y &&
  557. clickY <= image.y + image.height
  558. ) {
  559. clickedImage = image;
  560. break;
  561. }
  562. }
  563. if (clickedImage) {
  564. console.log('点击到图片:', clickedImage.id);
  565. this.showImageControls(clickedImage.id, e.clientX, e.clientY);
  566. } else {
  567. console.log('未点击到任何图片');
  568. // 可以在这里添加其他右键功能,比如添加新图片等
  569. }
  570. },
  571. // 开始移动图片
  572. startMoveImage(imageId, menuX, menuY) {
  573. console.log('开始移动图片,图片ID:', imageId);
  574. // 关闭菜单
  575. this.safeRemoveElement(`menu_${imageId}`);
  576. const canvas = document.getElementById('outputCanvas');
  577. if (!canvas) {
  578. console.error('Canvas 元素未找到');
  579. return;
  580. }
  581. const imageIndex = this.droppedImages.findIndex(
  582. (img) => img.id === imageId
  583. );
  584. if (imageIndex === -1) {
  585. console.error('图片未找到,ID:', imageId);
  586. return;
  587. }
  588. const image = this.droppedImages[imageIndex];
  589. console.log('找到图片:', image);
  590. // 如果已经在移动中,先移除旧的事件监听器
  591. if (this._isMoving) {
  592. this._cancelMove();
  593. }
  594. // 设置移动状态标志
  595. this._isMoving = true;
  596. this._currentMoveImageId = imageId;
  597. // 创建一个临时指示器
  598. const indicator = document.createElement('div');
  599. indicator.id = `move_indicator_${imageId}`;
  600. indicator.style.position = 'fixed';
  601. indicator.style.left = `${menuX}px`;
  602. indicator.style.top = `${menuY}px`;
  603. indicator.style.width = '40px';
  604. indicator.style.height = '40px';
  605. indicator.style.background = 'rgba(59, 130, 246, 0.3)';
  606. indicator.style.border = '2px dashed #3b82f6';
  607. indicator.style.borderRadius = '50%';
  608. indicator.style.zIndex = '3000';
  609. indicator.style.pointerEvents = 'none';
  610. document.body.appendChild(indicator);
  611. console.log('指示器已创建并添加到DOM');
  612. // 显示提示
  613. this.$message({
  614. showClose: true,
  615. message: '📍 移动鼠标调整位置,释放鼠标完成移动',
  616. type: 'info'
  617. });
  618. // 保存事件处理函数引用,用于后续移除
  619. this._moveHandler = (e) => {
  620. console.log('moveHandler 被调用,鼠标位置:', e.clientX, e.clientY);
  621. const rect = canvas.getBoundingClientRect();
  622. const scaleX = canvas.width / rect.width;
  623. const scaleY = canvas.height / rect.height;
  624. // 计算相对于 canvas 的位置
  625. const newX = (e.clientX - rect.left) * scaleX - image.width / 2;
  626. const newY = (e.clientY - rect.top) * scaleY - image.height / 2;
  627. // 限制在 canvas 范围内
  628. image.x = Math.max(0, Math.min(newX, canvas.width - image.width));
  629. image.y = Math.max(0, Math.min(newY, canvas.height - image.height));
  630. // 更新指示器位置
  631. indicator.style.left = `${e.clientX - 20}px`;
  632. indicator.style.top = `${e.clientY - 20}px`;
  633. // 使用组件实例引用
  634. const component = window._aaComponentInstance || this;
  635. if (component && component.redrawCanvas) {
  636. component.redrawCanvas();
  637. }
  638. };
  639. this._mouseUpHandler = (e) => {
  640. console.log(
  641. 'mouseUpHandler 被调用,位置:',
  642. e.clientX,
  643. e.clientY,
  644. '目标:',
  645. e.target
  646. );
  647. // 无论在哪里释放鼠标,都完成移动
  648. console.log('鼠标释放,完成移动');
  649. // 确保只处理一次
  650. if (indicator.parentNode) {
  651. console.log('指示器仍在DOM中,移除事件监听器');
  652. document.removeEventListener('mousemove', this._moveHandler);
  653. document.removeEventListener('mouseup', this._mouseUpHandler);
  654. indicator.remove();
  655. // 重置移动状态
  656. this._isMoving = false;
  657. this._currentMoveImageId = null;
  658. this._moveHandler = null;
  659. this._mouseUpHandler = null;
  660. // 使用组件实例引用
  661. const component = window._aaComponentInstance || this;
  662. if (component) {
  663. this.$message({
  664. showClose: true,
  665. message: '图片已移动',
  666. type: 'success'
  667. });
  668. }
  669. } else {
  670. console.log('指示器已不在DOM中');
  671. }
  672. };
  673. // 确保事件只绑定一次
  674. console.log('绑定事件监听器');
  675. document.removeEventListener('mousemove', this._moveHandler);
  676. document.removeEventListener('mouseup', this._mouseUpHandler);
  677. document.addEventListener('mousemove', this._moveHandler);
  678. document.addEventListener('mouseup', this._mouseUpHandler);
  679. },
  680. // 删除图片
  681. removeImage(imageId) {
  682. const imageIndex = this.droppedImages.findIndex(
  683. (img) => img.id === imageId
  684. );
  685. if (imageIndex === -1) return;
  686. this.droppedImages.splice(imageIndex, 1);
  687. // 删除控制菜单
  688. this.safeRemoveElement(`menu_${imageId}`);
  689. // 如果正在移动这张图片,取消移动状态
  690. if (this._currentMoveImageId === imageId) {
  691. this._cancelMove();
  692. }
  693. this.redrawCanvas();
  694. this.$message({
  695. showClose: true,
  696. message: '🗑️ 图片已删除',
  697. type: 'success'
  698. });
  699. },
  700. // 取消移动操作
  701. _cancelMove() {
  702. if (this._moveHandler) {
  703. document.removeEventListener('mousemove', this._moveHandler);
  704. }
  705. if (this._mouseUpHandler) {
  706. document.removeEventListener('mouseup', this._mouseUpHandler);
  707. }
  708. // 移除指示器
  709. if (this._currentMoveImageId) {
  710. const indicator = document.getElementById(
  711. `move_indicator_${this._currentMoveImageId}`
  712. );
  713. if (indicator && indicator.parentNode) {
  714. indicator.remove();
  715. }
  716. }
  717. // 重置状态
  718. this._isMoving = false;
  719. this._currentMoveImageId = null;
  720. this._moveHandler = null;
  721. this._mouseUpHandler = null;
  722. },
  723. // 渲染Canvas核心函数
  724. async renderToCanvas(type) {
  725. // 竞态保护:记录本次渲染序号与 Promise,供 exportToPDF 等待最新一次渲染完成
  726. const mySeq = ++this._renderSeq;
  727. this._lastRenderPromise = new Promise((resolve, reject) => {
  728. this._renderResolve = resolve;
  729. this._renderReject = reject;
  730. });
  731. const targetElement = document.getElementById('captureTarget');
  732. if (!targetElement) {
  733. console.warn('未找到目标元素');
  734. this._renderReject && this._renderReject(new Error('未找到目标元素'));
  735. return;
  736. }
  737. // 显示加载状态
  738. const canvasResultDiv = document.querySelector('.canvas-result');
  739. const originalMsg = canvasResultDiv.querySelector('p');
  740. if (originalMsg) originalMsg.style.display = 'none';
  741. // 添加加载提示
  742. let loadingMsg = document.getElementById('tempLoadingMsg');
  743. if (!loadingMsg) {
  744. loadingMsg = document.createElement('div');
  745. loadingMsg.id = 'tempLoadingMsg';
  746. loadingMsg.innerText = '⏳ 渲染中,请稍候...';
  747. loadingMsg.style.padding = '12px';
  748. loadingMsg.style.color = '#2563eb';
  749. loadingMsg.style.fontSize = '14px';
  750. canvasResultDiv.appendChild(loadingMsg);
  751. }
  752. try {
  753. // 使用 html2canvas 进行转换,scale: 2 提高清晰度
  754. const scale = 2;
  755. // captureTarget 是隐藏元素(left: -100000px),scrollWidth/Height 偶现为 0,
  756. // 用 getBoundingClientRect 兜底获取真实宽高,避免渲染出 0 尺寸 canvas
  757. const rect = targetElement.getBoundingClientRect();
  758. const targetWidth =
  759. targetElement.scrollWidth || Math.ceil(rect.width) || 1;
  760. const targetHeight =
  761. targetElement.scrollHeight || Math.ceil(rect.height) || 1;
  762. const canvas = await html2canvas(targetElement, {
  763. scale: scale, // 2倍分辨率,提高清晰度
  764. backgroundColor: '#ffffff',
  765. useCORS: true, // 尝试跨域图片
  766. logging: false,
  767. allowTaint: false,
  768. imageTimeout: 5000,
  769. windowWidth: targetWidth,
  770. windowHeight: targetHeight
  771. });
  772. // 移除加载提示
  773. const temp = document.getElementById('tempLoadingMsg');
  774. if (temp) temp.remove();
  775. // 获取输出canvas元素并绘制
  776. const outputCanvas = document.getElementById('outputCanvas');
  777. outputCanvas.width = canvas.width;
  778. outputCanvas.height = canvas.height;
  779. // 使用原始尺寸(去除scale倍数),保持显示比例
  780. outputCanvas.style.width = canvas.width / scale + 'px';
  781. outputCanvas.style.height = canvas.height / scale + 'px';
  782. const ctx = outputCanvas.getContext('2d');
  783. ctx.clearRect(0, 0, outputCanvas.width, outputCanvas.height);
  784. ctx.drawImage(canvas, 0, 0);
  785. // 保存基础内容的 Canvas 数据(用于快速重绘)
  786. this.baseCanvasData = canvas;
  787. this.canvasGenerated = true;
  788. // 设置 Canvas 拖放区域和事件监听
  789. if (type == 'init') {
  790. this.setupCanvasDropZone();
  791. } else {
  792. this.redrawCanvas();
  793. }
  794. // 仅当这是最新一次渲染时才 resolve,避免旧渲染覆盖新状态
  795. if (mySeq === this._renderSeq && this._renderResolve) {
  796. this._renderResolve();
  797. }
  798. // 显示成功提示
  799. // this.showMessage('✅ 渲染成功!可下载图片', 'success');
  800. } catch (error) {
  801. console.error('html2canvas 渲染失败: ', error);
  802. const temp = document.getElementById('tempLoadingMsg');
  803. if (temp) temp.remove();
  804. this.canvasGenerated = false;
  805. // 仅当这是最新一次渲染时才 reject
  806. if (mySeq === this._renderSeq && this._renderReject) {
  807. this._renderReject(error);
  808. }
  809. // this.showMessage('⚠️ 渲染失败,请检查HTML结构或外部资源', 'error');
  810. }
  811. },
  812. // 导出为 PDF(使用 jsPDF)
  813. async exportToPDF() {
  814. // 竞态保护:如果正在渲染,等待最新一次渲染完成(最多等 15s),
  815. // 避免 html2canvas 尚未绘制完成就导出导致偶现失败
  816. if (this._lastRenderPromise) {
  817. try {
  818. await Promise.race([
  819. this._lastRenderPromise,
  820. new Promise((_, reject) =>
  821. setTimeout(() => reject(new Error('等待渲染完成超时')), 15000)
  822. )
  823. ]);
  824. } catch (err) {
  825. console.error('等待渲染完成失败,跳过导出:', err);
  826. this.$message && this.$message.error('渲染尚未完成,请稍后重试');
  827. return;
  828. }
  829. }
  830. const outputCanvas = document.getElementById('outputCanvas');
  831. if (
  832. !this.canvasGenerated ||
  833. !outputCanvas ||
  834. outputCanvas.width === 0
  835. ) {
  836. this.$message && this.$message.error('内容尚未渲染,无法导出PDF');
  837. return;
  838. }
  839. try {
  840. // 使用 Canvas 的 CSS 尺寸(显示尺寸)
  841. let cssWidth =
  842. parseFloat(outputCanvas.style.width) || outputCanvas.width;
  843. let cssHeight =
  844. parseFloat(outputCanvas.style.height) || outputCanvas.height;
  845. // 防止 css 尺寸异常(比如渲染竞态时 style 未设置),兜底用 canvas 实际像素
  846. if (!cssWidth || !cssHeight || isNaN(cssWidth) || isNaN(cssHeight)) {
  847. cssWidth = outputCanvas.width;
  848. cssHeight = outputCanvas.height;
  849. }
  850. // 计算需要的降采样比例:
  851. // jsPDF 最大页面尺寸约 5080mm,按 96dpi 换算约 19200px,留 10% 余量用 17000px
  852. const MAX_SIDE = 17000;
  853. let scale = 1;
  854. const maxSide = Math.max(outputCanvas.width, outputCanvas.height);
  855. if (maxSide > MAX_SIDE) {
  856. scale = MAX_SIDE / maxSide;
  857. }
  858. // 降采样后从临时 canvas 取图,避免超大图 toDataURL OOM / 超出 jsPDF 限制
  859. let imgData;
  860. if (scale < 1) {
  861. const tempCanvas = document.createElement('canvas');
  862. tempCanvas.width = Math.round(outputCanvas.width * scale);
  863. tempCanvas.height = Math.round(outputCanvas.height * scale);
  864. const tempCtx = tempCanvas.getContext('2d');
  865. tempCtx.drawImage(
  866. outputCanvas,
  867. 0,
  868. 0,
  869. tempCanvas.width,
  870. tempCanvas.height
  871. );
  872. imgData = tempCanvas.toDataURL('image/jpeg', 0.95);
  873. } else {
  874. imgData = outputCanvas.toDataURL('image/jpeg', 0.95);
  875. }
  876. // 创建 PDF 实例,横向或纵向根据 Canvas 尺寸决定
  877. const orientation = cssWidth > cssHeight ? 'l' : 'p';
  878. // 使用 'mm' 单位,并根据 CSS 尺寸转换为毫米(保持显示比例)
  879. // 屏幕 DPI 约 96,1px = 25.4 / 96 mm ≈ 0.2646 mm
  880. const DPI = 96;
  881. const pxToMm = 25.4 / DPI;
  882. const pdfWidth = cssWidth * pxToMm;
  883. const pdfHeight = cssHeight * pxToMm;
  884. const pdf = new jsPDF(orientation, 'mm', [pdfWidth, pdfHeight]);
  885. // 将 Canvas 图片添加到 PDF
  886. pdf.addImage(imgData, 'JPEG', 0, 0, pdfWidth, pdfHeight);
  887. // 获取 PDF 文件流并触发事件
  888. const pdfBlob = pdf.output('blob');
  889. const pdfFile = new File([pdfBlob], `canvas-${Date.now()}.pdf`, {
  890. type: 'application/pdf'
  891. });
  892. // 触发 PDF 生成事件,父组件可以监听此事件
  893. this.$emit('pdf-generated', {
  894. pdfFile,
  895. droppedImages: this.droppedImages
  896. });
  897. // 保存 PDF
  898. // const timestamp = new Date()
  899. // .toISOString()
  900. // .slice(0, 19)
  901. // .replace(/:/g, '-');
  902. // pdf.save(`canvas-${timestamp}.pdf`);
  903. // this.showMessage('✅ PDF 导出成功', 'success');
  904. } catch (err) {
  905. console.error('PDF 导出失败', err);
  906. // this.showMessage('PDF 导出失败,请稍后重试', 'error');
  907. }
  908. },
  909. // 打印 Canvas / 另存为 PDF
  910. printCanvas() {
  911. const outputCanvas = document.getElementById('outputCanvas');
  912. if (
  913. !this.canvasGenerated ||
  914. !outputCanvas ||
  915. outputCanvas.width === 0
  916. ) {
  917. // this.showMessage('请先点击"渲染至Canvas"生成图像后再打印', 'error');
  918. return;
  919. }
  920. try {
  921. // 创建新窗口用于打印
  922. const printWindow = window.open('', '_blank');
  923. const dataUrl = outputCanvas.toDataURL('image/png');
  924. printWindow.document.write(`
  925. <!DOCTYPE html>
  926. <html>
  927. <head>
  928. <title>打印图片</title>
  929. <style>
  930. body {
  931. margin: 0;
  932. padding: 20px;
  933. display: flex;
  934. justify-content: center;
  935. align-items: center;
  936. }
  937. img {
  938. max-width: 100%;
  939. height: auto;
  940. }
  941. @media print {
  942. body {
  943. padding: 0;
  944. }
  945. img {
  946. max-width: 100%;
  947. page-break-inside: avoid;
  948. }
  949. }
  950. </style>
  951. </head>
  952. <body>
  953. <img src="${dataUrl}" onload="window.print(); window.close();" />
  954. </body>
  955. </html>
  956. `);
  957. printWindow.document.close();
  958. // this.showMessage('✅ 已打开打印窗口,可选择另存为PDF', 'success');
  959. } catch (err) {
  960. console.error('打印失败', err);
  961. // this.showMessage('打印失败,请检查浏览器设置', 'error');
  962. }
  963. },
  964. init(html, data) {
  965. if (data.droppedImages) {
  966. this.droppedImages = data.droppedImages;
  967. this.editableHtml = html;
  968. this.$nextTick(() => {
  969. this.renderToCanvas();
  970. });
  971. } else {
  972. this.editableHtml = html;
  973. this.$nextTick(() => {
  974. this.renderToCanvas('init');
  975. });
  976. }
  977. }
  978. }
  979. };
  980. </script>
  981. <style scoped>
  982. * {
  983. margin: 0;
  984. padding: 0;
  985. box-sizing: border-box;
  986. }
  987. .app-container {
  988. max-width: 1400px;
  989. width: 100%;
  990. margin: 0 auto;
  991. padding: 24px 20px;
  992. min-height: 100vh;
  993. display: flex;
  994. justify-content: center;
  995. align-items: center;
  996. background: linear-gradient(145deg, #e0eafc 0%, #cfdef3 100%);
  997. }
  998. .tool-card {
  999. background: rgba(255, 255, 255, 0.92);
  1000. backdrop-filter: blur(2px);
  1001. border-radius: 2rem;
  1002. box-shadow: 0 25px 45px -12px rgba(0, 0, 0, 0.3),
  1003. 0 1px 2px rgba(0, 0, 0, 0.05);
  1004. overflow: hidden;
  1005. padding: 1.8rem 2rem 2rem 2rem;
  1006. width: 100%;
  1007. }
  1008. h1 {
  1009. font-size: 2rem;
  1010. font-weight: 700;
  1011. background: linear-gradient(135deg, #1a2a6c, #3b82f6, #8b5cf6);
  1012. background-clip: text;
  1013. -webkit-background-clip: text;
  1014. color: transparent;
  1015. letter-spacing: -0.3px;
  1016. margin-bottom: 0.35rem;
  1017. }
  1018. .sub {
  1019. color: #4a5568;
  1020. border-left: 4px solid #3b82f6;
  1021. padding-left: 14px;
  1022. margin-bottom: 1.8rem;
  1023. font-weight: 500;
  1024. font-size: 0.95rem;
  1025. }
  1026. .converter-layout {
  1027. display: flex;
  1028. justify-content: center;
  1029. }
  1030. .editor-panel {
  1031. flex: 1.2;
  1032. min-width: 280px;
  1033. background: #f8fafc;
  1034. border-radius: 1.5rem;
  1035. padding: 1.25rem;
  1036. box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  1037. border: 1px solid #e2e8f0;
  1038. }
  1039. .panel-title {
  1040. font-weight: 600;
  1041. font-size: 1.2rem;
  1042. color: #0f172a;
  1043. margin-bottom: 1rem;
  1044. display: flex;
  1045. align-items: center;
  1046. gap: 8px;
  1047. border-bottom: 2px solid #cbd5e1;
  1048. padding-bottom: 8px;
  1049. }
  1050. .html-editor {
  1051. width: 100%;
  1052. height: 360px;
  1053. background: #1e293b;
  1054. color: #e2e8f0;
  1055. font-family: 'Fira Code', 'Cascadia Code', monospace;
  1056. font-size: 13px;
  1057. line-height: 1.5;
  1058. padding: 1rem;
  1059. border-radius: 1rem;
  1060. border: none;
  1061. resize: vertical;
  1062. outline: none;
  1063. margin-bottom: 1rem;
  1064. box-shadow: inset 0 2px 5px rgba(0, 0, 0, 0.2);
  1065. }
  1066. .html-editor:focus {
  1067. border: 1px solid #3b82f6;
  1068. background: #0f172a;
  1069. }
  1070. .toolbar {
  1071. display: flex;
  1072. gap: 12px;
  1073. flex-wrap: wrap;
  1074. margin-top: 8px;
  1075. margin-bottom: 16px;
  1076. }
  1077. button {
  1078. background: white;
  1079. border: 1px solid #cbd5e1;
  1080. padding: 8px 18px;
  1081. border-radius: 40px;
  1082. font-weight: 600;
  1083. font-size: 0.85rem;
  1084. cursor: pointer;
  1085. transition: 0.2s;
  1086. color: #1e293b;
  1087. display: inline-flex;
  1088. align-items: center;
  1089. gap: 6px;
  1090. box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
  1091. }
  1092. button.primary {
  1093. background: #3b82f6;
  1094. border-color: #3b82f6;
  1095. color: white;
  1096. box-shadow: 0 4px 8px rgba(59, 130, 246, 0.3);
  1097. }
  1098. button.primary:hover {
  1099. background: #2563eb;
  1100. transform: translateY(-1px);
  1101. }
  1102. button.download {
  1103. background: #10b981;
  1104. border-color: #10b981;
  1105. color: white;
  1106. }
  1107. button.download:hover {
  1108. background: #059669;
  1109. transform: translateY(-1px);
  1110. }
  1111. button.pdf {
  1112. background: #8b5cf6;
  1113. border-color: #8b5cf6;
  1114. color: white;
  1115. }
  1116. button.pdf:hover {
  1117. background: #7c3aed;
  1118. transform: translateY(-1px);
  1119. }
  1120. button.print {
  1121. background: #8b5cf6;
  1122. border-color: #8b5cf6;
  1123. color: white;
  1124. }
  1125. button.print:hover {
  1126. background: #7c3aed;
  1127. transform: translateY(-1px);
  1128. }
  1129. .canvas-buttons {
  1130. display: flex;
  1131. gap: 12px;
  1132. justify-content: center;
  1133. margin-top: 16px;
  1134. }
  1135. button:hover {
  1136. background: #f1f5f9;
  1137. transform: translateY(-1px);
  1138. }
  1139. .info-message {
  1140. font-size: 0.8rem;
  1141. color: #2563eb;
  1142. background: #eff6ff;
  1143. padding: 6px 12px;
  1144. border-radius: 40px;
  1145. display: inline-block;
  1146. }
  1147. .preview-panel {
  1148. flex: 1;
  1149. min-width: 0;
  1150. background: #ffffff;
  1151. border-radius: 1.5rem;
  1152. padding: 1.25rem;
  1153. box-shadow: 0 8px 20px rgba(0, 0, 0, 0.08);
  1154. border: 1px solid #eef2ff;
  1155. display: flex;
  1156. flex-direction: column;
  1157. }
  1158. .images-panel {
  1159. flex: 0 0 300px;
  1160. background: #ffffff;
  1161. border-radius: 1.5rem;
  1162. padding: 1.25rem;
  1163. box-shadow: 0 8px 20px rgba(0, 0, 0, 0.08);
  1164. border: 1px solid #eef2ff;
  1165. display: flex;
  1166. flex-direction: column;
  1167. position: fixed;
  1168. right: 2rem;
  1169. top: 40px;
  1170. max-height: calc(100vh - 80px);
  1171. overflow-y: auto;
  1172. z-index: 100;
  1173. }
  1174. .seal-section {
  1175. margin-bottom: 1rem;
  1176. }
  1177. .seal-section:last-child {
  1178. margin-bottom: 0;
  1179. }
  1180. .no-data {
  1181. grid-column: 1 / -1;
  1182. text-align: center;
  1183. color: #94a3b8;
  1184. padding: 20px 0;
  1185. font-size: 14px;
  1186. }
  1187. .images-grid {
  1188. display: grid;
  1189. grid-template-columns: repeat(2, 1fr);
  1190. gap: 12px;
  1191. /* flex: 1; */
  1192. overflow-y: auto;
  1193. }
  1194. .drag-item {
  1195. background: #f8fafc;
  1196. border: 2px solid #e2e8f0;
  1197. border-radius: 12px;
  1198. padding: 8px;
  1199. cursor: move;
  1200. transition: all 0.2s;
  1201. display: flex;
  1202. flex-direction: column;
  1203. align-items: center;
  1204. height: 130px; /* 固定高度:图片80px + 内边距8px*2 + 文字区域 */
  1205. justify-content: space-between;
  1206. }
  1207. .drag-item:hover {
  1208. border-color: #3b82f6;
  1209. background: #eff6ff;
  1210. transform: translateY(-2px);
  1211. box-shadow: 0 4px 12px rgba(59, 130, 246, 0.2);
  1212. }
  1213. .drag-item img {
  1214. width: 100%;
  1215. height: 80px;
  1216. object-fit: contain; /* 改为contain,确保完整显示图片 */
  1217. border-radius: 8px;
  1218. pointer-events: none;
  1219. background-color: #f1f5f9; /* 添加背景色,防止透明图片看不清 */
  1220. }
  1221. .drag-item-name {
  1222. font-size: 12px;
  1223. color: #64748b;
  1224. margin-top: 6px;
  1225. text-align: center;
  1226. overflow: hidden;
  1227. text-overflow: ellipsis;
  1228. white-space: nowrap;
  1229. width: 100%;
  1230. line-height: 1.4;
  1231. padding: 0 4px;
  1232. max-height: 32px; /* 限制文字最大高度 */
  1233. }
  1234. .preview-header {
  1235. display: flex;
  1236. justify-content: space-between;
  1237. align-items: baseline;
  1238. margin-bottom: 1rem;
  1239. flex-wrap: wrap;
  1240. }
  1241. .badge {
  1242. background: #e6f0ff;
  1243. padding: 4px 12px;
  1244. border-radius: 30px;
  1245. font-size: 0.75rem;
  1246. font-weight: 500;
  1247. color: #1e40af;
  1248. }
  1249. .canvas-area {
  1250. background: #f1f5f9;
  1251. border-radius: 1.2rem;
  1252. padding: 1rem;
  1253. text-align: center;
  1254. min-height: 280px;
  1255. }
  1256. .target-element {
  1257. background: white;
  1258. border-radius: 1rem;
  1259. padding: 1rem;
  1260. box-shadow: 0 6px 14px rgba(0, 0, 0, 0.05);
  1261. max-width: 100%;
  1262. overflow: auto;
  1263. }
  1264. hr {
  1265. margin: 0.8rem 0;
  1266. border-color: #e2e8f0;
  1267. }
  1268. .canvas-result {
  1269. margin-top: 1rem;
  1270. background: #ffffffd9;
  1271. border-radius: 1rem;
  1272. padding: 0.8rem;
  1273. text-align: center;
  1274. }
  1275. .canvas-result p {
  1276. font-size: 0.8rem;
  1277. color: #475569;
  1278. }
  1279. canvas {
  1280. border-radius: 12px;
  1281. box-shadow: 0 6px 12px rgba(0, 0, 0, 0.1);
  1282. background: white;
  1283. }
  1284. footer {
  1285. margin-top: 1.5rem;
  1286. text-align: center;
  1287. font-size: 0.75rem;
  1288. color: #334155;
  1289. }
  1290. @media (max-width: 780px) {
  1291. .tool-card {
  1292. padding: 1rem;
  1293. }
  1294. .html-editor {
  1295. height: 260px;
  1296. }
  1297. .converter-layout {
  1298. justify-content: center;
  1299. /* flex-wrap: wrap;
  1300. padding-right: 0;
  1301. gap: 1rem; */
  1302. }
  1303. .images-panel {
  1304. position: relative;
  1305. right: auto;
  1306. top: auto;
  1307. width: 100%;
  1308. max-height: 300px; /* 在小屏幕下限制最大高度 */
  1309. order: 2; /* 在移动端将图片区域放在底部 */
  1310. margin-top: 1rem;
  1311. }
  1312. .drag-item {
  1313. height: 120px; /* 在小屏幕下稍微减小高度 */
  1314. }
  1315. .drag-item img {
  1316. height: 70px; /* 在小屏幕下减小图片高度 */
  1317. }
  1318. }
  1319. </style>