我想用fabric js(或常规js)测量折线。到目前为止,我可以绘制折线,但是它仅测量从鼠标向下到鼠标向上的距离,而不是测量整个线的距离。例如,如果我要绘制一个圆,则其距离将返回“ 0”,因为起点和终点的顶点相同,而我想测量圆的周长。这是代码:
var Calculate = {
lineLength: function (x1, y1, x2, y2) {
return normalizedSize * (Math.sqrt(Math.pow(x2 * 1 - x1 * 1, 2) + Math.pow(y2 * 1 - y1 * 1, 2)));
}
}
Fabric.Polyline.points包含定义折线的所有点。
不用鼠标光标进行测量,而是将新点添加到折线,然后基于点进行计算:
var Calculate = {
lineLength: function (line) {
var distance = 0;
for (var i=0; i < line.points.length - 1; i++) {
var p1 = line.points[i];
var p2 = line.points[i + 1];
distance += normalizedSize * (Math.sqrt(Math.pow(p2.x - p1.x, 2) + Math.pow(p2.y - p1.y, 2)));
}
return distance;
}
}
这应该正确计算线距。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句