-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas_3.html
154 lines (136 loc) · 4.9 KB
/
canvas_3.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" type="" href="https://at.alicdn.com/t/font_dro5eh6p4ro4j9k9.css">
<style>
* {
margin: 0;
padding: 0;
}
body {
overflow: hidden;
}
#app {
background: #ddd;
display: block;
}
.actions {
position: fixed;
top: 0;
left: 0;
padding: 4px;
width: 100%;
}
.round{
position: absolute;
width: 10px;
height: 10px;
background: darkgrey;
border-radius:5px;
right: 0;
top: 0;
transition-property: opacity;
transition-delay: 0.1s;
}
</style>
</head>
<body>
<div class="actions">
<label><input type="radio" name="penType" value="pen" checked><i class="iconfont icon-pen"></i></label>
<label><input type="radio" name="penType" value="eraser"><i class="iconfont icon-eraser"></i></label>
<button type="" id="save"><i class="iconfont icon-save"></i></button>
</div>
<div class="round">
</div>
<canvas id="app" width="375" height="667"></canvas>
<script>
let previousPoint;
let canvas = document.querySelector('#app')
let clientWidth = document.documentElement.clientWidth;
let clientHeight = document.documentElement.clientHeight
canvas.width = clientWidth;
canvas.height = clientHeight;
let context = canvas.getContext('2d');
let width = canvas.width,height=canvas.height;
if (window.devicePixelRatio) {
canvas.style.width = width + "px";
canvas.style.height = height + "px";
canvas.height = height * window.devicePixelRatio;
canvas.width = width * window.devicePixelRatio;
context.scale(window.devicePixelRatio, window.devicePixelRatio);
}
let r = document.querySelector('.round');
context.fillStyle = "green"
context.strokeStyle = 'green';
canvas.addEventListener('touchstart',function(e){
let {pageX,pageY} = e.touches[0]
r.style.opacity = 1;
r.style.left = pageX - 5 + 'px'
r.style.top = pageY - 5 + 'px'
// round = document.createElement('div')
// round.style.position = 'absolute';
// round.style.top = pageY -5 + "px"
// round.style.left = pageX -5 + "px";
// round.style.height = '10px';
// round.style.width = '10px';
// round.style.opacity = 0.5;
// round.style.borderRadius = '5px';
// round.style.background = "green"
// round.style.transition = "opacity 1s"
// document.body.appendChild(round);
})
canvas.addEventListener('touchmove',function(e){
e.preventDefault();//加入这一句话之后,屏幕都不能缩放了
let penType = document.querySelector('input[type=radio]:checked').value
let {pageX,pageY} = e.touches[0];
r.style.left = pageX - 5 + 'px'
r.style.top = pageY - 5 + 'px'
// context.arc(pageX,pageY,5,0,Math.PI*2,false)
// context.fill()
if(penType === 'pen'){
if(previousPoint){
context.beginPath();
context.moveTo(previousPoint.pageX,previousPoint.pageY)
context.lineTo(pageX,pageY)
context.stroke()
}
previousPoint = {
pageX,pageY
}}else{
context.globalCompositeOperation= 'destination-out'
context.arc(pageX, pageY, 20, 0, Math.PI*2, true);
context.fill();
}
// previousPoint = {
// pageX:pageX,
// pageY:pageY
// }
})
canvas.addEventListener('touchend',function(e){
previousPoint = null;
r.style.opacity = 0;
})
save.onclick = function(){
var canvas = document.getElementById('app');
var data = canvas.toDataURL('image/png');
var newWindow = window.open('about:blank','image from canvas');
newWindow.document.write('<img src=' + `'${data}'` + ' alt = from canvas />')
}
// canvas.addEventListener('touchmove',function(e){
// let {pageX,pageY} = e.touches[0];
// context.beginPath();
// context.moveTo(pageX,pageY);
// })
// context.fillstyle = "green";
// context.strokestyle = "blue";
// context.beginPath()
// context.moveTo(75,50);
// context.lineTo(100,75);
// context.lineTo(100,25);
// context.lineTo(75,50);
// context.stroke();
</script>
</body>
</html>