-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdraw.html
152 lines (152 loc) · 5.73 KB
/
draw.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>简易触控画图工具(Touch API、Canvas绘制测试)</title>
<style>
canvas {
position: absolute;
left: 0px;
top: 0px;
touch-action: none;
}
dialog {
position: absolute;
}
input[type=number] {
width: 3em;
}
div {
display: inline-block;
width: auto;
height: 20px;
text-align: center;
background-color: skyblue;
}
</style>
</head>
<body>
<canvas id="canvas" onclick="toggleMenu()"></canvas>
<dialog id="menu">
颜色 <input type="color" id="color">
笔刷粗细 <input type="number" id="width" value="2">
启用压感 <input type="checkbox" id="force" checked="checked">
工具
<select id="tools">
<option>画笔</option>
<option value="rubber">橡皮</option>
</select><br />
<div onclick="setSize()">重设画布大小(清除全部内容)</div>
<div onclick="download()">导出画布</div>
彩虹色 <input type="checkbox" id="rainbow">
</dialog>
<script>
var c = document.getElementById("canvas");
var ctx = c.getContext('2d');
var w,i;
var h;
var mousedown;
var rainbow = false;
var rainbowcolor;
function setSize() {
w = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
h = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
c.width = w;
c.height = h;
ctx.fillStyle = "White";
ctx.fillRect(0,0,w,h);
}
function toggleMenu() {
if (document.getElementById("menu").open) {
document.getElementById("menu").open = false;
}
else {
document.getElementById("menu").open = "open";
}
}
function download() {
var name = prompt("请输入文件名(不带后缀名):","Project");
if (!name) {
name = "Project";
}
var url = c.toDataURL();
var link = document.createElement("a");
link.href = url;
link.download = name + ".png";
link.style = "display:none;";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
c.addEventListener("touchstart",(e) => {
ctx.lineWidth = document.getElementById("width").value;
if (document.getElementById("tools").value == "rubber") {
ctx.strokeStyle = "rgb(255,255,255)";
}
else if(rainbow) {
ctx.strokeStyle = rainbowcolor;
}
else {
ctx.strokeStyle = document.getElementById("color").value;
}
ctx.beginPath();
ctx.moveTo(e.touches[0].pageX,e.touches[0].pageY);
});
c.addEventListener("touchmove",(e) => {
if (document.getElementById("force").checked && e.touches[0].force != 0) {
ctx.lineWidth = document.getElementById("width") * e.touches[0].force;
}
ctx.lineTo(e.touches[0].pageX,e.touches[0].pageY);
ctx.stroke();
//console.log("触摸坐标:",e.touches[0].clientX,e.touches[0].clientY,"压感:",e.touches[0].force);
});
c.addEventListener("touchend",(e) => {
ctx.stroke();
});
c.addEventListener("mousedown",(e) => {
ctx.lineWidth = document.getElementById("width").value;
if (document.getElementById("tools").value == "rubber") {
ctx.strokeStyle = "rgb(255,255,255)";
}
else if(rainbow) {
ctx.strokeStyle = rainbowcolor;
}
else {
ctx.strokeStyle = document.getElementById("color").value;
}
ctx.beginPath();
ctx.moveTo(e.pageX,e.pageY);
mousedown = true;
});
c.addEventListener("mousemove",(e) => {
if (mousedown){
ctx.lineWidth = document.getElementById("width");
ctx.lineTo(e.pageX,e.pageY);
ctx.stroke();
//console.log("触摸坐标:",e.clientX,e.clientY);
}
});
c.addEventListener("mouseup",(e) => {
ctx.stroke();
mousedown = false;
});
document.getElementById("rainbow").addEventListener("click",() => {
if (!rainbow){
rainbow = true;
i = 0;
var int = window.setInterval(() => {
for (;i < rainbowcolor;i++) {
rainbowcolor = "hsl(" + i + ",100%,50%)";
}
i = 0;
},20);
}
else {
rainbow = false;
window.clearInterval(int);
}
});
setSize();
</script>
</body>
</html>