canvas使用js画箭头

/**

* @param {Object} ctx    canvas对象
* @param {Object} fromX  起点x
* @param {Object} fromY  起点y
* @param {Object} toX    终点x
* @param {Object} toY    终点y
* @param {Object} theta  箭头夹角
* @param {Object} headlen 斜边长度
* @param {Object} width 箭头宽度
* @param {Object} color 颜色
*/
function drawArrow(ctx, fromX, fromY, toX, toY,theta,headlen,width,color) { 
theta = typeof(theta) != 'undefined' ? theta : 30; 
headlen = typeof(theta) != 'undefined' ? headlen : 10; 
width = typeof(width) != 'undefined' ? width : 1;
color = typeof(color) != 'color' ? color : '#000'; 
// 计算各角度和对应的P2,P3坐标 
var angle = Math.atan2(fromY - toY, fromX - toX) * 180 / Math.PI, 
    angle1 = (angle + theta) * Math.PI / 180, 
    angle2 = (angle - theta) * Math.PI / 180, 
    topX = headlen * Math.cos(angle1), 
    topY = headlen * Math.sin(angle1), 
    botX = headlen * Math.cos(angle2), 
    botY = headlen * Math.sin(angle2); 
ctx.save(); 
ctx.beginPath(); 
var arrowX = fromX - topX, arrowY = fromY - topY;
    ctx.moveTo(arrowX, arrowY); 
    ctx.moveTo(fromX, fromY); 
    ctx.lineTo(toX, toY); 
    arrowX = toX + topX; 
    arrowY = toY + topY; 
    ctx.moveTo(arrowX, arrowY); 
    ctx.lineTo(toX, toY); 
    arrowX = toX + botX; 
    arrowY = toY + botY; 
    ctx.lineTo(arrowX, arrowY); 
    ctx.strokeStyle = color; 
    ctx.lineWidth = width; 
    ctx.stroke(); 
    ctx.restore(); 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值