Showing
7 changed files
with
117 additions
and
3 deletions
package-lock.json
0 → 100644
This diff could not be displayed because it is too large.
| ... | @@ -9,6 +9,7 @@ | ... | @@ -9,6 +9,7 @@ |
| 9 | "serve": "vite preview" | 9 | "serve": "vite preview" |
| 10 | }, | 10 | }, |
| 11 | "dependencies": { | 11 | "dependencies": { |
| 12 | + "html2canvas": "^1.4.1", | ||
| 12 | "jquery": "^3.6.0", | 13 | "jquery": "^3.6.0", |
| 13 | "lodash": "^4.17.21", | 14 | "lodash": "^4.17.21", |
| 14 | "moment": "^2.29.1", | 15 | "moment": "^2.29.1", | ... | ... |
src/assets/images/logo@3x.png
0 → 100644
108 KB
| ... | @@ -6,4 +6,11 @@ export default [{ | ... | @@ -6,4 +6,11 @@ export default [{ |
| 6 | title: '' | 6 | title: '' |
| 7 | }, | 7 | }, |
| 8 | children: [] | 8 | children: [] |
| 9 | +}, { | ||
| 10 | + path: '/image', | ||
| 11 | + name: 'html转图片', | ||
| 12 | + component: () => import('./views/html2canvas.vue'), | ||
| 13 | + meta: { | ||
| 14 | + title: '' | ||
| 15 | + } | ||
| 9 | }]; | 16 | }]; |
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
src/views/html2canvas.vue
0 → 100644
| 1 | +<template> | ||
| 2 | + <div> | ||
| 3 | + <!-- 生成图片后隐藏原始结构 --> | ||
| 4 | + <div v-if="flag" ref="canvasRef" style="width: 200px; height: 200px;"> | ||
| 5 | + <img :src="logo_image" alt="" /> | ||
| 6 | + </div> | ||
| 7 | + <div v-if="imgUrl"> | ||
| 8 | + <img :src="imgUrl" alt="" crossOrigin="anonymous" style="width: 300px; height: 300px;"/> | ||
| 9 | + </div> | ||
| 10 | + <div> | ||
| 11 | + <div @click="createImage">合成图片</div> | ||
| 12 | + </div> | ||
| 13 | + </div> | ||
| 14 | +</template> | ||
| 15 | + | ||
| 16 | +<script setup> | ||
| 17 | + | ||
| 18 | +import logo_image from '@images/logo@3x.png' | ||
| 19 | +</script> | ||
| 20 | + | ||
| 21 | +<script> | ||
| 22 | +import mixin from 'common/mixin'; | ||
| 23 | +import html2canvas from "html2canvas"; | ||
| 24 | + | ||
| 25 | +export default { | ||
| 26 | + mixins: [mixin.init], | ||
| 27 | + data() { | ||
| 28 | + return { | ||
| 29 | + imgUrl: "", | ||
| 30 | + flag: true | ||
| 31 | + } | ||
| 32 | + }, | ||
| 33 | + mounted() { | ||
| 34 | + | ||
| 35 | + }, | ||
| 36 | + methods: { | ||
| 37 | + createImage() { | ||
| 38 | + this.$nextTick(() => { | ||
| 39 | + // 获取要生成图片的 DOM 元素 | ||
| 40 | + let canvasDom = this.$refs.canvasRef; | ||
| 41 | + const options = { | ||
| 42 | + // backgroundColor: '#ffffff', | ||
| 43 | + // canvas: canvas, | ||
| 44 | + useCORS: true,//配置允许跨域 | ||
| 45 | + // scale:1, | ||
| 46 | + // windowWidth: document.body.scrollWidth, | ||
| 47 | + // windowHeight: document.body.scrollHeight, | ||
| 48 | + // x: 0, | ||
| 49 | + // y: window.pageYOffset, | ||
| 50 | + // allowTaint: true, | ||
| 51 | + // background: "#ffffff", // 一定要添加背景颜色,否则出来的图片,背景全部都是透明的 | ||
| 52 | + dpi: 300 // 处理模糊问题 | ||
| 53 | + }; | ||
| 54 | + // console.log("获取指定的宽高", width, height, canvas); | ||
| 55 | + html2canvas(canvasDom, options) | ||
| 56 | + .then(canvas => { | ||
| 57 | + try { | ||
| 58 | + // 生成图片地址 | ||
| 59 | + this.imgUrl = canvas.toDataURL("image/png"); | ||
| 60 | + // console.log("canvas.toDataURL('image/png')", this.imgUrl); | ||
| 61 | + this.flag = false; | ||
| 62 | + } catch (e) { | ||
| 63 | + alert("图片跨域,保存失败"); | ||
| 64 | + } | ||
| 65 | + }) | ||
| 66 | + .catch(err => { | ||
| 67 | + console.log("绘制失败"); | ||
| 68 | + }); | ||
| 69 | + }); | ||
| 70 | + } | ||
| 71 | + } | ||
| 72 | +} | ||
| 73 | +</script> | ||
| 74 | + | ||
| 75 | +<style lang="less" scoped> | ||
| 76 | +.m-html2canvas { | ||
| 77 | + display: flex; | ||
| 78 | +} | ||
| 79 | + | ||
| 80 | +.m-canvasRef { | ||
| 81 | + width: 500px; | ||
| 82 | + height: 500px; | ||
| 83 | + /* border: 1px solid red; */ | ||
| 84 | + /* box-sizing: border-box; */ | ||
| 85 | + box-shadow: 10px 10px 5px #888888; | ||
| 86 | +} | ||
| 87 | + | ||
| 88 | +.m-imageList { | ||
| 89 | + width: 500px; | ||
| 90 | + height: calc(500px / 3); | ||
| 91 | +} | ||
| 92 | + | ||
| 93 | +img { | ||
| 94 | + width: 100%; | ||
| 95 | + height: 100%; | ||
| 96 | + float: left; | ||
| 97 | +} | ||
| 98 | + | ||
| 99 | +.m-showImage { | ||
| 100 | + margin-left: 50px; | ||
| 101 | + background: red; | ||
| 102 | + width: 500px; | ||
| 103 | + height: 500px; | ||
| 104 | +} | ||
| 105 | + | ||
| 106 | +.m-btn { | ||
| 107 | + margin-left: 50px; | ||
| 108 | +} | ||
| 109 | +</style> | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
| ... | @@ -10,16 +10,13 @@ | ... | @@ -10,16 +10,13 @@ |
| 10 | import mixin from 'common/mixin' | 10 | import mixin from 'common/mixin' |
| 11 | import { mainStore } from '@/store' | 11 | import { mainStore } from '@/store' |
| 12 | import { storeToRefs } from 'pinia' | 12 | import { storeToRefs } from 'pinia' |
| 13 | - | ||
| 14 | export default { | 13 | export default { |
| 15 | mixins: [mixin.init], | 14 | mixins: [mixin.init], |
| 16 | data () { | 15 | data () { |
| 17 | return { | 16 | return { |
| 18 | - | ||
| 19 | } | 17 | } |
| 20 | }, | 18 | }, |
| 21 | mounted () { | 19 | mounted () { |
| 22 | - | ||
| 23 | }, | 20 | }, |
| 24 | methods: { | 21 | methods: { |
| 25 | 22 | ... | ... |
This diff is collapsed. Click to expand it.
-
Please register or login to post a comment