Html5 canvas 画带箭头的线

 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){

            // arbitrary styling
            ctx.strokeStyle="blue";
            ctx.fillStyle="blue";
            ctx.lineWidth=1;
            
            // draw the line
            ctx.beginPath();
            ctx.moveTo(this.x1,this.y1);
            ctx.lineTo(this.x2,this.y2);
            ctx.stroke();

            // draw the starting arrowhead
            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,startRadians);
            // draw the ending arrowhead
            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,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();
        }

        // create a new line object
        var line=new Line(50,50,250,275);
        // draw the line
        line.drawWithArrowheads(context);
时间: 2024-10-07 05:29:51

Html5 canvas 画带箭头的线的相关文章

CAD里面怎么画带箭头的直线

转自:http://jingyan.baidu.com/article/9113f81b0192e72b3214c709.html?st=2&os=0&bd_page_type=1&net_type=1 当我们需要在CAD里面完成物理方面力的分解的时候就需要画坐标轴等带箭头的直线,但是CAD里面是自己不带箭头直线的,下面将详细的介绍怎么在CAD里面巧妙的完成直线箭头的绘制. 工具/原料 CAD 方法/步骤 打开CAD界面,点击左边的构造线工具.   选中你需要绘制箭头直线的端点,并

HTML5 Canvas 画钟表

画钟表是2D画图的老生常谈,我也不能免俗弄了一个.代码如下: <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <head> <title>钟表</title> </head> <body onloa

html5 canvas画饼

1. [图片] lxdpie.jpg ?2. [文件] lqdpie.html ~ 801B     下载(7) <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html lang="ru"><head>  <title></title>  <met

html5 canvas画五角星(美国队长)

画一波五角星: 美国队长图标 原理:  (1)根据五角星的顶点,外顶点5个,内顶点5个,分成内外圆 (2)由三角函数可表示出每个顶点的位置 (3)利用canvas的lineTo()接口画图 上代码: 1 <!DOCTYPE html> 2 <html lang="en"> 3 4 <head> 5 <meta charset="UTF-8"> 6 <title>Captain America</tit

MFC画带箭头的直线

构造一个函数,是在startPoint,endPoint间画一条带箭头的线段: void CTry1View::DrawLine(POINT startPoint, POINT endPoint) { CClientDC dc(this); dc.MoveTo(startPoint); dc.LineTo(endPoint); double PI = 3.1415926; double t=PI/4; //箭头与直线夹角 double l=0.2; //箭头边长度占直线长度的百分比 POINT

html5 canvas 画hello ketty

<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8"> <title>Html5 - 小猫咪</title> <link rel="stylesheet" type="text/css" href="css/common/reset.css"/> &

html5 canvas 画时钟

<!DOCTYPE HTML> <html> <body> <canvas id="myCanvas" width="400" height="400" style="border:1px solid"></canvas> <script type="text/javascript">     // get the canvas    

html5——canvas画直线

<html> <head> <title>canvas demo</title> </head> <body> <canvas id="mycanvas" width="500px" height="500px" ></canvas> <script type="text/javascript"> var mycanva

MFC中如何画带实心箭头的直线

工作中遇到话流程图的项目,需要画带箭头的直线,经过摸索,解决:思路如下: (1) 两个点(p1,p2)确定一个直线,以直线的一个端点(假设p2)为原点,设定一个角度 (2)以P2为原点得到向量P2P1(P),向量P旋转theta角得到向量P1,向量P旋转-theta角得到向量P2 (3)伸缩向量至制定长度,平移变量到直线的末端 (4)现在已经有3个点了,画线就可 具体代码如下: void CworkflowDlg::DrawLine(CPoint p1, CPoint p2) { CClient