aa.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. <template>
  2. <div class="app-container">
  3. <div class="tool-card">
  4. <h1>📸 HTML → Canvas</h1>
  5. <div class="sub"
  6. >实时编辑HTML/CSS,一键渲染为高质量Canvas图像,支持下载PNG</div
  7. >
  8. <div class="converter-layout">
  9. <!-- 左侧: HTML 编辑器 -->
  10. <div class="editor-panel">
  11. <div class="panel-title"> <i>✍️</i> 编辑HTML内容 </div>
  12. <textarea
  13. v-model="editableHtml"
  14. class="html-editor"
  15. spellcheck="false"
  16. @input="handleHtmlInput"
  17. ></textarea>
  18. <div class="toolbar">
  19. <button class="primary" @click="renderToCanvas"
  20. >✨ 渲染至Canvas</button
  21. >
  22. <button @click="resetToDefault">🔄 重置示例</button>
  23. <button class="download" @click="downloadCanvasAsImage"
  24. >⬇️ 下载为PNG</button
  25. >
  26. </div>
  27. <div class="info-message">
  28. 💡
  29. 提示:支持任意HTML/CSS样式,外部图片需支持跨域或同源。复杂布局推荐使用内联样式。
  30. </div>
  31. </div>
  32. <!-- 右侧: 预览 & Canvas 结果 -->
  33. <div class="preview-panel">
  34. <div class="preview-header">
  35. <span
  36. class="panel-title"
  37. style="border: none; padding: 0; margin: 0"
  38. >🎨 原始DOM预览</span
  39. >
  40. <span class="badge">实时渲染区域</span>
  41. </div>
  42. <div class="canvas-area">
  43. <div
  44. id="captureTarget"
  45. class="target-element"
  46. v-html="previewHtml"
  47. ></div>
  48. </div>
  49. <hr />
  50. <div class="preview-header">
  51. <span
  52. class="panel-title"
  53. style="border: none; padding: 0; margin: 0"
  54. >🖼️ Canvas输出</span
  55. >
  56. <span class="badge">点击渲染后生成</span>
  57. </div>
  58. <div class="canvas-result">
  59. <p v-show="!canvasGenerated"
  60. >✨ 点击 "渲染至Canvas" 将上方内容转为图像</p
  61. >
  62. <canvas
  63. id="outputCanvas"
  64. style="max-width: 100%"
  65. v-show="canvasGenerated"
  66. ></canvas>
  67. </div>
  68. </div>
  69. </div>
  70. <footer>
  71. ⚡ 基于 html2canvas 引擎 | 精确捕获DOM节点样式,生成图片支持下载
  72. </footer>
  73. </div>
  74. </div>
  75. </template>
  76. <script>
  77. // 引入 html2canvas
  78. import html2canvas from 'html2canvas';
  79. // 默认精美的示例HTML + CSS (内联样式丰富,展示能力)
  80. const DEFAULT_HTML = `
  81. <div style="font-family: 'Segoe UI', system-ui, sans-serif; max-width: 100%; background: linear-gradient(125deg, #f9f3e6, #fff4e8); border-radius: 24px; padding: 1.2rem; box-shadow: 0 10px 20px rgba(0,0,0,0.08);">
  82. <div style="display: flex; align-items: center; gap: 14px; border-bottom: 2px dashed #ffb347; padding-bottom: 12px; margin-bottom: 16px;">
  83. <div style="background: #ff8c42; width: 48px; height: 48px; border-radius: 60px; display: flex; align-items: center; justify-content: center; color: white; font-size: 28px;">🎨</div>
  84. <div><h2 style="margin:0; color:#3b2e24;">创意工坊</h2><p style="margin:0; color:#a5673f; font-size: 12px;">HTML2Canvas 完美转换</p></div>
  85. </div>
  86. <div style="margin-bottom: 16px;">
  87. <span style="background: #ffd966; padding: 4px 12px; border-radius: 40px; font-size: 12px; font-weight: bold;">✨ 支持渐变 & 阴影</span>
  88. <span style="background: #b0d9b1; margin-left: 8px; padding: 4px 12px; border-radius: 40px; font-size: 12px;">🖌️ 复杂样式</span>
  89. </div>
  90. <div style="display: flex; gap: 16px; flex-wrap: wrap; margin: 16px 0;">
  91. <div style="background: white; border-radius: 20px; padding: 10px 14px; flex:1; box-shadow: 0 2px 6px rgba(0,0,0,0.05);"><strong>📦 卡片样式</strong><br>圆角+阴影</div>
  92. <div style="background: #eef0f5; border-radius: 20px; padding: 10px 14px; flex:1;"><strong>🌈 灵活性</strong><br>任意布局</div>
  93. </div>
  94. <div style="background: #2d3748; color: white; border-radius: 20px; padding: 12px; text-align: center; font-weight: 500;">
  95. ⚡ 可编辑HTML,实时渲染成 Canvas 图像
  96. </div>
  97. <div style="margin-top: 14px; font-size: 11px; color: #9e7e5e; text-align: center; border-top: 1px solid #ffe0b5; padding-top: 10px;">
  98. 🖱️ 点击下载按钮保存为高清PNG
  99. </div>
  100. </div>
  101. `;
  102. export default {
  103. name: 'HtmlToCanvas',
  104. data() {
  105. return {
  106. editableHtml: DEFAULT_HTML,
  107. previewHtml: DEFAULT_HTML,
  108. canvasGenerated: false
  109. };
  110. },
  111. mounted() {
  112. // 组件挂载后自动渲染一次
  113. this.$nextTick(() => {
  114. this.renderToCanvas();
  115. });
  116. },
  117. methods: {
  118. // 处理HTML输入,实时更新预览
  119. handleHtmlInput() {
  120. this.previewHtml = this.editableHtml;
  121. // 编辑后隐藏之前的canvas结果
  122. this.canvasGenerated = false;
  123. },
  124. // 渲染Canvas核心函数
  125. async renderToCanvas() {
  126. const targetElement = document.getElementById('captureTarget');
  127. if (!targetElement) {
  128. console.warn('未找到目标元素');
  129. return;
  130. }
  131. // 显示加载状态
  132. const canvasResultDiv = document.querySelector('.canvas-result');
  133. const originalMsg = canvasResultDiv.querySelector('p');
  134. if (originalMsg) originalMsg.style.display = 'none';
  135. // 添加加载提示
  136. let loadingMsg = document.getElementById('tempLoadingMsg');
  137. if (!loadingMsg) {
  138. loadingMsg = document.createElement('div');
  139. loadingMsg.id = 'tempLoadingMsg';
  140. loadingMsg.innerText = '⏳ 渲染中,请稍候...';
  141. loadingMsg.style.padding = '12px';
  142. loadingMsg.style.color = '#2563eb';
  143. loadingMsg.style.fontSize = '14px';
  144. canvasResultDiv.appendChild(loadingMsg);
  145. }
  146. try {
  147. // 使用 html2canvas 进行转换
  148. const canvas = await html2canvas(targetElement, {
  149. scale: 2, // 高清输出,2倍分辨率
  150. backgroundColor: '#ffffff',
  151. useCORS: true, // 尝试跨域图片
  152. logging: false,
  153. allowTaint: false,
  154. imageTimeout: 5000,
  155. windowWidth: targetElement.scrollWidth,
  156. windowHeight: targetElement.scrollHeight
  157. });
  158. // 移除加载提示
  159. const temp = document.getElementById('tempLoadingMsg');
  160. if (temp) temp.remove();
  161. // 获取输出canvas元素并绘制
  162. const outputCanvas = document.getElementById('outputCanvas');
  163. outputCanvas.width = canvas.width;
  164. outputCanvas.height = canvas.height;
  165. const ctx = outputCanvas.getContext('2d');
  166. ctx.clearRect(0, 0, outputCanvas.width, outputCanvas.height);
  167. ctx.drawImage(canvas, 0, 0);
  168. this.canvasGenerated = true;
  169. // 显示成功提示
  170. this.showMessage('✅ 渲染成功!可下载图片', 'success');
  171. } catch (error) {
  172. console.error('html2canvas 渲染失败: ', error);
  173. const temp = document.getElementById('tempLoadingMsg');
  174. if (temp) temp.remove();
  175. this.canvasGenerated = false;
  176. this.showMessage('❌ 渲染失败,请检查HTML结构或外部资源', 'error');
  177. }
  178. },
  179. // 下载Canvas为PNG
  180. downloadCanvasAsImage() {
  181. const outputCanvas = document.getElementById('outputCanvas');
  182. if (
  183. !this.canvasGenerated ||
  184. !outputCanvas ||
  185. outputCanvas.width === 0
  186. ) {
  187. this.showMessage('请先点击“渲染至Canvas”生成图像后再下载', 'error');
  188. return;
  189. }
  190. try {
  191. const link = document.createElement('a');
  192. const timestamp = new Date()
  193. .toISOString()
  194. .slice(0, 19)
  195. .replace(/:/g, '-');
  196. link.download = `canvas-${timestamp}.png`;
  197. link.href = outputCanvas.toDataURL('image/png');
  198. link.click();
  199. this.showMessage('✅ 下载已开始', 'success');
  200. } catch (err) {
  201. console.error('下载失败', err);
  202. this.showMessage(
  203. '无法下载图像,可能是跨域限制或Canvas被污染',
  204. 'error'
  205. );
  206. }
  207. },
  208. // 重置为默认示例
  209. resetToDefault() {
  210. this.editableHtml = DEFAULT_HTML;
  211. this.previewHtml = DEFAULT_HTML;
  212. this.canvasGenerated = false;
  213. this.$nextTick(() => {
  214. this.renderToCanvas();
  215. });
  216. },
  217. init(html) {
  218. this.editableHtml = html;
  219. this.previewHtml = html;
  220. },
  221. // 显示提示消息
  222. showMessage(msg, type) {
  223. const canvasResultDiv = document.querySelector('.canvas-result');
  224. const existingMsg = canvasResultDiv.querySelector('.temp-message');
  225. if (existingMsg) existingMsg.remove();
  226. const messageDiv = document.createElement('div');
  227. messageDiv.className = `temp-message message-${type}`;
  228. messageDiv.innerText = msg;
  229. messageDiv.style.fontSize = '12px';
  230. messageDiv.style.marginTop = '8px';
  231. messageDiv.style.padding = '4px 8px';
  232. messageDiv.style.borderRadius = '20px';
  233. messageDiv.style.backgroundColor =
  234. type === 'success' ? '#d1fae5' : '#fee2e2';
  235. messageDiv.style.color = type === 'success' ? '#065f46' : '#991b1b';
  236. canvasResultDiv.appendChild(messageDiv);
  237. setTimeout(() => {
  238. if (messageDiv) messageDiv.remove();
  239. }, 2500);
  240. }
  241. }
  242. };
  243. </script>
  244. <style scoped>
  245. * {
  246. margin: 0;
  247. padding: 0;
  248. box-sizing: border-box;
  249. }
  250. .app-container {
  251. max-width: 1400px;
  252. width: 100%;
  253. margin: 0 auto;
  254. padding: 24px 20px;
  255. min-height: 100vh;
  256. display: flex;
  257. justify-content: center;
  258. align-items: center;
  259. background: linear-gradient(145deg, #e0eafc 0%, #cfdef3 100%);
  260. }
  261. .tool-card {
  262. background: rgba(255, 255, 255, 0.92);
  263. backdrop-filter: blur(2px);
  264. border-radius: 2rem;
  265. box-shadow: 0 25px 45px -12px rgba(0, 0, 0, 0.3),
  266. 0 1px 2px rgba(0, 0, 0, 0.05);
  267. overflow: hidden;
  268. padding: 1.8rem 2rem 2rem 2rem;
  269. width: 100%;
  270. }
  271. h1 {
  272. font-size: 2rem;
  273. font-weight: 700;
  274. background: linear-gradient(135deg, #1a2a6c, #b21f1f, #fdbb4d);
  275. background-clip: text;
  276. -webkit-background-clip: text;
  277. color: transparent;
  278. letter-spacing: -0.3px;
  279. margin-bottom: 0.35rem;
  280. }
  281. .sub {
  282. color: #4a5568;
  283. border-left: 4px solid #3b82f6;
  284. padding-left: 14px;
  285. margin-bottom: 1.8rem;
  286. font-weight: 500;
  287. font-size: 0.95rem;
  288. }
  289. .converter-layout {
  290. display: flex;
  291. flex-wrap: wrap;
  292. gap: 2rem;
  293. margin-top: 0.5rem;
  294. }
  295. .editor-panel {
  296. flex: 1.2;
  297. min-width: 280px;
  298. background: #f8fafc;
  299. border-radius: 1.5rem;
  300. padding: 1.25rem;
  301. box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  302. border: 1px solid #e2e8f0;
  303. }
  304. .panel-title {
  305. font-weight: 600;
  306. font-size: 1.2rem;
  307. color: #0f172a;
  308. margin-bottom: 1rem;
  309. display: flex;
  310. align-items: center;
  311. gap: 8px;
  312. border-bottom: 2px solid #cbd5e1;
  313. padding-bottom: 8px;
  314. }
  315. .html-editor {
  316. width: 100%;
  317. height: 360px;
  318. background: #1e293b;
  319. color: #e2e8f0;
  320. font-family: 'Fira Code', 'Cascadia Code', monospace;
  321. font-size: 13px;
  322. line-height: 1.5;
  323. padding: 1rem;
  324. border-radius: 1rem;
  325. border: none;
  326. resize: vertical;
  327. outline: none;
  328. margin-bottom: 1rem;
  329. box-shadow: inset 0 2px 5px rgba(0, 0, 0, 0.2);
  330. }
  331. .html-editor:focus {
  332. border: 1px solid #3b82f6;
  333. background: #0f172a;
  334. }
  335. .toolbar {
  336. display: flex;
  337. gap: 12px;
  338. flex-wrap: wrap;
  339. margin-top: 8px;
  340. margin-bottom: 16px;
  341. }
  342. button {
  343. background: white;
  344. border: 1px solid #cbd5e1;
  345. padding: 8px 18px;
  346. border-radius: 40px;
  347. font-weight: 600;
  348. font-size: 0.85rem;
  349. cursor: pointer;
  350. transition: 0.2s;
  351. color: #1e293b;
  352. display: inline-flex;
  353. align-items: center;
  354. gap: 6px;
  355. box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
  356. }
  357. button.primary {
  358. background: #3b82f6;
  359. border-color: #3b82f6;
  360. color: white;
  361. box-shadow: 0 4px 8px rgba(59, 130, 246, 0.3);
  362. }
  363. button.primary:hover {
  364. background: #2563eb;
  365. transform: translateY(-1px);
  366. }
  367. button.download {
  368. background: #10b981;
  369. border-color: #10b981;
  370. color: white;
  371. }
  372. button.download:hover {
  373. background: #059669;
  374. transform: translateY(-1px);
  375. }
  376. button:hover {
  377. background: #f1f5f9;
  378. transform: translateY(-1px);
  379. }
  380. .info-message {
  381. font-size: 0.8rem;
  382. color: #2563eb;
  383. background: #eff6ff;
  384. padding: 6px 12px;
  385. border-radius: 40px;
  386. display: inline-block;
  387. }
  388. .preview-panel {
  389. flex: 1;
  390. min-width: 320px;
  391. background: #ffffff;
  392. border-radius: 1.5rem;
  393. padding: 1.25rem;
  394. box-shadow: 0 8px 20px rgba(0, 0, 0, 0.08);
  395. border: 1px solid #eef2ff;
  396. display: flex;
  397. flex-direction: column;
  398. }
  399. .preview-header {
  400. display: flex;
  401. justify-content: space-between;
  402. align-items: baseline;
  403. margin-bottom: 1rem;
  404. flex-wrap: wrap;
  405. }
  406. .badge {
  407. background: #e6f0ff;
  408. padding: 4px 12px;
  409. border-radius: 30px;
  410. font-size: 0.75rem;
  411. font-weight: 500;
  412. color: #1e40af;
  413. }
  414. .canvas-area {
  415. background: #f1f5f9;
  416. border-radius: 1.2rem;
  417. padding: 1rem;
  418. text-align: center;
  419. min-height: 280px;
  420. }
  421. .target-element {
  422. background: white;
  423. border-radius: 1rem;
  424. padding: 1rem;
  425. box-shadow: 0 6px 14px rgba(0, 0, 0, 0.05);
  426. max-width: 100%;
  427. overflow: auto;
  428. }
  429. hr {
  430. margin: 0.8rem 0;
  431. border-color: #e2e8f0;
  432. }
  433. .canvas-result {
  434. margin-top: 1rem;
  435. background: #ffffffd9;
  436. border-radius: 1rem;
  437. padding: 0.8rem;
  438. text-align: center;
  439. }
  440. .canvas-result p {
  441. font-size: 0.8rem;
  442. color: #475569;
  443. }
  444. canvas {
  445. border-radius: 12px;
  446. box-shadow: 0 6px 12px rgba(0, 0, 0, 0.1);
  447. background: white;
  448. }
  449. footer {
  450. margin-top: 1.5rem;
  451. text-align: center;
  452. font-size: 0.75rem;
  453. color: #334155;
  454. }
  455. @media (max-width: 780px) {
  456. .tool-card {
  457. padding: 1rem;
  458. }
  459. .html-editor {
  460. height: 260px;
  461. }
  462. }
  463. </style>