canvas根据角度画射线

10 篇文章 0 订阅
2 篇文章 0 订阅

            let ctx_line = canvas.getContext("2d"); 

            let centerX = canvas.width/2; //中心点
            let centerY = canvas.height/2; //中心点
            let arc_radius = 100;  //半径

            //画射线
            let x1,y1;
            arc_radius+=10;
            ctx_line.strokeStyle = "#ffffff";
            ctx_line.lineWidth = 6;
            ctx_line.beginPath();
            //定好中心点
            // 角度转弧度 = 角度/180*Math.PI
            x1 = centerX +  arc_radius * Math.cos(30 * Math.PI / 180);
            y1 = centerY +arc_radius * Math.sin(30 *Math.PI /180);
            ctx_line.moveTo(centerX,centerY);
            ctx_line.lineTo(x1,y1);

            x1 = centerX +  arc_radius * Math.cos(60 * Math.PI / 180);
            y1 = centerY +arc_radius * Math.sin(60 *Math.PI /180);
            ctx_line.moveTo(centerX,centerY);
            ctx_line.lineTo(x1,y1);

            x1 = centerX +  arc_radius * Math.cos(90 * Math.PI / 180);
            y1 = centerY +arc_radius * Math.sin(90 *Math.PI /180);
            ctx_line.moveTo(centerX,centerY);
            ctx_line.lineTo(x1,y1);

            x1 = centerX +  arc_radius * Math.cos(120 * Math.PI / 180);
            y1 = centerY +arc_radius * Math.sin(120 *Math.PI /180);
            ctx_line.moveTo(centerX,centerY);
            ctx_line.lineTo(x1,y1);

            x1 = centerX +  arc_radius * Math.cos(150 * Math.PI / 180);
            y1 = centerY +arc_radius * Math.sin(150 *Math.PI /180);
            ctx_line.moveTo(centerX,centerY);
            ctx_line.lineTo(x1,y1);

            x1 = centerX +  arc_radius * Math.cos(180 * Math.PI / 180);
            y1 = centerY +arc_radius * Math.sin(180 *Math.PI /180);
            ctx_line.moveTo(centerX,centerY);
            ctx_line.lineTo(x1,y1);

            x1 = centerX +  arc_radius * Math.cos(210 * Math.PI / 180);
            y1 = centerY +arc_radius * Math.sin(210 *Math.PI /180);
            ctx_line.moveTo(centerX,centerY);
            ctx_line.lineTo(x1,y1);

            x1 = centerX +  arc_radius * Math.cos(240 * Math.PI / 180);
            y1 = centerY +arc_radius * Math.sin(240 *Math.PI /180);
            ctx_line.moveTo(centerX,centerY);
            ctx_line.lineTo(x1,y1);

            x1 = centerX +  arc_radius * Math.cos(270 * Math.PI / 180);
            y1 = centerY +arc_radius * Math.sin(270 *Math.PI /180);
            ctx_line.moveTo(centerX,centerY);
            ctx_line.lineTo(x1,y1);

            x1 = centerX +  arc_radius * Math.cos(300 * Math.PI / 180);
            y1 = centerY +arc_radius * Math.sin(300 *Math.PI /180);
            ctx_line.moveTo(centerX,centerY);
            ctx_line.lineTo(x1,y1);

            x1 = centerX +  arc_radius * Math.cos(330 * Math.PI / 180);
            y1 = centerY +arc_radius * Math.sin(330 *Math.PI /180);
            ctx_line.moveTo(centerX,centerY);
            ctx_line.lineTo(x1,y1);

            x1 = centerX +  arc_radius * Math.cos(360 * Math.PI / 180);
            y1 = centerY +arc_radius * Math.sin(360 *Math.PI /180);
            ctx_line.moveTo(centerX,centerY);
            ctx_line.lineTo(x1,y1);

            ctx_line.stroke();

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值