Commit 542134c9 authored by 毛线's avatar 毛线

改回div渲染

parent 78a47ab4
...@@ -99,11 +99,22 @@ export default { ...@@ -99,11 +99,22 @@ export default {
} }
} }
if (data.bg) {
const card = new Card({
x: 0,
y: 0,
width: data.w,
height: data.h,
bgImg: data.bg,
})
card.draw(ctx)
}
if (child) { if (child) {
const drawList = [...child] const drawList = [...child]
drawList.sort(handle('z')) drawList.sort(handle('z'))
drawList.forEach(item => { drawList.forEach(item => {
const { x, y, w, h, bg, deg } = item const { x, y, w, h, bg, deg, t } = item
const card = new Card({ const card = new Card({
x, x,
y, y,
...@@ -111,6 +122,7 @@ export default { ...@@ -111,6 +122,7 @@ export default {
height: h, height: h,
bgImg: bg, bgImg: bg,
deg, deg,
type: t,
}) })
card.draw(ctx) card.draw(ctx)
}) })
...@@ -126,17 +138,34 @@ export default { ...@@ -126,17 +138,34 @@ export default {
} }
return data return data
}, },
// 导出图片
getImage() {
const canvas = this.$refs.canvas
const imageData = canvas.toDataURL('image/png') // 返回base64的URL
const elink = document.createElement('a')
elink.download = '图片'
elink.style.display = 'none'
elink.href = imageData
document.body.appendChild(elink)
elink.click()
URL.revokeObjectURL(elink.href) // 释放URL对象
// this.$Screenshot(document.querySelector('#wrapper')).then(canvas => {
// this.downFile(canvas.toDataURL('image/jpeg', 0.8), new Date().getTime())
// this.$message.success('图片导出成功')
// })
},
}, },
} }
function Card(params) { function Card(params) {
const { x, y, width, height, bgImg, deg } = params const { x, y, width, height, bgImg, deg, type } = params
console.log('params', params)
this.x = x // x 坐标 this.x = x // x 坐标
this.y = y // y 坐标 this.y = y // y 坐标
this.width = width // 宽度 this.width = width // 宽度
this.height = height // 高度 this.height = height // 高度
this.bgImg = bgImg // 图片路径 this.bgImg = bgImg // 图片路径
this.type = type // 类型,i:图片, p:手机, sp:侧面手机, va:可视区域
// this.draw = function(ctx) { // this.draw = function(ctx) {
// ctx.fillStyle = 'red' // ctx.fillStyle = 'red'
...@@ -152,6 +181,8 @@ function Card(params) { ...@@ -152,6 +181,8 @@ function Card(params) {
this.draw = function(ctx) { this.draw = function(ctx) {
const img = new Image() const img = new Image()
// img.setAttribute('crossOrigin', 'anonymous') // 设置跨域属性
img.crossOrigin = 'anonymous'
img.src = this.bgImg img.src = this.bgImg
img.onload = () => { img.onload = () => {
...@@ -172,11 +203,17 @@ function Card(params) { ...@@ -172,11 +203,17 @@ function Card(params) {
ctx.translate(centerX, centerY) ctx.translate(centerX, centerY)
ctx.rotate(Math.PI * (deg / 180)) ctx.rotate(Math.PI * (deg / 180))
ctx.translate(-centerX, -centerY) ctx.translate(-centerX, -centerY)
if (this.type === 'va') {
// ctx.globalCompositeOperation = 'destination-in'
} else {
ctx.globalCompositeOperation = 'source-over'
}
ctx.drawImage(img, 0, 0, img.width, img.height, x, y, width, height) ctx.drawImage(img, 0, 0, img.width, img.height, x, y, width, height)
ctx.translate(centerX, centerY) ctx.translate(centerX, centerY)
ctx.rotate(Math.PI * (-deg / 180)) ctx.rotate(Math.PI * (-deg / 180))
ctx.translate(-centerX, -centerY) ctx.translate(-centerX, -centerY)
console.log('渲染图片-') console.log('渲染图片', params)
} }
} }
} }
......
...@@ -123,8 +123,8 @@ export default { ...@@ -123,8 +123,8 @@ export default {
move(event) { move(event) {
const { data } = this const { data } = this
const { x, y, } = data const { x, y, } = data
const dataX = x const dataX = x * 1
const dataY = y const dataY = y * 1
const odiv = event.target // 获取目标元素 const odiv = event.target // 获取目标元素
const diff = -4 // diff是card组件,操作区域距离图层的距离 const diff = -4 // diff是card组件,操作区域距离图层的距离
......
...@@ -17,8 +17,8 @@ ...@@ -17,8 +17,8 @@
@test="(point) => $emit('test', point)" @test="(point) => $emit('test', point)"
/> />
<div style="width: 100%; height: 100%;"> <div style="width: 100%; height: 100%;">
<!-- <span v-if="!data.bg">{{ data.n }}</span> <span v-if="!data.bg">{{ data.n }}</span>
<div v-if="data.bg" :style="`background-image:url(${data.bg})`" alt="" class="bg"/> --> <div v-if="data.bg" :style="`background-image:url(${data.bg})`" alt="" class="bg"/>
</div> </div>
</div> </div>
</template> </template>
......
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
<!-- 测试div --> <!-- 测试div -->
<div :style="testStyle" class="testPoint"/> <div :style="testStyle" class="testPoint"/>
</div> </div>
<Canvas ref="canvas" :value="data" class="canvas" /> <!-- <Canvas ref="canvas" :value="data" class="canvas" /> -->
</div> </div>
</template> </template>
...@@ -139,20 +139,21 @@ export default { ...@@ -139,20 +139,21 @@ export default {
} }
this.$emit('on-change', { data: this.data, index }) this.$emit('on-change', { data: this.data, index })
this.$nextTick(() => { this.$nextTick(() => {
this.$refs.canvas.draw() this.$refs.canvas?.draw()
}) })
}, },
deleteCard(index) { deleteCard(index) {
this.data.child.splice(index, 1) this.data.child.splice(index, 1)
this.$emit('on-change', { data: this.data, index: '' }) this.$emit('on-change', { data: this.data, index: '' })
this.$refs.canvas.draw() this.$refs.canvas?.draw()
}, },
// 导出图片 // 导出图片
exportImg() { getImage() {
// this.$Screenshot(document.querySelector('#wrapper')).then(canvas => { // this.$Screenshot(document.querySelector('#wrapper')).then(canvas => {
// this.downFile(canvas.toDataURL('image/jpeg', 0.8), new Date().getTime()); // this.downFile(canvas.toDataURL('image/jpeg', 0.8), new Date().getTime())
// this.$message.success('图片导出成功') // this.$message.success('图片导出成功')
// }) // })
this.$refs.canvas?.getImage()
}, },
// 定位到某个图层 // 定位到某个图层
selectIndex(index) { selectIndex(index) {
...@@ -170,7 +171,7 @@ export default { ...@@ -170,7 +171,7 @@ export default {
onChangeCard: () => { onChangeCard: () => {
const { activeIndex, data } = this const { activeIndex, data } = this
this.$emit('on-change', { data, index: activeIndex }) // 选中的索引切换的时候,触发回调 this.$emit('on-change', { data, index: activeIndex }) // 选中的索引切换的时候,触发回调
this.$refs.canvas.draw() this.$refs.canvas?.draw()
}, },
test: (point) => { test: (point) => {
this.testPoint = point this.testPoint = point
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment