-
Notifications
You must be signed in to change notification settings - Fork 169
/
Canvas.vue
136 lines (129 loc) · 2.99 KB
/
Canvas.vue
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
<template>
<div id="canvasBox" :style="getHorizontalStyle">
<div class="greet">
<span>{{msg}}</span>
<a @touchstart="clear" @mousedown="clear">清屏</a>
<a @touchstart="download" @mousedown="download">下载</a>
<select v-model="degree">
<option v-for="item in scope" :value="item.value">{{item.title}}</option>
</select>
</div>
<canvas></canvas>
</div>
</template>
<script>
import Draw from '../utils/draw';
export default {
name: 'canvas',
data() {
return {
msg: 'Just use canvas to draw',
degree: 0, // 屏幕整体旋转的角度, 可取 -90,90,180等值
scope: [
{
value: 0,
title: '正常',
},
{
value: 90,
title: '顺时针旋转90°',
},
{
value: 180,
title: '顺时针旋转180°',
},
{
value: -90,
title: '逆时针旋转90°',
},
],
};
},
components: {
Draw,
},
mounted() {
this.canvasBox = document.getElementById('canvasBox');
this.initCanvas();
},
computed: {
getHorizontalStyle() {
const d = document;
const w = window.innerWidth || d.documentElement.clientWidth || d.body.clientWidth;
const h = window.innerHeight || d.documentElement.clientHeight || d.body.clientHeight;
let length = (h - w) / 2;
let width = w;
let height = h;
switch (this.degree) {
case -90:
length = -length;
case 90:
width = h;
height = w;
break;
default:
length = 0;
}
if (this.canvasBox) {
this.canvasBox.removeChild(document.querySelector('canvas'));
this.canvasBox.appendChild(document.createElement('canvas'));
setTimeout(() => {
this.initCanvas();
}, 200);
}
return {
transform: `rotate(${this.degree}deg) translate(${length}px,${length}px)`,
width: `${width}px`,
height: `${height}px`,
transformOrigin: 'center center',
};
},
},
methods: {
initCanvas() {
const canvas = document.querySelector('canvas');
this.draw = new Draw(canvas, -this.degree);
},
clear() {
this.draw.clear();
},
download() {
this.draw.downloadPNGImage(this.draw.getPNGImage());
},
upload() {
const image = this.draw.getPNGImage();
const blob = this.draw.dataURLtoBlob(image);
const url = '';
const successCallback = (response) => {
console.log(response);
};
const failureCallback = (error) => {
console.log(error);
};
this.draw.upload(blob, url, successCallback, failureCallback);
},
},
};
</script>
<style>
#canvasBox {
display: flex;
flex-direction: column;
height: 100%;
}
.greet {
padding: 20px;
font-size: 20px;
user-select: none;
}
.greet a {
cursor: pointer;
}
.greet select {
font-size: 18px;
}
canvas {
flex: 1;
cursor: crosshair;
}
</style>