drawCanvas.vue
6.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<template>
<div>
<!-- <el-button v-show="isIcon" style="margin-left: 15px;margin-right: 50px;" class="el-button manage-add-btn el-button--primary" @click="confirmCapture">保存</el-button> -->
<el-button v-show="isIcon && !isCancel" style="margin-left: 15px;margin-right: 50px;" class="el-button manage-add-btn el-button--primary" @click="cancelCapture">{{$t('dialog.cancel')}}</el-button>
<el-button v-show="isIcon" style="margin-left: 15px;" class="el-button manage-add-btn el-button--primary" @click="clearCapture">{{$t('button.emptyCanvas')}}</el-button>
<!-- <el-button v-show="!isIcon" style="margin-left: 15px;margin-right: 50px;" id="capture" class="el-button manage-add-btn el-button--primary" @click="capture">编辑</el-button> -->
<canvas ref="canvas"></canvas>
</div>
</template>
<script>
export default{
data(){
return{
isCancel:false,
picUrl:'',
isIcon:false,
captureCanvas:'',
locHistory:[],
isMouseDown:false,
endTime:new Date().getTime(),
lastTimestamp:0,
lastLineWidth:-1,
lastLoc : {
x: 0,
y: 0
},
isRemote:'',
width:'',
height:''
}
},
methods:{
createCanvas(pic,width,height,type,isDeal){
// console.log(pic)
this.isCancel = width?false:true;
this.picUrl= pic
this.width = width
this.height = height
this.isRemote = type
let that = this
let image = new Image()
that.$api.videoShopTourReport.getBase64({path: that.picUrl}).then(data => {
let result = data.data;
if(result.code == '200'){
image.src = 'data:image/png;base64,'+result.data
image.style.width = width?width+'px':'770px'
image.style.height = height?height+'px':'500px'
image.crossOrigin = 'Anonymous'
image.onload = function(){
that.captureCanvas = that.$refs.canvas;
that.captureCanvas.width = width?width:'770'
that.captureCanvas.height = height?height:'500'
let ctx = that.captureCanvas.getContext("2d");
ctx.drawImage(image, 0, 0,that.captureCanvas.width,that.captureCanvas.height);
// let pattern=ctx.createPattern(image,'no-repeat')
// ctx.fillStyle=pattern;
// if(!isDeal){
// that.$emit('getBase64Str',that.captureCanvas)
// }
that.capture()
}
}
});
},
cancelCapture(){
this.isIcon = false;
this.$emit('getIsRemote',this.isRemote)
},
confirmCapture(){
this.$emit('getBase64Str',this.captureCanvas)
// if(this.isRemote){
// this.$emit('getIsRemote',this.isRemote)
// }
// this.closeCapture()
},
clearCapture(){
let context = this.captureCanvas.getContext("2d")
context.clearRect(0, 0, this.captureCanvas.width, this.captureCanvas.height)
this.createCanvas(this.picUrl,this.width,this.height)
},
closeCapture(){
this.isIcon = false;
},
capture(){
this.isIcon = true;
// 绘制
var context = this.captureCanvas.getContext("2d");
let _this = this
this.captureCanvas.onmousedown = function(e) {
if(!_this.isIcon){
return
}
e.preventDefault();
_this.beginStroke({
x: e.clientX,
y: e.clientY
})
}
this.captureCanvas.onmouseup = function(e) {
e.preventDefault();
_this.endStroke()
}
this.captureCanvas.onmouseout = function(e) {
e.preventDefault();
_this.endStroke()
}
this.captureCanvas.onmousemove = function(e) {
e.preventDefault();
if (_this.isMouseDown) {
_this.moveStroke({
x: e.clientX,
y: e.clientY
},context)
}
}
},
beginStroke(point) {
this.isMouseDown = true
this.lastTimestamp = new Date().getTime()
this.lastLoc = this.windowToCanvas(point.x, point.y)
this.locHistory.push({
x: this.lastLoc.x,
y: this.lastLoc.y,
width: 0,
t: this.lastTimestamp - this.endTime
})
},
endStroke() {
this.locHistory.push({
x: this.lastLoc.x,
y: this.lastLoc.y,
width: 0,
t: 0
})
this.isMouseDown = false;
this.endTime = new Date().getTime()
},
moveStroke(point,context) {
// console.log("mouse move")
var curLoc = this.windowToCanvas(point.x, point.y)
var curTimestamp = new Date().getTime();
var s = this.calcDistance(curLoc, this.lastLoc)
var t = curTimestamp - this.lastTimestamp;
var lineWidth = 3
//draw
context.beginPath()
context.moveTo(this.lastLoc.x, this.lastLoc.y)
context.lineTo(curLoc.x, curLoc.y)
this.locHistory.push({
x: curLoc.x,
y: curLoc.y,
with: lineWidth,
t: t
})
context.lineWidth = lineWidth
context.strokeStyle = '#FF0000';
context.lineCap = "round"
context.linJoin = "round"
context.stroke();
//每次过程结束时,将结束值赋给初始值,一直延续
this.lastLoc = curLoc
this.lastTimestamp = curTimestamp
this.lastLineWidth = lineWidth
},
calcDistance(loc1, loc2) {
return Math.sqrt((loc1.x - loc2.x) * (loc1.x - loc2.x) + (loc1.y - loc2.y) * (loc1.y - loc2
.y)) //通过起始结束坐标x,y值计算路程长度
},
windowToCanvas(x, y) {
var bbox = this.captureCanvas.getBoundingClientRect(); //获取canvas的位置信息
return {
x: Math.round(x - bbox.left),
y: Math.round(y - bbox.top)
} //返回当前鼠标相对于canvas的位置
},
}
}
</script>
<style>
</style>