Browse Source

修正正确缩放图片BUG;封装标注事件

develop
陈鹏飞 3 years ago
parent
commit
976700b05b
  1. 166
      src/app/system-management/image-label2/image-label2.component.ts

166
src/app/system-management/image-label2/image-label2.component.ts

@ -54,10 +54,14 @@ export class ImageLabel2Component implements OnInit {
// 等比例缩放图片 // 等比例缩放图片
var scale = 1; var scale = 1;
if (img.width > center.clientWidth || img.height > center.clientHeight) { if (img.width > center.clientWidth || img.height > center.clientHeight) {
if (img.width > img.height) { let scaleOne = center.clientWidth / img.width;
scale = center.clientWidth / img.width; let scaleTwo = center.clientHeight / img.height;
}else { if (img.width * scaleOne <= center.clientWidth && img.height * scaleOne <= center.clientHeight) {
scale = center.clientHeight / img.height; scale = scaleOne;
} else if (img.width * scaleTwo <= center.clientWidth && img.height * scaleTwo <= center.clientHeight) {
scale = scaleTwo;
} else {
scale = 0.3;
} }
} }
that.canvasWidth = img.width * scale; that.canvasWidth = img.width * scale;
@ -109,89 +113,89 @@ export class ImageLabel2Component implements OnInit {
// let html = document.documentElement // let html = document.documentElement
// let boxDiv = document.getElementsByClassName("canvasDialog")[0] // let boxDiv = document.getElementsByClassName("canvasDialog")[0]
canvas.onmousedown = (e)=>{ canvas.onmousedown = (e)=>{
var clickX = e.pageX - canvas.offsetLeft; this.canvasClickEvent(e,canvas,context);
var clickY = e.pageY - canvas.offsetTop; }
this.downx = clickX }
this.downy = clickY
if (this.isInt(clickX, clickY)) {
this.isInOut = true
return
} else {
this.isInOut = false
}
let index //canvas 点击事件 标注
let beforeCircles = this.markType? this.circles: this.circles2 canvasClickEvent(e, canvas, context) {
//判断当前点击点是否在已经绘制的圆圈上,如果是执行相关操作,并return,不进入画线的代码 var clickX = e.pageX - canvas.offsetLeft;
for (var i = 0; i < beforeCircles.length; i++) { var clickY = e.pageY - canvas.offsetTop;
let circle = beforeCircles[i]; this.downx = clickX
//使用勾股定理计算这个点与圆心之间的距离 this.downy = clickY
var distanceFromCenter = Math.sqrt(Math.pow(circle.x - clickX, 2) + Math.pow(circle.y - clickY, 2)); if (this.isInt(clickX, clickY)) {
this.isInOut = true
// 如果是其他的点,则设置可以拖动 return
if (distanceFromCenter <= circle.radius) { } else {
// 清除之前选择的圆圈 this.isInOut = false
index = i; }
this.isDragging = true;
//停止搜索 let index
return; let beforeCircles = this.markType? this.circles: this.circles2
} //判断当前点击点是否在已经绘制的圆圈上,如果是执行相关操作,并return,不进入画线的代码
} for (var i = 0; i < beforeCircles.length; i++) {
//如果点击新的位置,则进入下面的代码,绘制点 let circle = beforeCircles[i];
//context.clearRect(0, 0, canvas.width, canvas.height); //使用勾股定理计算这个点与圆心之间的距离
//遍历数组画圆 var distanceFromCenter = Math.sqrt(Math.pow(circle.x - clickX, 2) + Math.pow(circle.y - clickY, 2));
var circle = {
x: clickX, // 如果是其他的点,则设置可以拖动
y: clickY, if (distanceFromCenter <= circle.radius) {
radius: 10, // 清除之前选择的圆圈
color: "blue", index = i;
isSelected: false, //拖拽点的标记 this.isDragging = true;
}; //停止搜索
if (this.markType) { return;
this.circles.push(circle);
this.allpoints = JSON.parse(JSON.stringify(this.circles))
this.circles[0].color = "green";
} else {
this.circles2.push(circle);
this.allpoints2 = JSON.parse(JSON.stringify(this.circles2))
this.circles2[0].color = "red";
}
for (var i = 0; i < beforeCircles.length; i++) {
let circle = beforeCircles[i];
// 绘制圆圈
context.globalAlpha = 0.85;
context.beginPath();
context.arc(circle.x, circle.y, circle.radius, 0, Math.PI * 2);
context.fillStyle = circle.color;
context.strokeStyle = "black";
context.fill();
context.stroke();
} }
// 画线 }
var point = { //如果点击新的位置,则进入下面的代码,绘制点
x: clickX, //context.clearRect(0, 0, canvas.width, canvas.height);
y: clickY, //遍历数组画圆
}; var circle = {
this.markType? this.points.push(point) : this.points2.push(point) x: clickX,
y: clickY,
radius: 10,
color: "blue",
isSelected: false, //拖拽点的标记
};
if (this.markType) {
this.circles.push(circle);
this.allpoints = JSON.parse(JSON.stringify(this.circles))
this.circles[0].color = "green";
} else {
this.circles2.push(circle);
this.allpoints2 = JSON.parse(JSON.stringify(this.circles2))
this.circles2[0].color = "red";
}
for (var i = 0; i < beforeCircles.length; i++) {
let circle = beforeCircles[i];
// 绘制圆圈
context.globalAlpha = 0.85;
context.beginPath(); context.beginPath();
context.lineWidth = 3; context.arc(circle.x, circle.y, circle.radius, 0, Math.PI * 2);
//从起始点开始绘制 context.fillStyle = circle.color;
let beforePoint = this.markType? this.points: this.points2 context.strokeStyle = "black";
context.moveTo(beforePoint[0].x, beforePoint[0].y);
for (var i = 0; i < beforePoint.length; i++) {
context.lineTo(beforePoint[i].x, beforePoint[i].y);
}
context.closePath()
context.fillStyle = "rgb(2,100,30)";
context.fill(); context.fill();
context.strokeStyle = "#9d4dca";
context.stroke(); context.stroke();
} }
} // 画线
var point = {
//canvas 点击事件 标注 x: clickX,
canvasClickEvent(e) { y: clickY,
};
this.markType? this.points.push(point) : this.points2.push(point)
context.beginPath();
context.lineWidth = 3;
//从起始点开始绘制
let beforePoint = this.markType? this.points: this.points2
context.moveTo(beforePoint[0].x, beforePoint[0].y);
for (var i = 0; i < beforePoint.length; i++) {
context.lineTo(beforePoint[i].x, beforePoint[i].y);
}
context.closePath()
context.fillStyle = "rgb(2,100,30)";
context.fill();
context.strokeStyle = "#9d4dca";
context.stroke();
} }
//判断点位是否在图形区域内 //判断点位是否在图形区域内

Loading…
Cancel
Save