jeudi 23 octobre 2014

Javascript draw line script not working


Vote count:

0




Tested in Firefox and Chrome. Script found on SO in an old Q/A. Script claimed to be working, doesn't work for me. Would rather use pure JS to solve this problem instead of HTML5 canvas to support older browsers.


jsfiddle: http://ift.tt/1tfr2nV



<html>
<head>
<script>

window.onload = function() {
DrawLine(1,1,100,100);
}

function DrawLine(x1, y1, x2, y2){

if(y1 < y2){
var pom = y1;
y1 = y2;
y2 = pom;
pom = x1;
x1 = x2;
x2 = pom;
}

var a = Math.abs(x1-x2);
var b = Math.abs(y1-y2);
var c;
var sx = (x1+x2)/2 ;
var sy = (y1+y2)/2 ;
var width = Math.sqrt(a*a + b*b ) ;
var x = sx - width/2;
var y = sy;

a = width / 2;

c = Math.abs(sx-x);

b = Math.sqrt(Math.abs(x1-x)*Math.abs(x1-x)+Math.abs(y1-y)*Math.abs(y1-y) );

var cosb = (b*b - a*a - c*c) / (2*a*c);
var rad = Math.acos(cosb);
var deg = (rad*180)/Math.PI

htmlns = "http://ift.tt/lH0Osb";
div = document.createElementNS(htmlns, "div");
div.setAttribute('style','border:1px solid black;width:'+width+'px;height:0px;-moz-transform:rotate('+deg+'deg);-webkit-transform:rotate('+deg+'deg);position:absolute;top:'+y+'px;left:'+x+'px;');
}

</script>
</head>
<body>

</body>
</html>


asked 8 mins ago







Javascript draw line script not working

Aucun commentaire:

Enregistrer un commentaire