Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
177 lines (155 loc) · 4.32 KB
/
Copy pathmain.js
File metadata and controls
177 lines (155 loc) · 4.32 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
const content = document.getElementById('xxx');
const context = content.getContext('2d');
let lineWidth = 8;
autoSetCanvasSize(content)
listenToUser(content)
let eraserEnabled = false;
pen.onclick = function(){
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, content.width, content.height);
}
download.onclick = function(){
var url = content.toDataURL("image/png")
var a = document.createElement('a')
document.body.appendChild(a)
a.href = url
a.download = '我的画儿'
a.target = '_blank'
a.click()
}
red.onclick = function(){
context.fillStyle = 'red'
context.strokeStyle = 'red'
red.classList.add('active')
green.classList.remove('active')
blue.classList.remove('active')
}
green.onclick = function(){
context.fillStyle = 'green'
context.strokeStyle = 'green'
red.classList.remove('active')
green.classList.add('active')
blue.classList.remove('active')
}
blue.onclick = function(){
context.fillStyle = 'blue'
context.strokeStyle = 'blue'
red.classList.remove('active')
green.classList.remove('active')
blue.classList.add('active')
}
thin.onclick = function(){
lineWidth = 14
}
thick.onclick = function(){
lineWidth = 20
}
/******/
function autoSetCanvasSize(canvas) {
setCanvasSize()
window.onresize = function() {
setCanvasSize()
}
function setCanvasSize() {
let pageWidth = document.documentElement.clientWidth
let pageHeight = document.documentElement.clientHeight
canvas.width = pageWidth
canvas.height = pageHeight
}
}
function drawLine(x1, y1, x2, y2) {
context.beginPath();
context.moveTo(x1, y1) // 起点
context.lineWidth = lineWidth
context.lineCap = "round"
context.lineTo(x2, y2) // 终点
context.stroke()
context.closePath()
}
function listenToUser(canvas) {
let using = false
let lastPoint = {
x: undefined,
y: undefined
}
// 特性检测
if(document.body.ontouchstart !== undefined){
// 触屏设备 苏菲就是个触屏设备啊哥
canvas.ontouchstart = function(aaa){
let x = aaa.touches[0].clientX
let y = aaa.touches[0].clientY
console.log(x,y)
using = true
if (eraserEnabled) {
context.clearRect(x - 5, y - 5, 10, 10)
} else {
lastPoint = {
"x": x,
"y": y
}
}
}
canvas.ontouchmove = function(aaa){
console.log('边摸边动')
let x = aaa.touches[0].clientX
let y = aaa.touches[0].clientY
if (!using) {return}
if (eraserEnabled) {
context.clearRect(x - 5, y - 5, 10, 10)
} else {
const newPoint = {
"x": x,
"y": y
};
drawLine(lastPoint.x, lastPoint.y, newPoint.x, newPoint.y)
lastPoint = newPoint
}
}
canvas.ontouchend = function(){
console.log('摸完了')
using = false
}
}else{
// 非触屏设备
canvas.onmousedown = function(aaa) {
let x = aaa.clientX
let y = aaa.clientY
using = true
if (eraserEnabled) {
context.clearRect(x - 5, y - 5, 10, 10)
} else {
lastPoint = {
"x": x,
"y": y
}
}
}
canvas.onmousemove = function(aaa) {
const x = aaa.clientX
const y = aaa.clientY
if (!using) {return}
if (eraserEnabled) {
context.clearRect(x - 5, y - 5, 10, 10)
} else {
const newPoint = {
"x": x,
"y": y
}
drawLine(lastPoint.x, lastPoint.y, newPoint.x, newPoint.y)
lastPoint = newPoint
}
}
canvas.onmouseup = function() {
using = false
}
}
}