canvas画箭头demo
效果图:
代码:
<!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的更多相关文章
- android 使用Canvas画箭头
		
public class MyCanvas extends View{ private Canvas myCanvas; private Paint myPaint=new Pai ...
 - canvas 画圈 demo
		
html代码: <canvas id="clickCanvas2" width="180" height="180" data-to ...
 - html5 canvas画流程图
		
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
 - HTML5 之Canvas 绘制时钟 Demo
		
<!DOCTYPE html> <html> <head> <title>Canvas 之 时钟 Demo</title> <!--简 ...
 - 深夜,用canvas画一个时钟
		
深夜,用canvas画一个时钟 查看demo 这几天准备阿里巴巴的笔试,可以说已经是心力交瘁,自从阿里和蘑菇街的内推被刷掉之后,开始越来越怀疑起自己的能力来,虽然这点打击应该是微不足道的.毕竟校招在刚 ...
 - WPF画箭头
		
简介 参考Using WPF to Visualize a Graph with Circular Dependencies的基础上写了一个WPF画箭头的库. 效果图如下: 使用的XAML代码如下: ...
 - 使用javascript和canvas画月半弯
		
使用javascript和canvas画月半弯,月半弯好浪漫!浏览器须支持html5 查看效果:http://keleyi.com/a/bjad/8xqdm0r2.htm 以下是代码: <!do ...
 - 踩个猴尾不容易啊  Canvas画个猴子
		
踩个猴尾不容易啊 Canvas画个猴子 <!DOCTYPE html> <html> <head> <meta charset="UTF-8&qu ...
 - canvas画随机闪烁的星星
		
canvas画一颗星星: 规则的星星有内切圆和外切圆,每两个点之间的角度是固定的,因此可得到星星的每个点的坐标,画出星星. function drawStars(x,y,radius1,radius2 ...
 
随机推荐
- 四十七.iptables防火墙 filter表控制 扩展匹配 nat表典型应用
			
1.iptables基本管理 关闭firewalld,开启iptables服务 查看防火墙规则 追加.插入防火墙规则 删除.清空防火墙规则 1.1 关闭firewalld,启动iptables服务 ...
 - TensorFlow(二):基本概念以及练习
			
一:基本概念 1.使用图(graphs)来表示计算任务 2.在被称之为会话(Session)的上下文(context)中执行图 3.使用tensor表示数据 4.通过变量(Variable)维护状态 ...
 - 好久木来了,一直忙于项目(加懒惰),今天讲讲vuecli3.0的使用
			
vue更新换代很快,马上vue都要出3.0了,这是一个巨大的变革,不过今天讲的是vuecli3.0,里面使用的vue仍然是2的,所有可以放心大胆使用. Vue CLI 是一个基于 Vue.js 进行快 ...
 - (转)kafka 详解
			
kafka入门:简介.使用场景.设计原理.主要配置及集群搭建(转) 问题导读: 1.zookeeper在kafka的作用是什么? 2.kafka中几乎不允许对消息进行"随机读写"的 ...
 - shell 脚本同时对远程多台机器执行命令
			
脚本1:需要机器之间免密 ssh-copy-id [-i [identity_file]] [user@]machine #!/bin/bash # ------------------------- ...
 - Synchronized 原理
			
1.同步代码块: 反编译结果: monitorenter : 每个对象有一个监视器锁(monitor).当monitor被占用时就会处于锁定状态,线程执行monitorenter指令时尝试获取moni ...
 - 2018-2019-2 网络对抗技术 20165231 Exp 8 Web基础
			
实验内容 (1).Web前端HTML(0.5分) 能正常安装.启停Apache.理解HTML,理解表单,理解GET与POST方法,编写一个含有表单的HTML. (2).Web前端javascipt(0 ...
 - Qt之模型/视图(自定义风格)
			
Qt之模型/视图(自定义风格) 关于自定义风格是针对视图与委托而言的,使用事件与QSS都可以进行处理,今天关于美化的细节讲解一下. 先看下图: 先撇开界面的美观性(萝卜青菜,各有所爱),就现有的这些风 ...
 - GDPR全文翻译(一)
			
General Data Protection Regulation <一般数据保护法案>全文翻译(一) 编者按 2016年4月14日,欧洲议会投票通过了商讨四年的<一般数据保护法案 ...
 - Drawable: getIntrinsicWidth()和getIntrinsicHeight()方法的使用误区
			
经常会使用上述两个API来获取ImageView中显示图片的大小,但是在某些情况下,这两个API返回的大小可能与原图的大小不一致,比如原图大小是72*72,分别把原图放置在xhdpi,xxhdpi,x ...