0
点赞
收藏
分享

微信扫一扫

根据给定的坐标和颜色绘制一条直线

梦幻之云 2022-04-23 阅读 62
/**
  * 根据给定的坐标和颜色绘制一条直线
  * @param {CanvasRenderingContext2D} ctx - 渲染上下文.
  * @param {number} fx - 起点横坐标.
  * @param {number} fy - 起点纵坐标.
  * @param {number} tx - 终点横坐标.
  * @param {number} ty - 终点纵坐标.
  * @param {string} color - 颜色.
  */
function drawLine(ctx, fx, fy, tx, ty, color) {
    ctx.fillStyle = color;
    ctx.translate(fx, fy);
    ctx.rotate(Math.atan2(ty - fy, tx - fx));
    ctx.fillRect(0, 0, Math.sqrt(Math.pow(tx - fx, 2) + Math.pow(ty - fy, 2)), 1);
    ctx.setTransform(1, 0, 0, 1, 0, 0);
}
举报

相关推荐

绘制一条流动的线

0 条评论