重新处理一下顺眼多了
精选
- 拍摄地点: 辽宁省葫芦岛市南票区振兴大街 地点
- 镜头/望远镜: 无
- 相机: 无
- 赤道仪: 无
return false;
});
});
});
});
}
tempImg.onload = function() {
drawAnnotationsWithSize(tempImg.naturalWidth, tempImg.naturalHeight);
};
if (tempImg.complete) {
drawAnnotationsWithSize(tempImg.naturalWidth || tempImg.width, tempImg.naturalHeight || tempImg.height);
}
} catch (e) {
console.error('Error parsing annotations:', e);
}
}
if (img.complete) {
setupCanvas();
} else {
img.onload = setupCanvas;
}
});
if (otherAnnotation.type !== 'ngc' && otherAnnotation.type !== 'ic') return false;
const otherX = otherAnnotation.pixelx * scaleX;
const otherY = otherAnnotation.pixely * scaleY;
const otherRadius = Math.max(20, otherAnnotation.radius * scaleX);
return points.some(point => {
const dx = point.x - otherX;
const dy = point.y - otherY;
return Math.sqrt(dx * dx + dy * dy) <= otherRadius;
});
});
// 检查是否与其他标签重叠
const hasLabelCollision = window.drawnLabels.some(label => {
return points.some(point =>
point.x >= label.left &&
point.x <= label.right &&
point.y >= label.top &&
point.y <= label.bottom
);
});
// 降低碰撞惩罚分数
if (hasLabelCollision) {
pos.score -= 400; // 从2000降到400
}
if (hasCircleCollision) {
pos.score -= 600; // 从3000降到600
}
if (hasSelfCollision) {
pos.score -= 800; // 从4000降到800
}
// 调整距离检查的阈值和惩罚
const distanceToCenter = Math.sqrt(
Math.pow(pos.x - x, 2) +
Math.pow(pos.y - y, 2)
);
if (distanceToCenter < selfRadius * 1.2) { // 从1.5改为1.2
pos.score -= 300; // 从2000降到300
}
// 增加方向奖励
if (pos.angle === 0) pos.score += 150; // 右侧
if (pos.angle === 180) pos.score += 120; // 左侧
if (pos.angle === 90 || pos.angle === 270) pos.score += 80; // 上下
});
// 选择最佳位置(分数最高的位置)
const bestPosition = positions.reduce((best, current) =>
current.score > best.score ? current : best
, positions[0]);
// 降低最低分数阈值,允许更多标注显示
if (bestPosition.score < -1000) { // 从0改为-1000
return;
}
// 绘制文字
ctx.save();
ctx.translate(bestPosition.x, bestPosition.y);
// 处理文字旋转
let finalAngle = bestPosition.angle;
// 确保文字总是正向可读
if (finalAngle > 90 && finalAngle <= 270) {
finalAngle = (finalAngle + 180) % 360;
}
ctx.rotate(finalAngle * Math.PI / 180);
// 文字阴影
ctx.shadowColor = 'rgba(0, 0, 0, 0.8)';
ctx.shadowBlur = 3;
ctx.shadowOffsetX = 1;
ctx.shadowOffsetY = 1;
ctx.fillStyle = '#00ff00';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(text, 0, 0);
ctx.restore();
// 记录已绘制的标签位置(使用旋转后的边界框)
const rotationRad = bestPosition.angle * Math.PI / 180;
const cos = Math.cos(rotationRad);
const sin = Math.sin(rotationRad);
const corners = [
{x: -textWidth/2, y: -textHeight/2},
{x: textWidth/2, y: -textHeight/2},
{x: -textWidth/2, y: textHeight/2},
{x: textWidth/2, y: textHeight/2}
].map(p => ({
x: bestPosition.x + p.x * cos - p.y * sin,
y: bestPosition.y + p.x * sin + p.y * cos
}));
const boundingBox = {
left: Math.min(...corners.map(p => p.x)),
right: Math.max(...corners.map(p => p.x)),
top: Math.min(...corners.map(p => p.y)),
bottom: Math.max(...corners.map(p => p.y))
};
window.drawnLabels.push(boundingBox);
}
});
}
if (tempImg.complete) {
drawAnnotationsWithSize(tempImg.naturalWidth, tempImg.naturalHeight);
} else {
tempImg.onload = function() {
drawAnnotationsWithSize(tempImg.naturalWidth, tempImg.naturalHeight);
};
}
} catch (error) {
console.error('Error parsing annotations:', error);
}
}
// 如果图片还未加载完成,等待加载
if (!img.complete) {
img.onload = function() {
setTimeout(setupCanvas, 100);
};
} else {
setTimeout(setupCanvas, 100);
}
}
});
星友评论