i found this piece of code to create a diagonal in a div.
https://jsbin.com/tefakagohi/edit?html,css,output
Im trying to make a line generator in JS. however i would like to create this object complety using js...
Starting with the document.createElement() etc...
I have some code here:
function createLine(name, x1, y1, x2, y2, color){
var rectX1;
var rectY1;
var rectX2;
var rectY2;
//Line direction
// 0 = top left -> bottom right
// 1 = top right -> bottom left
// 2 = bottom left -> top right
// 3 = bottom right -> top left
var lineDirection = null;
//Find the direction
if (x1 < x2 && y1 < y2) {
lineDirection = 0;
rectX1 = x1;
rectY1 = y1;
rectX2 = x2;
rectY2 = y2;
}
if (x2 < x1 && y1 < y2) {
lineDirection = 1;
rectX1 = x2;
rectY1 = y1;
rectX2 = x1;
rectY2 = y2;
}
if (x1 < x2 && y2 < y1) {
lineDirection = 2;
rectX1 = x1;
rectY1 = y2;
rectX2 = x2;
rectY2 = y1;
}
if (x2 < x1 && y2 < y1) {
lineDirection = 3;
rectX1 = x2;
rectY1 = y2;
rectX2 = x1;
rectY2 = y1;
}
//Create a div with the 2 points
var div = document.createElement("div");
div.id = name;
div.style.position = "absolute";
div.style.zIndex = 5;
div.style.left = rectX1 + "px";
div.style.top = rectY1 + "px";
div.style.right = (window.innerWidth - rectX2) + "px";
div.style.bottom = (window.innerHeight - rectY2) + "px";
div.style.backgroundColor = color;
//Add the div to the body
document.body.appendChild(div);
}
This does a bit more but now i would like to create the diagonal.
And yes I know that i need some formulas to calculate the degree and the length of the line but for now I would just like to know how i can create the diagonal with only js.
Thx a lot, Jules