-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas-tools.html
212 lines (188 loc) · 5.33 KB
/
canvas-tools.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<!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">
<link rel="stylesheet" href="https://artskin.github.io/html-native-ui/assets/index.a9865689.css">
<style>
.canvas-layer{
position: relative;
}
input[type="color"]{
padding:0;
}
input[type="color" i]::-webkit-color-swatch-wrapper{
padding: 0;
}
input[type="color" i]::-webkit-color-swatch{
border: 0;
}
#tools{
display: flex;
}
canvas{
position: absolute;background: rgba(0,0,0,.1);left: 0;top: 0;
aspect-ratio: var(--imgRatio);
}
.canvas-layer img,.canvas-layer canvas{
width: 80%;
}
img{user-select: none;}
#canvas{border: 1px red dashed;box-sizing: border-box;}
</style>
</head>
<body>
<div class="paper">
<h1>Canvas 绘制工具</h1>
<hr>
<div id='tools'>
<button value="rect">矩形</button>
<button value='line'>线段</button>
<input id="color" type="color" value="#cc0000" />
</div>
<div class="canvas-layer">
<img src="assets/images/map.png" alt="">
<canvas id="canvas"></canvas>
</div>
</div>
</body>
<script>
function Shape(x,y,color){
this.x = x
this.y = y
this.color = color
this.isSelected = false;
}
function $(val){
const prefix = val.substr(0,1)
if(prefix ==='.'){
return document.querySelectorAll(val)
} else if(prefix === '#'){
return document.getElementById(val.substring(1))
}else{
return document.querySelector(val)
}
}
let currentType = 'line'
$('#tools').onclick = (e) =>{
const siblings = e.target.parentNode.children;
for(let i=0;i<siblings.length;i++){
//siblings[i].classList.remove('active')
siblings[i].dataset.type =''
}
if(e.target.tagName ==='BUTTON'){
currentType = e.target.value
//e.target.classList.add('active')
e.target.dataset.type ='primary'
}
}
const canvasSetting = {
ctx:'',
color:'#ffcc00',
width:'2px'
}
const canvas = document.getElementById('canvas');
let ctx = canvas.getContext('2d')
let ratioW = 1
let ratioH = 1
let mousedownPos = {x:0,y:0}
const strokeCache = []
function init(){
const bgMap = new Image()
bgMap.src = 'https://artskin.github.io/jsCase/assets/images/map.png'
bgMap.onload = (e)=>{
const img = e.target || e.path[0];
const originWidth = img.naturalWidth
const originHeight = img.naturalHeight
const imgRatio = `${originWidth}/${originHeight}`
canvas.style.cssText = `--imgRatio:${imgRatio}`
canvas.width = originWidth;
canvas.height = originHeight;
ratioW = originWidth/canvas.clientWidth;
ratioH = originHeight/canvas.clientHeight;
console.log(ratioH,ratioW)
//previewShap()
previewPath()
}
}
init()
function previewShap(){
ctx.fillStyle = "rgba(255,0,0,0.5)";
ctx.strokeStyle = 'orange'
ctx.fillRect(10, 13, 335, 340);
}
function previewPath(){
ctx.lineWidth = 5;
ctx.beginPath();
ctx.moveTo(10,10);
ctx.lineTo(100,200);
ctx.lineTo(100,20);
ctx.closePath();
ctx.stroke()
}
function transPos (e){
return {
x:e.layerX*ratioW,
y:e.layerY*ratioH
}
}
let cacheImageData
canvas.onmousedown = function (e){
ctx.fillStyle = $('#color').value
ctx.strokeStyle = $('#color').value
ctx.rect(e.layerX*ratioW-15, e.layerY*ratioH-15, 30, 30);
ctx.fill();
mousedownPos=transPos(e);
cacheImageData = ctx.getImageData(0,0,canvas.width,canvas.height)
strokeCache.push(mousedownPos);
console.log(currentType,strokeCache)
if(currentType ==='rect'){
canvas.addEventListener('mousemove',drawRect)
}else{
canvas.addEventListener('mousemove',drawLine)
}
ctx.setLineDash([]);
console.log(ctx.isPointInPath(mousedownPos.x, mousedownPos.y))
}
canvas.ondblclick = function (e) {
e.preventDefault();
e.stopPropagation();
}
function closeDraw(){
ctx.closePath()
canvas.removeEventListener('mousemove',drawLine);
canvas.removeEventListener('mousemove',drawRect);
}
function drawLine(e){
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.putImageData(cacheImageData,0,0,0,0,canvas.width,canvas.height)
ctx.beginPath()
ctx.moveTo(mousedownPos.x, mousedownPos.y)
ctx.lineTo(e.layerX*ratioW, e.layerY*ratioH);
ctx.stroke();
}
function drawRect(e){
if(strokeCache.length>1){
closeDraw()
ctx.setLineDash([]);
}
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.setLineDash([10,10]);
ctx.putImageData(cacheImageData,0,0,0,0,canvas.width,canvas.height)
const data = [mousedownPos.x,mousedownPos.y,e.layerX*ratioW-mousedownPos.x,e.layerY*ratioH-mousedownPos.y]
ctx.strokeRect(...data)
ctx.fillStyle = 'rgba(0,0,0,.2)'
ctx.fillRect(...data);
//ctx.fill()
//ctx.strokeRect(50, 50, 50, 50);
}
</script>
</html>