-
Notifications
You must be signed in to change notification settings - Fork 0
/
canvas3.html
249 lines (229 loc) · 7.03 KB
/
canvas3.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<!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;opacity: .4;}
#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 $(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)
}
}
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()
// canvas 矩形框集合
var rects=[];
function rectar(x,y,width,height){
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.isSelected = false;
}
function drawRect() {
// 清除画布,准备绘制
context.clearRect(0, 0, canvas.width, canvas.height);
// 遍历所有矩形框
console.log(rects)
for(var i=0; i<rects.length; i++) {
var rect = rects[i];
// 绘制矩形
context.strokeStyle="#FF0000";
context.strokeRect(rect.x,rect.y,rect.width,rect.height,rect.color);
if (rect.isSelected) {
context.lineWidth = 50;
}
else {
context.lineWidth = 10;
}
}
}
function addRandomRect(x=10,y=10,w=200,h=200) {
var x=x;
var y=y;
var width=w;
var height=h;
// 创建一个新的矩形对象
var rect=new rectar(x,y,width,height);
// 把它保存在数组中
rects.push(rect);
// 重新绘制画布
drawRect();
}
var SelectedRect;
var x1;
var y1;
var right=false;
var widthstart,widthend;
var heightstart,heightend;
function canvasClick(e) {
// 取得画布上被单击的点
console.log(e)
var clickX = e.pageX - canvas.offsetLeft;
var clickY = e.pageY - canvas.offsetTop;
// 查找被单击的矩形框
for(var i=rects.length-1; i>=0; i--) {
var rect = rects[i];
widthstart=rect.x;
widthend=rect.x+rect.width;
heightstart=rect.y;
heightend=rect.y+rect.height;
// 判断这个点是否在矩形框中
console.log(clickX,clickY,rect)
console.log(clickX>=widthstart,(clickX<(widthend-20)),(clickY>=heightstart),(clickY<(heightend-20)))
if ((clickX>=widthstart&&clickX<(widthend-20))&&(clickY>=heightstart)&&(clickY<(heightend-20))) {
console.log(clickX);
// 清除之前选择的矩形框
if (SelectedRect != null) SelectedRect.isSelected = false;
SelectedRect = rect;
x1=clickX-SelectedRect.x;
y1=clickY-SelectedRect.y;
//选择新圆圈
rect.isSelected = true;
// 使圆圈允许拖拽
isDragging = true;
//debugger
//更新显示
drawRect();
//停止搜索
return;
};
/*
设置拉伸的界限。
*/
// if ((clickX>=(widthend-20))&&(clickY>=(heightend-20)))
// {
// SelectedRect = rect;
// right=true;
// } //18-02-01改 if ((clickX>=(widthend-20)&&((clickX<=(widthend+20)))&&(clickY>=(heightend-20))&&(clickY>=(heightend+20))) { SelectedRect = rect; right=true; }
}
}
function dragRect(e) {
// 判断矩形是否开始拖拽
if (isDragging == true) {
// 判断拖拽对象是否存在
if (SelectedRect != null) {
// 取得鼠标位置
var x = e.pageX - canvas.offsetLeft;
var y = e.pageY - canvas.offsetTop;
// 将圆圈移动到鼠标位置
SelectedRect.x= x-x1;
SelectedRect.y= y-y1;
// 更新画布
drawRect();
}
}
//判断是否开始拉伸
if (right) {
//设置拉伸最小的边界
if ((e.pageX - canvas.offsetLeft-SelectedRect.x)>50) {
SelectedRect.width=e.pageX - canvas.offsetLeft-SelectedRect.x;
}
else {
SelectedRect.width=50;
}
console.log(SelectedRect.width);
if((e.pageY - canvas.offsetTop-SelectedRect.y)>50){
SelectedRect.height=e.pageY - canvas.offsetTop-SelectedRect.y;
} else {
SelectedRect.height=50;
}
drawRect();
}
};
var isDragging = false;
function stopDragging() {
isDragging = false;
right=false;
}
function clearCanvas() {
// 去除所有矩形
rects = [];
// 重新绘制画布.
drawCircles();
}
window.onload = function() {
canvas = document.getElementById("canvas");
context = canvas.getContext("2d");
canvas.onmousedown = canvasClick;
canvas.onmouseup = stopDragging;
canvas.onmouseout = stopDragging;
canvas.onmousemove =dragRect;
addRandomRect()
addRandomRect(200,200,100,100)
};
</script>
</html>