【问题标题】:How to draw an arrowhead with proper coordinates in the canvas如何在画布中绘制具有适当坐标的箭头
【发布时间】:2018-08-20 02:41:34
【问题描述】:

我已经使用下面的代码行绘制了曲线,我需要绘制一个箭头。为此,我需要绘制 2 条带某个角度的线并将其旋转某个角度。画起来非常混乱。我正在关注为箭头提供的link 中的帖子。

.html

<canvas id = "canvas" width = "100px" height = "120px"></canvas>

.ts

  arrow({ x: 10, y: 10 }, { x: 100, y: 140 }, 15); //function called on reload.

  function arrow(p1, p2, size) {
  var angle = Math.atan2((p2.y - p1.y), (p2.x - p1.x));

  //curve line
  ctx.strokeStyle = 'white';      
  ctx.beginPath();      
  ctx.lineWidth=3;     
  ctx.moveTo(40,0);     
  ctx.bezierCurveTo(30, 0, -70, 75, 100, 150);
  ctx.lineTo(100,120)         
  ctx.stroke();

 //to draw a triangle ??

}

【问题讨论】:

    标签: javascript html css canvas


    【解决方案1】:

    我试着看起来像

    arrow({ x: 10, y: 10 }, { x: 100, y: 140 }, 15); //function called on reload.
    
      function arrow(p1, p2, size) {
      var angle = Math.atan2((p2.y - p1.y), (p2.x - p1.x));
    var canvas = document.getElementById("canvas");
    var ctx = canvas.getContext("2d");
      //curve line
    ctx.fillStyle = "";
    ctx.fillRect(0,0,200,200);
      ctx.strokeStyle = 'white';      
      ctx.beginPath();      
      ctx.lineWidth=3;     
      ctx.moveTo(40,20);     
      ctx.bezierCurveTo(30,40, -0,110, 100, 149.5);
      ctx.moveTo(100,150.6);
      ctx.lineTo(82,133);
    ctx.stroke();
      ctx.moveTo(100,149.7);
      ctx.lineTo(76,146);
    ctx.stroke();
     //to draw a triangle ??
      }
    &lt;canvas id = "canvas" width = "150px" height = "300px"&gt;&lt;/canvas&gt;

    【讨论】:

    • 曲线与我的截图不同。画布尺寸也不匹配。您可以编写代码仅绘制箭头并将其放置在我的曲线底部的位置吗?我想我可以接受那个答案
    • 如果你能在链接中看到 aikon 写的 ans 使用了 javascript 函数
    • 我最后能做的就是抱歉
    猜你喜欢
    • 2010-10-22
    • 2015-01-04
    • 2012-07-15
    • 2020-06-25
    • 2015-12-08
    • 2017-02-26
    • 2019-08-14
    • 2020-07-07
    • 2014-12-26
    相关资源
    最近更新 更多