html画布直线代码,在html页面上绘制线条

window.addEventListener("load",function(){

function pow2(n){

return n*n;

}

var p1 = {

id:"p1",

x:150,

y:50

};

var p2 = {

id:"p2",

x:300,

y:250

};

var select = null;

function getAngle(){

var dist = Math.sqrt(pow2(p1.x-p2.x)+pow2(p1.y-p2.y));

var l = document.getElementById("line");

var cos = (p2.x-p1.x)/Math.sqrt(pow2(p2.x-p1.x)+pow2(p2.y-p1.y));

var behind = p1.x < p2.x;

var higher = p1.y > p2.y;

l.style.width = (dist*2)+"px";

l.style.left = (p1.x-dist)+"px";

l.style.top = (p1.y)+"px";

l.style.transform = "rotateZ("+(higher?-1:1)*Math.acos(cos)*(180/Math.PI)+"deg)";

}

var down = false

document.addEventListener("mousemove",function(e){

if(select){

select.x = e.pageX;

select.y = e.pageY;

console.log(p1);

var p = document.getElementById(select.id);

p.style.left = (select.x-5)+"px";

p.style.top = (select.y-5)+"px";

getAngle();

}

});

document.addEventListener("mouseup",function(e){

if(!select)

select = p1;

else if(select == p1)

select = p2;

else

select = null;

});

document.addEventListener("mousedown",function(e){

down = true;

});

});


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值