javascript - 垂直于另一条线的 HTML canvas 绘图线

标签 javascript html canvas

我正在使用 HTML canvas 绘制如下图所示的线,但是这条线的两侧有一条边。

enter image description here

如图所示,两条边与主线不垂直
我尝试了以下解决方案,但没有成功:
* 旋转边缘线,但旋转会将它们从原始位置转换
* 找到主线的 Angular ,然后相对于线绘制线,但这个解决方案并不容易实现(很可能我实现错误)。

这是我的代码,但它总是绘制垂直边缘:

<!DOCTYPE html>
<html>
<body>

<canvas id="myCanvas" width="200" height="200" style="border:1px solid #d3d3d3;">

<script>

var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");

var x1 = 100;
var x2 = 150;
var y1 = 50;
var y2 = 120;

ctx.beginPath();
ctx.strokeStyle = "purple";  // Purple path
ctx.moveTo(x1,y1);
ctx.lineTo(x2,y2);            
ctx.stroke();  // Draw it

ctx.beginPath();
ctx.moveTo(x1,y1);
ctx.lineTo(x1,y1+10);
ctx.stroke();


ctx.restore();
ctx.beginPath();
ctx.moveTo(x1,y1);
ctx.lineTo(x1,(y1-10));
ctx.stroke();


ctx.beginPath();
ctx.moveTo(x2,y2);
ctx.lineTo(x2,y2+10);
ctx.stroke();


ctx.restore();
ctx.beginPath();
ctx.moveTo(x2,y2);
ctx.lineTo(x2,(y2-10));
ctx.stroke();


</script>
</body>
</html>

任何人都可以帮我旋转两条边缘线,使其垂直于主线。 谢谢。

最佳答案

将任何二维向量旋转 90 度

如果您还记得二维向量的旋转 90 度规则,垂直线就很容易计算。

向量 {x,y} 可以顺时针 {-y,x} 或逆时针 {y,-x} 旋转 90 度。交换 x 和 y 并取反顺时针方向的 y 或逆时针方向的 x 取反

因此对于线段 x1,y1x2,y2 转换为向量,将其归一化矢量并旋转90度,如下所示

function getPerpOfLine(x1,y1,x2,y2){ // the two points can not be the same
    var nx = x2 - x1;  // as vector
    var ny = y2 - y1;
    const len = Math.sqrt(nx * nx + ny * ny);  // length of line
    nx /= len;  // make one unit long
    ny /= len;  // which we call normalising a vector
    return [-ny, nx]; // return the normal  rotated 90 deg
}

然后说你想在线段的两端画一条10像素的线

const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
document.body.append(canvas);
ctx.strokeStyle = "black";
ctx.lineJoin = ctx.lineCap = "round";
ctx.lineWidth = 3;

// the line segment
const x1 = 40, y1 = 40, x2 = 260, y2 = 110;
const endLen = 10; // length of end lines

var px = y1 - y2; // as vector at 90 deg to the line
var py = x2 - x1;
const len = endLen / Math.hypot(px, py);
px *= len;  // make leng 10 pixels
py *= len; 

// draw line the start cap and end cap.
ctx.beginPath();

ctx.lineTo(x1, y1);   // the line start
ctx.lineTo(x2, y2);
ctx.moveTo(x1 + px, y1 + py); // the start perp line
ctx.lineTo(x1 - px, y1 - py);
ctx.moveTo(x2 + px, y2 + py); // the end perp line
ctx.lineTo(x2 - px, y2 - py);
ctx.stroke();

更新

沿线呈现内容的简单解决方案,使用相同的 90 度规则。

还有一种使用相同矢量旋转进行渲染的替代方法,但不是通过矢量乘法设置垂直轴,而是将变换的 y 轴设置为与沿线的 x 轴成 90 度 Angular 。将原点设置为线的开头,您只需相对于线进行渲染即可。

setTransformToLine(x1, y1, x2, y2)

下面的函数会将 Canvas 变换设置为沿线

// Set 2D context  current transform along the line x1,y1,x2,y2 and origin to
// start of line. y Axis is rotated clockwise 90 from the line.
// Returns the line length as that is most frequently required when
// using the method saving some time.
function setTransformToLine(x1, y1, x2, y2) {
  const vx = x2 - x1;   // get the line as vector
  const vy = y2 - y1;
  const len = Math.hypot(vx, vy); // For <= IE11 use Math.sqrt(vx * vx + vy * vy)
  const nx = vx / len; // Normalise the line vector. Making it one
  const ny = vy / len; // pixel long. This sets the scale

  // The transform is the normalised line vector for x axis, y at 90 deg 
  // and origin at line start
  ctx.setTransform(nx, ny, -ny, nx, x1, y1); // set transform

  return len;
}

如何使用

此示例展示了如何使用转换来执行同一行,但也添加了注释长度。

const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
document.body.append(canvas);
ctx.strokeStyle = "black";
ctx.lineJoin = ctx.lineCap = "round";
ctx.lineWidth = 3;
ctx.font = "16px arial";
ctx.textBaseline = "middle";
ctx.textAlign = "center";

const x1 = 40, y1 = 40, x2 = 260, y2 = 110;
const endLen = 10; 


function setTransformToLine(x1, y1, x2, y2) {
  const vx = x2 - x1; 
  const vy = y2 - y1;
  const len = Math.hypot(vx, vy); 
  const nx = vx / len; 
  const ny = vy / len;
  ctx.setTransform(nx, ny, -ny, nx, x1, y1);
  return len;
}

// Set the transform along the line. Keep the line length
// line len is need to get the x coord of the end of the line
const lineLen = setTransformToLine(x1, y1, x2, y2);

const lineLenStr = Math.round(lineLen) + "px";
const textWidth = ctx.measureText(lineLenStr).width;

const rlen = lineLen - textWidth - 16; // find the remaining line len after removing space for text

// Rendering is done in line local coordinates
// line is from (0,0) to (lineLen,0)

// Now draw the line the ends first and then along the line leaving gap for text
ctx.beginPath();
ctx.lineTo(0, -endLen);             // start perp line
ctx.lineTo(0,  endLen); 

ctx.moveTo(lineLen, -endLen);       // end of line is at lineLen
ctx.lineTo(lineLen,  endLen); 

ctx.moveTo(0,0);                    // line start segment
ctx.lineTo(rlen / 2, 0);

ctx.moveTo(lineLen - rlen / 2,0);   // line end segment
ctx.lineTo(lineLen, 0);

ctx.stroke(); // render it.

// now add text at the line center
ctx.fillText(lineLenStr, lineLen / 2, 0);

// To restore the transform to its default use identity matrix
ctx.setTransform(1, 0, 0, 1, 0, 0);

关于javascript - 垂直于另一条线的 HTML canvas 绘图线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46513786/

相关文章:

javascript - 如何从脚本标签和/或控制台中抓取值

javascript - 如何在异步操作中指示 "doneness"

python - 在 Django 中实现 HTML 和 CSS

javascript - 如何在 Chart.js 上为 Canvas 设置固定大小

javascript - 将 Canvas 元素下载到图像

javascript - 使用 jQuery 对数组中的元素进行动画处理

javascript - Fetch API 返回 415(不支持的媒体类型)

javascript - 我正在玩 Canvas 。我怎样才能让它发挥作用?

javascript - AngularJS:带有 ng-options 参数的指令中的下拉菜单

javascript - 为什么google map用Canvas做距离测量而不是SVG?