配套 vue 使用
<template>
<div class="demo1content">
<canvas id="canvas1"></canvas>
<button @click="changecolor({name:'body',color:'#FF5A33'})">
改变身体颜色
</button>
<button @click="changecolor({name:'left',color:'#0078D7'})">
改变左袖子颜色
</button>
<button @click="changecolor({name:'topbody',color:'#C76F10'})">
改变上衣紫色
</button>
</div>
</template>
<script>
export default {
data() {
return {
message: '测试5',
canvasobj: {
main_context: null,
width: 600,
height: 400,
change_context: null,
canvasall: null,
},
basicimg: require('@/assets/imgs/base_product.png'),
changedata: [
{
name: 'body',
color: '#81D640',
imgsrc: require('@/assets/imgs/body_color.png'),
},
{
name: 'left',
color: '#C90000',
imgsrc: require('@/assets/imgs/left_sleeve_color.png'),
},
{
name: 'topbody',
color: '#FFE793',
imgsrc: require('@/assets/imgs/shoulder_color.png'),
},
],
basic_content: '',
change_content: [],
}
},
mounted() {
this.imginit()
},
methods: {
imginit() {
this.canvasobj.canvasall = document.querySelector('#canvas1')
this.canvasobj.canvasall.width = this.canvasobj.width
this.canvasobj.canvasall.height = this.canvasobj.height
this.canvasobj.main_context = this.canvasobj.canvasall.getContext('2d')
this.canvasobj.change_context = this.canvasobj.canvasall
.cloneNode()
.getContext('2d')
this.loadImages()
},
loadImages() {
this.canvasobj.main_context.clearRect(
0,
0,
this.canvasobj.width,
this.canvasobj.height
)
const mainLoaded = this.createimg(this.basicimg).then((img) => {
this.basic_content = img
})
let changeall = []
let changealled = this.changedata.map((content) => {
return this.createimg(content.imgsrc).then((img) => {
changeall.push(img)
})
})
Promise.all([mainLoaded, ...changealled])
.then(() => {
this.draw(changeall)
})
.catch((e) => {
window.alert('出错误了,请查看图片地址')
})
},
createimg(url) {
return new Promise((resolve, reject) => {
const img = new Image()
img.crossOrigin = 'anonymous'
img.src = url
img.onload = () => resolve(img)
img.onerror = () => reject()
})
},
draw(changeall) {
this.canvasobj.main_context.drawImage(
this.basic_content,
0,
0,
this.canvasobj.width,
this.canvasobj.height
)
this.canvasobj.main_context.globalCompositeOperation = 'hard-light'
this.drawchange(changeall)
console.log(changeall)
const previousOpacity = this.canvasobj.main_context.globalAlpha
this.canvasobj.main_context.globalAlpha = 0.75
this.canvasobj.main_context.drawImage(
this.basic_content,
0,
0,
this.canvasobj.width,
this.canvasobj.height
)
this.canvasobj.main_context.globalAlpha = previousOpacity
},
drawchange(changeall) {
console.log(changeall)
for (let i = 0; i < changeall.length; i++) {
this.canvasobj.change_context.globalCompositeOperation = 'hard-light'
this.canvasobj.change_context.clearRect(
0,
0,
this.canvasobj.width,
this.canvasobj.height
)
this.canvasobj.change_context.drawImage(
changeall[i],
0,
0,
this.canvasobj.width,
this.canvasobj.height
)
this.canvasobj.change_context.globalCompositeOperation = 'source-atop'
this.canvasobj.change_context.fillStyle = this.changedata[i].color
this.canvasobj.change_context.fillRect(
0,
0,
this.canvasobj.width,
this.canvasobj.height
)
this.canvasobj.main_context.drawImage(
this.canvasobj.change_context.canvas,
0,
0,
this.canvasobj.width,
this.canvasobj.height
)
}
},
changecolor(color) {
for (let i = 0; i < this.changedata.length; i++) {
if (color.name == this.changedata[i].name) {
this.changedata[i].color = color.color
this.imginit()
}
}
},
},
}
</script>
<style lang="less" scoped>
.demo1content {
width: 100%;
height: 100%;
}
</style>