-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
202 lines (179 loc) · 4.79 KB
/
main.js
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
var yyy = document.getElementById('xxx');
var context = yyy.getContext('2d');
var lineWidth = 5;
autoSetCanvasSize(yyy)
/*************/
listenToUser(yyy)
var eraserEnabled = false;
pen.onclick = function(){
//click事件兼容移动端和pc
eraserEnabled = false
pen.classList.add('active')
eraser.classList.remove('active')
}
eraser.onclick = function(){
eraserEnabled = true
eraser.classList.add('active')
pen.classList.remove('active')
}
//橡皮事件
clear.onclick = function(){
context.clearRect(0,0,yyy.width,yyy.height);
}//清除图像
download.onclick = function(){
var url = yyy.toDataURL("image/png")
console.log(url)
var a = document.createElement('a')
document.body.appendChild(a)
a.href = url;
a.download = '我的画'
a.target = '_blank'
a.click();
}
/***********/
function listenToUser(canvas) {
var using = false;
var lastPoint = { x: undefined, y: undefined }
//特性检测
if (document.body.ontouchstart !== undefined) {
//触屏设备
canvas.ontouchstart = function (aaa) {
console.log('开始摸')
console.log(aaa)
var x = aaa.touches[0].clientX;
var y = aaa.touches[0].clientY;
/*因为移动触屏设备支持多点触控,所以会存储多个触点信息,
现在只有一个触点那就是鼠标指针,数据存在touches第零个数组里。*/
using = true;
if (eraserEnabled) {
context.clearRect(x - 5, y - 5, 10, 10)
}
else {
lastPoint = { "x": x, "y": y }
console.log(lastPoint);
//drawCircle(x, y, 1);画圆
}
}
canvas.ontouchmove = function (aaa) {
console.log('摸着拖')
var x = aaa.touches[0].clientX;
var y = aaa.touches[0].clientY;
console.log(x,y)
if (!using) { return }
if (eraserEnabled) {
context.clearRect(x - 5, y - 5, 10, 10)
}
else {
var newPoint = {
"x": x,
"y": y
}
//drawCircle(x, y, 1);画圆
drawLine(lastPoint.x, lastPoint.y, newPoint.x, newPoint.y)//画线从xy到新的xy
lastPoint = newPoint;//新的点变成最后一个点,用来更新
}
}
canvas.ontouchend = function () {
console.log('摸完了')
using = false;
}
}
else {
//非触屏设备
canvas.onmousedown = function (aaa) {
var x = aaa.clientX;
var y = aaa.clientY;
using = true;
if (eraserEnabled) {
context.clearRect(x - 5, y - 5, 10, 10)
}
else {
lastPoint = { "x": x, "y": y }
console.log(lastPoint);
//drawCircle(x, y, 1);画圆
}
}
canvas.onmousemove = function (aaa) {
var x = aaa.clientX;
var y = aaa.clientY;
if (!using) { return }
if (eraserEnabled) {
context.clearRect(x - 5, y - 5, 10, 10)
}
else {
var newPoint = {
"x": x,
"y": y
}
//drawCircle(x, y, 1);画圆
drawLine(lastPoint.x, lastPoint.y, newPoint.x, newPoint.y)//画线从xy到新的xy
lastPoint = newPoint;//新的点变成最后一个点,用来更新
}
}
canvas.onmouseup = function (aaa) {
using = false;
}
}
}
black.onclick = function(){
context.strokeStyle= 'black'
black.classList.add('active')
red.classList.remove('active')
green.classList.remove('active')
blue.classList.remove('active')
}
red.onclick = function(){
context.strokeStyle = 'red'
black.classList.remove('active')
red.classList.add('active')
green.classList.remove('active')
blue.classList.remove('active')
}
green.onclick = function(){
black.classList.remove('active')
context.strokeStyle = 'green'
red.classList.remove('active')
green.classList.add('active')
blue.classList.remove('active')
}
blue.onclick = function(){
black.classList.remove('active')
context.strokeStyle = 'blue'
red.classList.remove('active')
green.classList.remove('active')
blue.classList.add('active')
}
//切换颜色
thin.onclick = function(){
lineWidth = 5;
}
thick.onclick = function(){
lineWidth = 10;
}
/***********/
function autoSetCanvasSize(canvas) {
setCanvasSize()
window.onresize = function () {
setCanvasSize()
}
function setCanvasSize() {
var pageWidth = document.documentElement.clientWidth//从api获取宽度
var pageHeight = document.documentElement.clientHeight//从api获取高度
canvas.width = pageWidth;
canvas.height = pageHeight;
}
}
/****************/
function drawCircle(x, y, radius) {
context.beginPath();
context.arc(x, y, radius, 0, Math.PI * 2);
context.fill();
}
function drawLine(x1, y1, x2, y2) {
context.beginPath();
context.moveTo(x1, y1);//起点
context.lineWidth = lineWidth;//线宽
context.lineTo(x2, y2);//终点
context.stroke();//划线
context.closePath();//结束
}