forked from heylight/canvas-select
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
161 lines (151 loc) · 4.03 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
<title>canvas-select示例</title>
<style>
* {
margin: 0;
padding: 0;
}
/* .box {
display: flex;
} */
.container {
background-color: #ccc;
width: 100%;
max-width: 400px;
height: 400px;
}
.right {
margin-left: 20px;
}
#output {
height: 450px;
width: 400px;
}
</style>
</head>
<body>
<div class="box">
<div class="left">
<canvas class="container"></canvas>
<div>
<button onclick="change(1)">创建矩形</button>
<button onclick="change(2)">创建多边形</button>
<button onclick="change(3)">创建点</button>
<button onclick="change(4)">创建折线</button>
<button onclick="change(5)">创建圆形</button>
<button onclick="change(0)">取消创建</button>
<br />
<button onclick="zoom(true)">放大</button>
<button onclick="zoom(false)">缩小</button>
<button onclick="fitting()">适配</button>
<button onclick="focusMode()">专注模式</button>
</div>
</div>
<!-- <div class="right">
<div>output:</div>
<textarea id="output" onchange="changeData()"></textarea>
</div> -->
</div>
<script src="./lib/canvas-select.min.js"></script>
<script>
// alert(document.body.clientWidth)
const output = document.getElementById('output');
const instance = new CanvasSelect('.container', 'https://www.zhuhailiang.com/api/images/onepiece.png');
// instance.focusMode = true
instance.ctrlRadius = navigator.userAgent.includes('Mobile') ? 6 : 3
window.c = instance;
let option = [
{
label: "矩形",
coor: [[184, 183], [275, 238]],
type: 1
},
{
label: "多边形",
coor: [[135, 291], [129, 319], [146, 346], [174, 365], [214, 362], [196, 337], [161, 288]],
type: 2
},
{
label: "点",
coor: [345, 406],
type: 3
},
{
label: "折线",
coor: [[470, 155], [490, 230], [493, 298]],
type: 4
},
{
label: "圆形",
coor: [369, 197],
radius: 38,
type: 5
},
]
// let list = []
// for (let i = 0; i < 1000; i++) {
// list = list.concat(option)
// }
// console.log('number, list.length)
instance.on('load', (src) => {
console.log('图片加载完成', src);
instance.setData(option);
});
instance.on('warn', (msg) => {
console.warn('warn', msg);
});
// 添加
instance.on('add', (info) => {
console.log('当前添加', info)
window.info = info
});
// 删除
instance.on('delete', (info) => {
console.log('当前删除', info)
});
// 选中
instance.on('select', (info) => {
console.log('当前选中', info)
});
instance.on('updated', (result) => {
console.log('标注结果', result)
// const list = [...result]
// list.sort((a, b) => a.index - b.index)
// output.value = JSON.stringify(list, null, 2);
});
function change(num) {
instance.createType = num;
}
function zoom(type) {
instance.setScale(type);
}
function fitting() {
instance.fitZoom();
}
function focusMode() {
instance.setFocusMode(!instance.focusMode);
}
function changeData() {
const data = JSON.parse(output.value)
instance.setData(data)
}
</script>
<!-- <script src="https://unpkg.com/[email protected]/build/stats.min.js"> </script>
<script>
const stats = new Stats();
// stats.showPanel(2); // 0: fps, 1: ms, 2: mb, 3+: custom
document.body.appendChild(stats.dom);
function animate() {
stats.begin();
// monitored code goes here
stats.end();
requestAnimationFrame(animate);
}
requestAnimationFrame(animate);
</script> -->
</body>
</html>