javascript - 画一个箭头 HummusJS

标签 javascript node.js hummus.js

如何在hummusJS 中绘制箭头? 我正在使用鹰嘴 bean 泥在 pdf 中绘图。我需要在pdf中画一个箭头。我能够划清界限。但是我怎样才能画一个箭头呢? 我尝试了以下

if(x2 > y2)
            {
                a1 = x2-5;
                b1 = y2-5;
                a2 = x2-5;
                b2 = y2+5;
                a3 = x2+5;
                b3 = y2;
            }
            else
            {
                a1 = x2-5;
                b1 = y2+5;
                a2 = x2+5;
                b2 = y2+5;
                a3 = x2;
                b3 = y2-5;                
            }

 cxt.drawPath(a1,b1,a2,b2,a3,b3,{type: 'fill',
            color: '#000000'})

我也试过这样

      var d =5;
            a1 = x2-d*Math.sin(45);
            b1 = y2-d*Math.cos(45);
            a2 = x2+d*Math.sin(45);
            b2 = y2+d*Math.cos(45);
cxt.drawPath(x2,y2,a1,b1,{type: 'fill',
                color: '#000000'})
cxt.drawPath(x2,y2,a2,b2,{type: 'fill',
                color: '#000000'})

但这不是在正确的位置绘制箭头 这是图片enter image description here

最佳答案

根据以下问题的回答:How to calculate the coordinates of a arrowhead based on the arrow? 特别是公认的:https://stackoverflow.com/a/10316601/1713942

您可以使用矢量算术计算所需的坐标:

function arrowhead(A, B) {
    var h = 10 * Math.sqrt(3);
    var w = 10;
    var d = {x: B.x - A.x, y: B.y - A.y};
    var length = Math.sqrt(d.x*d.x + d.y*d.y);
    var U = {x: d.x/length, y: d.y/length};
    var V = {x: -U.y, y: U.x};
    return [
        {x: B.x - U.x*h + V.x*w, y: B.y - U.y*h + V.y*w},
        {x: B.x - U.x*h - V.x*w, y: B.y - U.y*h - V.y*w}
    ];
}

hw 确定长度和半宽。该函数返回两个点:箭头的两端。

用法示例:

var startPoint = {x:100, y:100};
var endPoint = {x:200, y:200};

var arrowPoint = arrowhead(startPoint, endPoint);

// draw line
cxt.drawPath(startPoint.x, startPoint.y, endPoint.x, endPoint.y);

// draw arrow head
cxt.drawPath(endPoint.x, endPoint.y, arrowPoint[0].x, arrowPoint[0].y, arrowPoint[1].x, arrowPoint[1].y, endPoint.x, endPoint.y, {type: 'fill', color: '#000000'});

关于javascript - 画一个箭头 HummusJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50759391/

相关文章:

node.js -/lib64/libc.so.6 : version `GLIBC_2.14' not found. 为什么会出现此错误?

javascript - 如何用php或js包裹图形纹理?

JavaScript 正则表达式不工作

javascript - Safari 显示的字体与其他浏览器不同 - 为特定浏览器设置特定字体系列 - 如何更改 Safari 的字体平滑

javascript - 按钮互相改变

javascript - 如何在 Express Route 中捕获不定数量的参数?

node.js - 循环合并 PDF 文件 ( Hummus-Recipe )

javascript - HummusJS - 在 JavaScript 中将 HTML 页面转换为 PDF

node.js - 将 session 存储在express(node.js)和neo4j数据库上

node.js - Electron 关闭按钮不起作用