Skip to content

Commit 3f56225

Browse files
committed
优化绘制功能;
修复获取图片初始高度的错误; 重写辅助轴
1 parent 439e514 commit 3f56225

File tree

2 files changed

+36
-36
lines changed

2 files changed

+36
-36
lines changed

example/labelimg.css

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -139,34 +139,28 @@ polygon:hover {
139139
.lbi-svg-box::-webkit-scrollbar-corner {
140140
background: #E6E6E6;
141141
}
142-
.lbi-svg {
142+
.lbi-axis {
143143
position: absolute;
144144
left: 0;
145145
top: 0;
146-
/* width: 100%;
147-
height: 500px;*/
146+
width: 100%;
147+
height: 550px;
148148
}
149-
.lbi-xaxis {
149+
.lbi-svg {
150150
position: absolute;
151151
left: 0;
152-
right: 4px;
153-
height: 2px;
154-
background-color: #1c79c6;
155-
}
156-
.lbi-yaxis {
157-
position: absolute;
158-
position: absolute;
159152
top: 0;
160-
bottom: 4px;
161-
width: 2px;
162-
background-color: #1c79c6;
153+
z-index: 5;
154+
/* width: 100%;
155+
height: 500px;*/
163156
}
164157
.lbi-mask {
165158
position: absolute;
166159
left: 0;
167160
top: 0;
168161
right: 0;
169162
bottom: 0;
163+
z-index: 10;
170164
display: none;
171165
background-color: rgba(0,0,0,0.2);
172166
}

example/labelimg.js

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ var Labelimg = (function () {
3838
_self = this;
3939

4040
render.call(this)
41-
draw.call(this)
41+
draw()
4242
}
4343

4444
Labelimg.prototype = {
@@ -53,7 +53,7 @@ var Labelimg = (function () {
5353
var svg = document.querySelector('.lbi-svg');
5454
// 保存图片原始尺寸,当图片放大或缩小后,需要与原始尺寸对比,计算比例系数
5555
_self.imgWidth = img.naturalWidth;
56-
_self.imgHeight = img.clientHeight;
56+
_self.imgHeight = img.naturalHeight;
5757
svg.setAttribute('viewBox', '0, 0, ' + _self.imgWidth + ', ' + _self.imgHeight);
5858

5959
// 初始化图片大小,让图片和父元素一样宽,提高体验
@@ -88,18 +88,18 @@ var Labelimg = (function () {
8888
// colorBox
8989
document.querySelector('.lbi-color-box').innerHTML = render.colorBox(this.COLORS);
9090
render.handleColor()
91+
render.handleShape()
9192

9293
// 获取 selectBox 的 html 结构字符串并渲染
9394
var selectHtml = render.selectBox(this.labelObj);
9495
document.getElementById('lbi-select-names').innerHTML = selectHtml.namesHtml;
9596
document.getElementById('lbi-select-labels').innerHTML = selectHtml.labelsHtml;
96-
9797
render.handleSelect()
9898
// renderToolbar(target, tools)
9999
// renderBoard(target)
100100
// renderLabels(target)
101101
// renderTip(target)
102-
// render.axisSetting(document.querySelector('.lbi-paint-box'))
102+
render.axisSetting(document.querySelector('.lbi-paint-box'))
103103
}
104104
// 整体UI框架的 html 结构
105105
render.ui = function () {
@@ -111,8 +111,10 @@ var Labelimg = (function () {
111111
<img src="" alt="" class="lbi-img" />
112112
<svg class="lbi-svg"></svg>
113113
</div>
114-
<div class="lbi-xaxis"></div>
115-
<div class="lbi-yaxis"></div>
114+
<svg class="lbi-axis">
115+
<line x1="0" y1="0" x2="870" y2="0" style="stroke:#1c79c6;stroke-width:2" />
116+
<line x1="0" y1="0" x2="0" y2="550" style="stroke:#1c79c6;stroke-width:2" />
117+
</svg>
116118
</div>
117119
<div class="lbi-mask">
118120
<div class="lbi-select-box">
@@ -251,6 +253,7 @@ var Labelimg = (function () {
251253
for(let i = 0; i < shapes.length; i++) {
252254
shapes[i].onclick = function (e) {
253255
_self.shape = shapes[i].dataset.shape;
256+
draw()
254257
}
255258
}
256259
}
@@ -266,16 +269,18 @@ var Labelimg = (function () {
266269
}
267270
// 设置辅助轴
268271
render.axisSetting = function (target) {
269-
var xaxis = document.querySelector('.lbi-xaxis'),
270-
yaxis = document.querySelector('.lbi-yaxis');
271-
target.onmouseenter = function (e) {
272-
xaxis.style.top = e.offsetY +'px'
273-
yaxis.style.left = e.offsetX +'px'
272+
var axis = document.querySelector('.lbi-axis'),
273+
xaxis = axis.firstElementChild,
274+
yaxis = axis.lastElementChild;
274275
target.onmousemove = function (e) {
275-
xaxis.style.top = e.offsetY +'px'
276-
yaxis.style.left = e.offsetX +'px'
276+
xaxis.setAttribute('y1', e.offsetY)
277+
xaxis.setAttribute('y2', e.offsetY)
278+
yaxis.setAttribute('x1', e.offsetX)
279+
yaxis.setAttribute('x2', e.offsetX)
280+
// xaxis.style.top = e.offsetY +'px'
281+
// yaxis.style.left = e.offsetX +'px'
277282
}
278-
}
283+
279284
}
280285

281286
function renderLabels(target) {
@@ -372,7 +377,8 @@ var Labelimg = (function () {
372377

373378
}
374379
function drawPoint(parent, attrs) {
375-
parent.addEventListener('click', function (e){
380+
parent.onmousedown = parent.onmousemove = parent.onmouseup = null
381+
parent.onclick = function (e){
376382
_self.x = e.offsetX * _self.kx;
377383
_self.y = e.offsetY * _self.ky;
378384
var attrs = {
@@ -387,12 +393,12 @@ var Labelimg = (function () {
387393
var point = createPoint(attrs)
388394
parent.appendChild(point)
389395

390-
// createLabelsItem(parent.children.length)
391396
document.querySelector('.lbi-mask').style.display = 'block';
392-
},false)
397+
}
393398

394399
}
395400
function drawRect(parent) {
401+
parent.onclick = null
396402
var x, y, width, height;
397403
parent.onmousedown = function (e) {
398404
_self.x = e.offsetX * _self.kx;
@@ -430,14 +436,14 @@ var Labelimg = (function () {
430436
parent.removeChild(rect);
431437
return;
432438
}
433-
434-
createLabelsItem(parent.children.length);
439+
document.querySelector('.lbi-mask').style.display = 'block';
435440
}
436441
}
437442
}
438443
function drawPolygon(parent) {
444+
parent.onmousedown = parent.onmousemove = parent.onmouseup = null
439445
// 绘制栈,保存起始点和每条线的 DOM 节点,当多边形绘制完毕后,需要删除之前的circle和line节点
440-
parent.addEventListener('click', function (e) {
446+
parent.onclick = function (e) {
441447
if(e.target.tagName === 'circle') {
442448
var points = _self.polygonConfig.points.join(' ')
443449
var polygon = createPolygon(points)
@@ -447,7 +453,7 @@ var Labelimg = (function () {
447453
parent.removeChild(item)
448454
})
449455
polygon.setAttribute('data-index', parent.children.length)
450-
createLabelsItem(parent.children.length)
456+
document.querySelector('.lbi-mask').style.display = 'block';
451457
_self.polygonConfig.stack = []
452458
_self.polygonConfig.points = []
453459
} else {
@@ -483,7 +489,7 @@ var Labelimg = (function () {
483489
_self.polygonConfig.stack.push(line)
484490
}
485491
}
486-
})
492+
}
487493
}
488494

489495
// 创建 svg 图形

0 commit comments

Comments
 (0)