1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
| <!DOCTYPE HTML>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Trace-relation</title>
<meta name="Author" content="NoSmoking">
<style>
html, body {
font: 1em/1.25 Verdana;
}
h1, h2, h3 {
color: #069;
}
header, section {
position: relative;
margin: auto;
max-width: 60em;
}
.case {
display: flex;
z-index: 1;
position: absolute;
align-items: center;
justify-content: center;
text-align: center;
width: 15em;
height: 100%;
min-height: 48px;
font-size: .8em;
}
.case-bleu {
border: 1px solid #ABC;
background: #CDE;
}
.case-rouge {
border: 1px solid #CAB;
background: #ECD;
}
.case-vert {
border: 1px solid #BCA;
background: #DEC;
}
.case:hover {
z-index: 100;
box-shadow: 0 0 5px #880;
cursor: pointer;
}
#depart_1 {
top: 160px;
left: 200px;
}
#arrivee_1 {
top: 80px;
left: 160px;
}
#arrivee_2 {
top: 300px;
left: 240px;
}
#depart_2 {
top: 240px;
left: 240px;
width: 8em;
}
#arrivee_3 {
top: 316px;
left: 500px;
height: 6em;
}
</style>
</head>
<body>
<header>
<h1>Trace relations</h1>
</header>
<section id="relation">
<div id="arrivee_3" class="case-vert case">Arrivée #3</div>
<div id="depart_1" class="case-bleu case">Départ #1</div>
<div id="arrivee_1" class="case-rouge case">Arrivée #1</div>
<div id="depart_2" class="case-bleu case">Départ #2</div>
<div id="arrivee_2" class="case-rouge case">Arrivée #2</div>
</section>
<script>
/**
*
*/
function createTrait(color, vert) {
var oElem = document.createElement('DIV');
var oStyle = oElem.style;
oElem.className = 'trait';
oStyle.boxSizing = 'border-box';
oStyle.position = 'absolute';
oStyle.borderColor = color;
oStyle.borderStyle = 'solid';
oStyle.borderWidth = '0';
if (vert) {
oStyle.width = 0;
oStyle.borderLeftWidth = '1px';
}
else {
oStyle.height = 0;
oStyle.borderTopWidth = '1px';
}
return oElem;
}
/**
*
*/
function moveToX(left, top, width, color) {
var oTrait = createTrait(color);
var oStyle = oTrait.style;
var posX = left + width;
if (width < 0) {
left = left + width;
width *= -1;
posX = left;
}
oStyle.left = left + 'px';
oStyle.top = top + 'px';
oStyle.width = (width+1) + 'px';
document.body.appendChild(oTrait);
return {
'x': posX,
'y': top,
'elem' : oTrait
};
}
/**
*
*/
function moveToY(left, top, height, color) {
var oTrait = createTrait(color, 1);
var oStyle = oTrait.style;
var posY = top + height;
if (height < 0) {
top = top + height;
height *= -1;
posY = top;
}
oStyle.left = left + 'px';
oStyle.top = top + 'px';
oStyle.height = (height+1) + 'px';
document.body.appendChild(oTrait);
return {
'x': left,
'y': posY,
'elem' : oTrait
};
}
/**
*
*/
function drawFleche(posx, posy, color) {
var oElem = document.createElement('DIV');
var oStyle = oElem.style;
var w = 12;
var w_2 = w / 2;
oStyle.position = 'absolute';
oStyle.left = posx + 'px';
oStyle.top = posy + 'px';
oStyle.width = 0;
oStyle.height = 0;
oStyle.borderStyle = 'solid';
oStyle.borderWidth = w_2 + 'px 0 ' + w_2 + 'px ' + w + 'px';
oStyle.borderColor = 'transparent transparent transparent ' + color;
oStyle.transform = 'translate( -100%, -50%)';
document.body.appendChild(oElem);
return oElem;
}
/**
*
*/
function drawPoint(posx, posy, color) {
var oElem = document.createElement('DIV');
var oStyle = oElem.style;
var w = 4;
oElem.className ='point';
oStyle.position = 'absolute';
oStyle.left = posx + 'px';
oStyle.top = posy + 'px';
oStyle.width = 0;
oStyle.height = 0;
oStyle.borderStyle = 'solid';
oStyle.borderRadius = '50%';
oStyle.borderWidth = w + 'px';
oStyle.borderColor = color;
oStyle.zIndex = 10;
oStyle.transform = 'translate( -50%, -50%)';
document.body.appendChild(oElem);
return oElem;
}
/**
*
*/
function traceLiaison(depart, arrivee, color) {
// initialisation
color = color || '#000';
var oDepart = document.getElementById(depart);
var oArrivee = document.getElementById(arrivee);
var obj_depart = oDepart.getBoundingClientRect();
var obj_arrivee = oArrivee.getBoundingClientRect();
// calcul déplacement
var decalX = 20;
var ancre_depart = {
'x': obj_depart.right,
'y': obj_depart.top + obj_depart.height / 2
};
var ancre_arrivee = {
'x': obj_arrivee.left,
'y': obj_arrivee.top + obj_arrivee.height / 2
};
var hauteur_fleche = ancre_arrivee.y - ancre_depart.y;
var largeur_fleche = (ancre_depart.x - ancre_arrivee.x) + (decalX * 2);
// deplacement
var pos = {
'x': ancre_depart.x,
'y': ancre_depart.y
};
drawPoint(pos.x, pos.y, color);
pos = moveToX(pos.x, pos.y, decalX, color);
pos = moveToY(pos.x, pos.y, hauteur_fleche / 2, color);
pos = moveToX(pos.x, pos.y, - largeur_fleche, color);
pos = moveToY(pos.x, pos.y, hauteur_fleche / 2, color);
pos = moveToX(pos.x, pos.y, decalX, color);
drawFleche( pos.x, pos.y, color);
}
// début du tracé
traceLiaison('arrivee_3','arrivee_1', '#069');
traceLiaison('depart_1','arrivee_1', '#069');
traceLiaison('depart_2','arrivee_2', '#069');
traceLiaison('arrivee_2','arrivee_3', '#069');
traceLiaison('depart_1','arrivee_3', '#069');
</script>
</body>
</html> |
Partager