Html5 canvas 画带箭头的线

电脑杂谈  发布时间:2019-05-19 08:20:17  来源:网络整理

js canvas 画箭头_html5 canvas 画箭头_html5 canvas 画箭头

html5 canvas 画箭头

推荐:html5学习canvas图像处理

html5 canvas 画箭头_js canvas 画箭头_html5 canvas 画箭头

上一篇博客主要学习了关于canvas的图性处理相关用法,这里所有的图形都是通过计算机进行绘制的,今天学些使用canvas来绘制以后的图像以及图像的部分处理操作。html5 canvas 画箭头 d

html5 canvas 画箭头_js canvas 画箭头_html5 canvas 画箭头

2019阿里云全部产品优惠券(新购或升级都可以使用,强烈推荐)

html5 canvas 画箭头_html5 canvas 画箭头_js canvas 画箭头

领取地址:https://promotion.aliyun.com/ntms/yunparter/invite.html

html5 canvas 画箭头_js canvas 画箭头_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);

推荐:HTML5 canvas 小实例

原文:HTML5 canvas 小实例 源代码下载地址: HTML5 canvas 小实例 。


本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/jisuanjixue/article-99766-1.html

    相关阅读
      发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

      • 俞灏
        俞灏

        既然这样挑衅

      • 孙义斐
        孙义斐

        不过他这招如果用在另外一个大国面前应该会凑效

      热点图片
      拼命载入中...