-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
229 lines (201 loc) · 5.91 KB
/
script.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
var img = new Image(); // 创建一个<img>元素
// 获取canvas
let canvas = document.getElementById("canvas");
const open = document.querySelector('.open')
const select = document.querySelector('.select')
const set = document.querySelector('.set')
const options = document.querySelector('.options')
const exportCanvas = document.querySelector('.exportCanvas')
const clear = document.querySelector('.clear')
window.onclick = (e) => {
if (e.target === open) {
options.style.display = 'none'
select.style.display = 'flex'
return
}
if (e.target === set) {
select.style.display = 'none'
options.style.display = 'flex'
return
}
select.style.display = 'none'
options.style.display = 'none'
}
const color = ['#ff5252', '#000', '#00c853', '#00b0ff']
const point = [5, 10, 12, 15, 20]
CanvasRenderingContext2D.prototype.clearArc = function (x, y, radius, startAngle, endAngle, anticlockwise) {
this.beginPath();
this.globalCompositeOperation = 'destination-out';
this.fillStyle = 'black';
this.arc(x, y, radius, startAngle, endAngle, anticlockwise);
// 参数分别是:圆心横坐标、纵坐标、半径、开始的角度、结束的角度、是否逆时针
this.fill();
this.closePath();
};
//获取这个元素的context——图像稍后将在此被渲染。
const ctx = canvas.getContext('2d') //建立一个 CanvasRenderingContext2D 二维渲染上下文。
//获取当前文档
canvas.width = document.documentElement.clientWidth * window.devicePixelRatio;
canvas.height = document.documentElement.clientHeight * window.devicePixelRatio;
canvas.style.width = document.documentElement.clientWidth + "px";
canvas.style.height = document.documentElement.clientHeight + "px";
const canvasOptions = {
strokeStyle: 'rgb(246,55,64)',
lineWidth: 10,
lineJoin: 'round',
lineCap: 'round',
scaleX: window.devicePixelRatio,
scaleY: window.devicePixelRatio
}
const data = {
isPaint: false,// 绘制状态
beginPoint: { x: null, y: null },//起始点
controlPoint: { x: null, y: null },//控制点
endPoint: { x: null, y: null },//结束点
pointsArr: []//贝塞尔曲线需要计算的点
}
//绘制
//设置画笔宽度
ctx.strokeStyle = canvasOptions.strokeStyle
ctx.lineWidth = canvasOptions.lineWidth
ctx.lineJoin = canvasOptions.lineJoin
ctx.lineCap = canvasOptions.lineCap
// 根据 x 水平方向和 y 垂直方向,为canvas 单位添加缩放变换,消除锯齿
ctx.scale(canvasOptions.scaleX, canvasOptions.scaleY);
select.onclick = (e) => {
if (e.target.dataset.color) {
ctx.strokeStyle = e.target.dataset.color
}
if (e.target.dataset.size) {
ctx.lineWidth = parseInt(e.target.dataset.size)
}
}
// 判断是否移动设备
let isPhone = "ontouchstart" in document.documentElement;
if (!isPhone) {
canvas.onmousedown = (e) => {
data.isPaint = true
pushPoint(e)
data.beginPoint = {
x: e.clientX,
y: e.clientY
}
}
canvas.onmousemove = (e) => {
if (!data.isPaint) return;
pushPoint(e)
if (data.pointsArr.length >= 3) {
//得到控制点
getPoint('mousemove')
paint(data.beginPoint, data.controlPoint, data.endPoint).then(() => {
data.beginPoint = data.endPoint;
})
}
}
canvas.onmouseup = (e) => {
pushPoint(e)
if (data.pointsArr.length >= 3) {
getPoint('mouseup')
//绘制
paint(data.beginPoint, data.controlPoint, data.endPoint).then(() => {
arr.push(ctx)
data.isPaint = false
data.beginPath = null
data.pointsArr = []
})
}
}
} else {
canvas.ontouchstart = (e) => {
data.isPaint = true
pushPoint(e.changedTouches[0])
data.beginPoint = {
x: e.clientX,
y: e.clientY
}
}
canvas.ontouchmove = (e) => {
if (!data.isPaint) return;
pushPoint(e.changedTouches[0])
if (data.pointsArr.length >= 3) {
//得到控制点
getPoint('mousemove')
paint(data.beginPoint, data.controlPoint, data.endPoint).then(() => {
data.beginPoint = data.endPoint;
})
}
}
canvas.ontouchend = (e) => {
pushPoint(e.changedTouches[0])
if (data.pointsArr.length >= 3) {
getPoint('mouseup')
//绘制
paint(data.beginPoint, data.controlPoint, data.endPoint).then(() => {
arr.push(ctx)
data.isPaint = false
data.beginPath = null
data.pointsArr = []
})
}
}
}
var arr = []
const pushPoint = (e) => {
data.pointsArr.push({
x: e.clientX,
y: e.clientY
})
}
const getPoint = (stage) => {
if (stage === 'mousemove') {
//得到控制点
let controlAndEnd = data.pointsArr.slice(-2)
data.controlPoint = controlAndEnd[0]
//计算出结束点
data.endPoint = {
x: (controlAndEnd[0].x + controlAndEnd[1].x) / 2,
y: (controlAndEnd[0].y + controlAndEnd[1].y) / 2,
}
return
}
//mouseup
//得到控制点
let controlAndEnd = data.pointsArr.slice(-2)
data.controlPoint = controlAndEnd[0]
//计算出结束点
data.endPoint = controlAndEnd[1]
}
function paint(beginPoint, controlPoint, endPoint) {
return new Promise((reslove) => {
//绘制线段
ctx.beginPath();
ctx.moveTo(beginPoint.x, beginPoint.y);
ctx.quadraticCurveTo(controlPoint.x, controlPoint.y, endPoint.x, endPoint.y);
ctx.stroke();
reslove()
})
}
/*
橡皮檫
*/
let eraser = document.querySelector('.eraser')
eraser.onclick = (e) => {
canvas.addEventListener('mousemove', function (e) {
ctx.clearArc(e.clientX, e.clientY, 10, 10);
})
}
//导出
exportCanvas.onclick = () => {
var fullQuality = canvas.toDataURL("image/png", 1.0);
var dlLink = document.createElement('a');
dlLink.download = new Date().getDate()
dlLink.href = fullQuality;
dlLink.dataset.downloadurl = ["image/png", dlLink.download, dlLink.href].join(':');
document.body.appendChild(dlLink);
dlLink.click();
document.body.removeChild(dlLink);
}
//清空
clear.onclick = () => {
ctx.clearRect(0, 0, canvas.width, canvas.height)
}