Canvas绘制箭头

Canvas的 CanvasRenderingContext2D 对象中是没有提供绘制箭头的方法,也就是说,如果我们需要在Canvas中绘制箭头是需要自己封装函数来处理的。

封装函数

// ctx:Canvas绘图环境
// fromX, fromY:起点坐标(也可以换成 p1 ,只不过它是一个数组)
// toX, toY:终点坐标 (也可以换成 p2 ,只不过它是一个数组)
// theta:三角斜边一直线夹角
// headlen:三角斜边长度
// width:箭头线宽度
// color:箭头颜色
function drawArrow(ctx, fromX, fromY, toX, toY,theta = 30,headlen = 10,width = 1,color = '#333') {
    var theta = theta || 30,
        headlen = headlen || 10,
        width = width || 1,
        color = color || '#000',
        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, 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();
}

调用一下

var canvas = document.querySelector('#mycanvas');
var ctx = canvas.getContext('2d');
drawArrow(ctx, 150, 100, 350,100);

效果图:

双向箭头:

function drawArrow(ctx, fromX, fromY, toX, toY,theta,headlen,width,color) {
  var theta = theta || 30,
      headlen = headlen || 10,
      width = width || 1,
      color = color || '#000',
      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.lineTo(fromX, fromY);
  arrowX = fromX - botX;
  arrowY = fromY - botY;
  ctx.lineTo(arrowX, arrowY);
  ctx.moveTo(fromX, fromY);
  ctx.lineTo(toX, toY);
  // Reverse length on the other side
  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();
}

 

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供关于用canvas绘制箭头环形图的相关信息。这是一个很有趣的项目,需要一些基本的HTML5和JavaScript编程知识。在这里我提供一个简单的步骤指南: Step 1: 创建一个Canvas元素 首先,您需要在HTML文件中创建一个Canvas元素。在您的HTML文件中添加以下代码即可: ``` <canvas id="myCanvas" width="400" height="400"></canvas> ``` <br> Step 2: 设置Canvas样式 接下来,您需要设置Canvas的样式。样式可以用CSS来设置,也可以用JavaScript来设置。 ``` // CSS样式设置样例 canvas { background-color: #FFF; } // 使用JavaScript设置样式 var canvas = document.getElementById("myCanvas"); canvas.style.backgroundColor = "#FFF"; ``` <br> Step 3: 画一个环形图 现在,您需要使用JavaScript代码画一个环形图。 ``` var canvas = document.getElementById('myCanvas'); var context = canvas.getContext('2d'); var centerX = canvas.width / 2; var centerY = canvas.height / 2; var radius = 100; context.beginPath(); context.arc(centerX, centerY, radius, 0, 2 * Math.PI, false); context.fillStyle = '#ddd'; context.fill(); context.lineWidth = 10; context.strokeStyle = '#aaa'; context.stroke(); ``` <br> Step 4: 添加箭头 最后,我们需要在环形图上添加一个箭头,这需要一些算法实现。这里我们提供一个简单的算法: ``` // 在一个圆形对象上绘制箭头 function drawArrow(context, start, end, radius) { var angle = Math.atan2(end.y - start.y, end.x - start.x); var x1 = end.x + radius * Math.cos(angle - Math.PI / 8); var y1 = end.y + radius * Math.sin(angle - Math.PI / 8); var x2 = end.x + radius * Math.cos(angle + Math.PI / 8); var y2 = end.y + radius * Math.sin(angle + Math.PI / 8); context.beginPath(); context.moveTo(x1, y1); context.lineTo(end.x, end.y); context.lineTo(x2, y2); context.strokeStyle = '#f00'; context.stroke(); } ``` 然后,我们调用绘制箭头的函数: ``` var start = {x: centerX - radius, y: centerY}; var end = {x: centerX + radius, y: centerY}; drawArrow(context, start, end, 10); ``` 现在,您可以在Canvas中看到一个环形图,并在图形中央看到一个箭头
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值