效果图:

代码:

<!DOCTYPE html>
<html>
<title>canvas画箭头demo</title>
<body>
<canvas id="canvas" width="200" height="200" style="border:1px dotted #d3d3d3;"></canvas>
<script>
var canvas=document.getElementById("canvas");
var context=canvas.getContext("2d"); function Line(x1,y1,x2,y2){
this.x1=x1;
this.y1=y1;
this.x2=x2;
this.y2=y2;
}
Line.prototype.drawWithArrowheads=function(ctx,type){ // 设置箭头样式
ctx.strokeStyle="black";
ctx.fillStyle="black";
ctx.lineWidth=3; // 画线
ctx.beginPath();
ctx.moveTo(this.x1,this.y1);
ctx.lineTo(this.x2,this.y2);
ctx.stroke(); if(type == 1){
// 画向上的箭头
var startRadians=Math.atan((this.y2-this.y1)/(this.x2-this.x1));
startRadians+=((this.x2>this.x1)?-90:90)*Math.PI/-180;
this.drawArrowhead(ctx,this.x1,this.y1-5,startRadians);
}else{
// 画向下的箭头
var endRadians=Math.atan((this.y2-this.y1)/(this.x2-this.x1));
endRadians+=((this.x2>this.x1)?90:-90)*Math.PI/-180;
this.drawArrowhead(ctx,this.x2,this.y2+5,endRadians); }
}
Line.prototype.drawArrowhead=function(ctx,x,y,radians){
ctx.save();
ctx.beginPath();
ctx.translate(x,y);
ctx.rotate(radians);
ctx.moveTo(0,0);
ctx.lineTo(5,20);
ctx.lineTo(-5,20);
ctx.closePath();
ctx.restore();
ctx.fill();
} // 创建一个新的箭头对象
var line=new Line(50,50,50,155);
var line1=new Line(60,50,60,155);
// 第二个参数为1则箭头向上,不为1则箭头向下
line.drawWithArrowheads(context,1);
line1.drawWithArrowheads(context,2);
</script>
</body>
</html>

canvas画箭头demo的更多相关文章

  1. android 使用Canvas画箭头

    public class MyCanvas extends View{        private Canvas myCanvas;    private Paint myPaint=new Pai ...

  2. canvas 画圈 demo

    html代码: <canvas id="clickCanvas2"  width="180" height="180" data-to ...

  3. html5 canvas画流程图

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  4. HTML5 之Canvas 绘制时钟 Demo

    <!DOCTYPE html> <html> <head> <title>Canvas 之 时钟 Demo</title> <!--简 ...

  5. 深夜,用canvas画一个时钟

    深夜,用canvas画一个时钟 查看demo 这几天准备阿里巴巴的笔试,可以说已经是心力交瘁,自从阿里和蘑菇街的内推被刷掉之后,开始越来越怀疑起自己的能力来,虽然这点打击应该是微不足道的.毕竟校招在刚 ...

  6. WPF画箭头

    简介 参考Using WPF to Visualize a Graph with Circular Dependencies的基础上写了一个WPF画箭头的库. 效果图如下: 使用的XAML代码如下: ...

  7. 使用javascript和canvas画月半弯

    使用javascript和canvas画月半弯,月半弯好浪漫!浏览器须支持html5 查看效果:http://keleyi.com/a/bjad/8xqdm0r2.htm 以下是代码: <!do ...

  8. 踩个猴尾不容易啊 Canvas画个猴子

    踩个猴尾不容易啊  Canvas画个猴子 <!DOCTYPE html> <html> <head> <meta charset="UTF-8&qu ...

  9. canvas画随机闪烁的星星

    canvas画一颗星星: 规则的星星有内切圆和外切圆,每两个点之间的角度是固定的,因此可得到星星的每个点的坐标,画出星星. function drawStars(x,y,radius1,radius2 ...

随机推荐

  1. package.json 与 package-lock.json 的区别

    根据官方文档,这个package-lock.json 是在 `npm install`时候生成一份文件,用以记录当前状态下实际安装的各个npm package的具体来源和版本号. 它有什么用呢?因为n ...

  2. notepad++修改背景色

  3. 支持utf8的str_split函数

    <?php header("Content-type: text/html; charset=utf-8"); /** * 按字节数对字符串进行分片 * @param $st ...

  4. MySQL 5.7:聊聊sql_mode

    1.sql_mode=only_full_group_by 导致的语法错误问题 MySQLSyntaxErrorException Caused by: com.mysql.jdbc.exceptio ...

  5. 如何从DOS命令窗口进行复制粘贴

    在DOS窗口没有右键复制按钮,那我们如果想要进行复制粘贴该如何操作呢?本篇就来说明一下这个操作流程. 工具/原料   WIN7系统 方法/步骤     按“WIN+R”调出运行页面,在其中输入CMD然 ...

  6. Cesium Workshop

    参考资料: https://cesiumjs.org/tutorials/Cesium-Workshop/ https://github.com/geoadmin/workshop-cesium3d ...

  7. Linux中 mkdir 创建文件夹命令

    语法 mkdir (选项)(参数) 选项 -Z:设置安全上下文,当使用SELinux时有效: -m<目标属性>或--mode<目标属性>建立目录的同时设置目录的权限: -p或- ...

  8. pgpool-II 高可用搭建

    pgpool-II主备流复制的架设1.环境 OS: CentOS release 6.4 (Final)DB: postgresql 9.3.6pgpool服务器: pgpool 172.16.0.2 ...

  9. PHP使用MongoDB存储经纬度,查询距离

    https://blog.csdn.net/qq_40012295/article/details/84861466 https://docs.mongodb.com/manual/reference ...

  10. Leetcode: Longest Palindromic Substring && Summary: Palindrome

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...