-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
180 lines (155 loc) · 4.13 KB
/
Copy pathindex.js
File metadata and controls
180 lines (155 loc) · 4.13 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
var canvas = document.getElementById('canvas')
var context = canvas.getContext('2d')
var lineWidth = 3
var useEraser= false
var usePaint = false
clear.onclick = function() {
context.clearRect(0, 0, canvas.width, canvas.height)
}
download.onclick = function() {
var url = canvas.toDataURL('image/png')
var a = document.createElement('a')
a.href = url
a.download = 'canvas'
a.click()
}
thin.onclick = function() {
lineWidth = 3
}
thick.onclick = function() {
lineWidth = 5
}
pen.onclick = function() {
useEraser = false
pen.classList.add('active')
eraser.classList.remove('active')
}
eraser.onclick = function() {
useEraser = true
pen.classList.remove('active')
eraser.classList.add('active')
}
black.onclick = function() {
context.strokeStyle = 'black'
black.classList.add('active')
red.classList.remove('active')
yellow.classList.remove('active')
green.classList.remove('active')
pen.classList.add('black')
pen.classList.remove('red')
pen.classList.remove('yellow')
pen.classList.remove('green')
}
red.onclick = function() {
context.strokeStyle = 'red'
red.classList.add('active')
black.classList.remove('active')
yellow.classList.remove('active')
green.classList.remove('active')
pen.classList.remove('black')
pen.classList.add('red')
pen.classList.remove('yellow')
pen.classList.remove('green')
}
yellow.onclick = function() {
context.strokeStyle = 'yellow'
black.classList.remove('active')
red.classList.remove('active')
yellow.classList.add('active')
green.classList.remove('active')
pen.classList.remove('black')
pen.classList.remove('red')
pen.classList.add('yellow')
pen.classList.remove('green')
}
green.onclick = function() {
context.strokeStyle = 'green'
black.classList.remove('active')
red.classList.remove('active')
yellow.classList.remove('active')
green.classList.add('active')
pen.classList.remove('black')
pen.classList.remove('red')
pen.classList.remove('yellow')
pen.classList.add('green')
}
autoSetCanvasSize(canvas)
listenToUser()
//判断设备是否支持touch事件
function listenToUser() {
var lastPoint = {x:undefined, y: undefined}
//特性检测
if(document.body.ontouchstart !== undefined) {
//触屏设备
canvas.ontouchstart = function(e) {
console.log('开始摸我了')
var x = e.touches[0].clientX, y = e.touches[0].clientY
usePaint = true
if(useEraser) {
context.clearRect(x, y, 10, 10)
} else {
lastPoint.x = x, lastPoint.y = y
}
}
canvas.ontouchmove = function(e) {
console.log('正在摸')
var x = e.touches[0].clientX, y = e.touches[0].clientY
var newPoint = {x:x, y:y}
if(!usePaint) return
if(useEraser) {
context.clearRect(x, y, 14, 14)
} else {
drawLine(lastPoint.x, lastPoint.y, newPoint.x, newPoint.y)
}
lastPoint = newPoint }
canvas.ontouchend = function() {
console.log('摸完了')
usePaint = false
}
} else{
//非触屏设备
canvas.onmousedown = function(e) {
var x = e.clientX, y = e.clientY
usePaint = true
if(useEraser) {
context.clearRect(x, y, 14, 14)
} else {
lastPoint.x = x, lastPoint.y = y
}
}
canvas.onmousemove = function(e) {
var x = e.clientX, y = e.clientY
var newPoint = {x:x, y:y}
if(!usePaint) return
if(useEraser) {
context.clearRect(x, y, 10, 10)
} else {
drawLine(lastPoint.x, lastPoint.y, newPoint.x, newPoint.y)
}
lastPoint = newPoint
}
canvas.onmouseup = function(e) {
usePaint = false
}
}
}
function autoSetCanvasSize(canvas) {
changeCanvasWidth(canvas)
window.onresize = function() {
changeCanvasWidth(canvas)
}
}
function changeCanvasWidth(canvas) {
var height = document.documentElement.clientHeight
var width = document.documentElement.clientWidth
canvas.width = width
canvas.height = height
}
function drawLine(x1, y1, x2, y2) {
context.beginPath()
context.lineWidth = lineWidth
context.moveTo(x1, y1)
context.lineTo(x2, y2)
context.stroke() //描边
context.closePath()
}