-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas.html
189 lines (169 loc) · 5.24 KB
/
canvas.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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
<meta name="renderer" content="webkit">
<title>Canvas 绘图</title>
<meta name="description" content="首页描述"/>
<meta name="keywords" content="首页关键词"/>
<meta name="author" content="Web Layout:amu"/>
<meta name="format-detection" content="telephone=no,date=no,address=no,email=no,url=no"/>
<link rel="stylesheet" href="./src/style/base.css">
<style>
#canvas{
border: 1px seagreen dashed;transform-origin: 0 0;
width: 800px;height: 400px;max-width: 100%;
box-sizing: border-box;
}
#canvas:hover{cursor :url(assets/images/draw.ico) 0 28,auto;}
#save_href{display: none;}
#outimg{z-index: 10;top:15%}
#outimg img{max-width: 100%;border: 1px dashed #ccc;}
</style>
</head>
<body>
<div class="paper">
<h1>Canvas</h1>
<hr>
<h2>案例:手绘(支持high DPI)</h2>
<p>
<button id="clear">清空</button>
<button id="save">生成图片</button>
<button id="replay">回放</button>
</p>
<canvas id="canvas" width="600" height="400"></canvas>
<br>
<a download="mydraw.png" id="save_href">下载</a>
<span id="touchSize"></span>
<dialog id="outimg">
<br>
<img src="" alt="">
<p class="center">长按图片下载 <button id="btnClose">关闭</button></p>
</dialog>
</div>
</body>
<script type="module">
import canvasdpr from './src/js/canvas.hdpr.js'
let canvas=document.getElementById("canvas");
let strokeCache = []
let index = 1;
let steps = []
let stepIndex = 0
let ctx=canvasdpr(canvas);
let touchSize = document.querySelector('#touchSize')
function drawInit(evt){
let startPoint =[evt.clientX-canvas.offsetLeft,evt.clientY-canvas.offsetTop]
ctx.beginPath();
ctx.lineCap="round";
ctx.shadowBlur=1.4;
ctx.shadowColor="#234";
ctx.moveTo(startPoint[0],startPoint[1]);
strokeCache.push(startPoint)
}
canvas.onmousedown=function(event){
drawInit(event)
document.addEventListener('mousemove',drawFree)
};
function clearCanvas (){
ctx.clearRect(0,0,ctx.canvas.width,ctx.canvas.height)
strokeCache = []
index = 1;
steps = []
stepIndex = 0
btnReplay.disabled = true
btnSave.disabled = true
btnClear.disabled = true
}
function drawFree(evt){
evt.preventDefault();
evt.stopPropagation();
if(!evt.clientX){
console.log(evt)
touchSize.innerHTML = evt.targetTouches[0].radiusX+'-'+evt.targetTouches[0].force //force[0.1~1]
evt.clientX = evt.targetTouches[0].clientX
evt.clientY = evt.targetTouches[0].clientY
}
let point =[evt.clientX-canvas.offsetLeft,evt.clientY-canvas.offsetTop]
ctx.lineTo(point[0],point[1]);
ctx.lineWidth = 6;
ctx.strokeStyle="#234";
ctx.stroke();
strokeCache.push(point)
}
function rePlay(){
ctx.clearRect(0,0,ctx.canvas.width,ctx.canvas.height)
index = 1
stepIndex = 0
ctx.beginPath();
ctx.lineWidth = 6;
ctx.strokeStyle="#234";
console.log(strokeCache)
ctx.moveTo(strokeCache[0][0],strokeCache[0][1])
renderDrawing()
}
function renderDrawing(){
if(index === steps[stepIndex]){
stepIndex ++
ctx.beginPath();
}
ctx.lineTo(strokeCache[index][0],strokeCache[index][1])
ctx.stroke();
if(index<strokeCache.length-1){
index++
requestAnimationFrame(renderDrawing)
}else{
ctx.closePath();
}
}
function drawEnd(){
ctx.closePath();
console.log(strokeCache)
steps.push(strokeCache.length)
if(strokeCache.length>1){
btnReplay.disabled = false
btnSave.disabled = false
btnClear.disabled = false
}
document.removeEventListener('mousemove',drawFree);
}
canvas.onmouseup=function(){
drawEnd()
};
//移动端兼容
canvas.addEventListener('touchstart',(e)=>{
drawInit(e)
canvas.addEventListener('touchmove',drawFree)
})
canvas.addEventListener('touchend',drawEnd)
let btnClear = document.querySelector('#clear')
let btnSave = document.querySelector('#save')
let btnReplay = document.querySelector('#replay')
let btnClose = document.querySelector('#btnClose')
let outDialog = document.querySelector("#outimg")
btnReplay.disabled = true
btnSave.disabled = true
btnClear.disabled = true
btnClear.addEventListener('click',clearCanvas)
btnSave.addEventListener('click',saveCanvas)
btnReplay.addEventListener('click',rePlay)
btnClose.addEventListener('click',closeDialog)
function saveCanvas(){
ctx.save()
let image = new Image()
image.src = canvas.toDataURL("image/png")
if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)){
outDialog.open = true
outDialog.querySelector('img').src=image.src
}else{
let svaeHref = document.querySelector('#save_href')
svaeHref.style.display = 'inline-block'
svaeHref.href=image.src;
}
}
function closeDialog(){
outDialog.open = false
}
</script>
</html>