canvas 绘制带箭头的直线

今天记录分享下如何用canvas绘制带有箭头的简单思路。举一个如下图所示的箭头为例来说吧~

要绘制这样一个箭头,第一步我们要先绘制一条线段,然后在线段的末端绘制箭头,需要算出箭头两端所在点的位置,那么这两个点要怎么计算呢?其实不难,就纯数学计算,算出两个点和线段的夹角就可以了,参考下图:

假设线段与坐标系x轴的夹角为α,箭头与线的夹角为θ, 根据三角函数关系很容易就得出如下关系:

// 箭头向上

// 指向左侧

/Lpoint=(x1-r*cos(π-(α+θ), y + r*sin(π-(α+θ))) => (x1+r*cos(α+θ), y + r*sin(α+θ))

/Rpoint=(x1+r*cos(α-θ), y + r*sin(α-θ)) => (x1+r*cos(α-θ), y + r*sin(α-θ))

// 指向右侧

/Lpoint=(x1- r*cos(π - (α+θ)), y + r*sin(π - (α+θ))) => (x1+r*cos(α+θ), y + r*sin(α+θ))

Rpoint=(x1+r*cos(α-θ), y + r*sin(α-θ)) =>  (x1+r*cos(α-θ), y + r*sin(α-θ))

// 箭头向下

指向左侧

Lpoint=(x1-r*cos(π-(α+θ)), y1 - r*sin(π-(α+θ))) => (x1+r*cos(α+θ), y - r*sin(α+θ))

Rpoint=(x1+ r*cos(α-θ), y - r*sin(α-θ)) =>  (x1+r*cos(α-θ), y - r*sin(α-θ))

// 指向右侧

Lpoint=(x1-r*cos(π-(α+θ), y - r*sin(π-(α+θ))) => (x1+r*cos(α+θ), y - r*sin(α+θ))

Rpoint=(x1+r*cos(α-θ), y - r*sin(α-θ)) =>  (x1+r*cos(α-θ), y - r*sin(α-θ))

整合为代码则是:

/**
 * 绘制带有箭头的直线
 * @param fromX/fromY 起点坐标
 * @param toX/toY 终点坐标
 * @param color 线与箭头颜色
 **/
function drawLineArrow(ctx, fromX, fromY, toX, toY, color) {
    const headLenth = 10;//自定义箭头线的长度r
    const theta = 35;//箭头与线的夹角θ
    let arrowX, arrowY;//箭头线终点坐标
    // 计算各角度和对应的箭头终点坐标
    const angle = Math.atan2(fromY - toY, fromX - toX) * 180 / Math.PI;
    const angleLeft = (angle + theta) * Math.PI / 180;
    const angleRight = (angle - theta) * Math.PI / 180;
    const LpointX = headLenth * Math.cos(angleLeft);
    const LpointY = headLenth * Math.sin(angleLeft);
    const RpointX = headLenth * Math.cos(angleRight);
    const RpointY = headLenth * Math.sin(angleRight);
    ctx.beginPath();
    //画直线
    ctx.moveTo(fromX, fromY);
    ctx.lineTo(toX, toY);

    arrowX = toX + LpointX;
    arrowY = toY + LpointY;
    //画上边箭头线
    ctx.moveTo(arrowX, arrowY);
    ctx.lineTo(toX, toY);

    arrowX = toX + RpointX;
    arrowY = toY + RpointY;
    //画下边箭头线
    ctx.lineTo(arrowX, arrowY);
    
    ctx.strokeStyle = color;
    ctx.stroke();
}

到这,绘制以一个箭头好像也不难嘛,在这个基础上做更多的可配置和定义,其实能绘制出更多不形态的箭头。

完整的demo代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>绘制箭头</title>
</head>
<body>
    <canvas id="canvas" width="500" height="500"></canvas>
</body>
<script>
canvas = document.getElementById("canvas");
ctx = canvas.getContext('2d');
drawLineArrow(ctx, 300, 200, 200, 100, "#3D7BFF");
    
/**
 * 绘制带有箭头的直线
 * @param fromX/fromY 起点坐标
 * @param toX/toY 终点坐标
 * @param color 线与箭头颜色
 **/
function drawLineArrow(ctx, fromX, fromY, toX, toY, color) {
    const headLenth = 10;//自定义箭头线的长度r
    const theta = 35;//箭头与线的夹角θ
    let arrowX, arrowY;//箭头线终点坐标
    // 计算各角度和对应的箭头终点坐标
    const angle = Math.atan2(fromY - toY, fromX - toX) * 180 / Math.PI;
    const angleLeft = (angle + theta) * Math.PI / 180;
    const angleRight = (angle - theta) * Math.PI / 180;
    const LpointX = headLenth * Math.cos(angleLeft);
    const LpointY = headLenth * Math.sin(angleLeft);
    const RpointX = headLenth * Math.cos(angleRight);
    const RpointY = headLenth * Math.sin(angleRight);
    ctx.beginPath();
    //画直线
    ctx.moveTo(fromX, fromY);
    ctx.lineTo(toX, toY);

    arrowX = toX + LpointX;
    arrowY = toY + LpointY;
    //画上边箭头线
    ctx.moveTo(arrowX, arrowY);
    ctx.lineTo(toX, toY);

    arrowX = toX + RpointX;
    arrowY = toY + RpointY;
    //画下边箭头线
    ctx.lineTo(arrowX, arrowY);
    
    ctx.strokeStyle = color;
    ctx.stroke();
}
</script>
</html>

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Canvas绘制双线箭头直线,你可以按照以下步骤进行操作: 1. 获取到Canvas元素的引用,并创建一个2D上下文对象。 ```javascript const canvas = document.getElementById("myCanvas"); const ctx = canvas.getContext("2d"); ``` 2. 设置线条的样式,包括线宽和颜色。 ```javascript ctx.lineWidth = 2; ctx.strokeStyle = "black"; ``` 3. 绘制直线的起点和终点。 ```javascript const startX = 50; const startY = 50; const endX = 200; const endY = 50; ctx.beginPath(); ctx.moveTo(startX, startY); ctx.lineTo(endX, endY); ctx.stroke(); ``` 4. 绘制箭头。可以通过绘制三角形来模拟箭头的形状。 ```javascript // 绘制箭头的长度和宽度 const arrowLength = 10; const arrowWidth = 6; // 计算箭头的角度和两侧点的坐标 const angle = Math.atan2(endY - startY, endX - startX); const arrowPoint1X = endX - arrowLength * Math.cos(angle - Math.PI / 6); const arrowPoint1Y = endY - arrowLength * Math.sin(angle - Math.PI / 6); const arrowPoint2X = endX - arrowLength * Math.cos(angle + Math.PI / 6); const arrowPoint2Y = endY - arrowLength * Math.sin(angle + Math.PI / 6); // 绘制箭头 ctx.beginPath(); ctx.moveTo(endX, endY); ctx.lineTo(arrowPoint1X, arrowPoint1Y); ctx.moveTo(endX, endY); ctx.lineTo(arrowPoint2X, arrowPoint2Y); ctx.stroke(); // 绘制箭头底部闭合 ctx.beginPath(); ctx.moveTo(arrowPoint1X, arrowPoint1Y); ctx.lineTo(arrowPoint2X, arrowPoint2Y); ctx.lineTo(endX, endY); ctx.closePath(); ctx.fill(); ``` 完整的代码示例如下: ```javascript const canvas = document.getElementById("myCanvas"); const ctx = canvas.getContext("2d"); ctx.lineWidth = 2; ctx.strokeStyle = "black"; const startX = 50; const startY = 50; const endX = 200; const endY = 50; ctx.beginPath(); ctx.moveTo(startX, startY); ctx.lineTo(endX, endY); ctx.stroke(); const arrowLength = 10; const arrowWidth = 6; const angle = Math.atan2(endY - startY, endX - startX); const arrowPoint1X = endX - arrowLength * Math.cos(angle - Math.PI / 6); const arrowPoint1Y = endY - arrowLength * Math.sin(angle - Math.PI / 6); const arrowPoint2X = endX - arrowLength * Math.cos(angle + Math.PI / 6); const arrowPoint2Y = endY - arrowLength * Math.sin(angle + Math.PI / 6); ctx.beginPath(); ctx.moveTo(endX, endY); ctx.lineTo(arrowPoint1X, arrowPoint1Y); ctx.moveTo(endX, endY); ctx.lineTo(arrowPoint2X, arrowPoint2Y); ctx.stroke(); ctx.beginPath(); ctx.moveTo(arrowPoint1X, arrowPoint1Y); ctx.lineTo(arrowPoint2X, arrowPoint2Y); ctx.lineTo(endX, endY); ctx.closePath(); ctx.fill(); ``` 这样就可以在Canvas绘制双线箭头直线了。记得根据你的需求修改起点和终点的坐标。希望对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值