<template>
<view class="container">
<view class="" v-for="(item, index) in compositeTypes" :key="index"> <gcanvas :ref="gcanvas${index}
" class="canvas"></gcanvas>
<view class="">
{{item}}
</view></view>
<view class="btn"> <view class="btn-text" @click="handleDraw">开始动画</view> </view>
</view>
</template>
<script>
import { enable, WeexBridge } from '../../gcanvas/index.js'
module.exports = {
data: {
interval: 0,
compositeTypes: [
'source-over',
'source-in',
'source-out',
'source-atop',
'destination-over',
'destination-in',
'destination-out',
'destination-atop',
'lighter',
'darker',
'copy',
'xor',
],
},
methods: {
handleDraw() {
for (var i = 0; i < this.compositeTypes.length; i++) {
this.test(i, this.compositeTypes[i])
}
},
test(i, name) {
var ganvas = this.$refs['gcanvas' + i][0]
/通过元素引用获取canvas对象/
this.canvasObj = enable(ganvas, {
bridge: WeexBridge,
})
//var ctx = gcanvas.getContext("2d");
/获取绘图所需的上下文,目前不支持3d/
const context = this.canvasObj.getContext('2d')
var blue = 'rgba(0,0,255,0.5)'
var red = 'rgba(255,0,0,0.5)'
context.fillStyle = blue
context.fillRect(15, 15, 70, 70)
// set composite property
context.globalCompositeOperation = name
// draw circle
context.fillStyle = red
context.beginPath()
context.arc(75, 75, 35, 0, Math.PI * 2, true)
context.fill()
context.draw()
},
},
}
</script>
<style>
.canvas {
width: 605upx;
height: 400upx;
}
.btn-text:active {
background-color: #0f0;
}
.btn-text {
padding-top: 20px;
padding-bottom: 20px;
margin-left: 20px;
margin-right: 20px;
flex-direction: row;
background-color: #ff9537;
border-radius: 10px;
width: 335px;
text-align: center;
color: #fff;
}
/ page /
.container {
flex-direction: column;
background-color: #ff9537;
}
</style>
1 个回复
momoca (作者)
事例代码:
1.globalCompositeOperation没生效
2.控制台报错