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
| <!DOCTYPE html>
<html lang="">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<header></header>
<main>
<table border="10px">
<tr height="50px">
</tr>
<tr>
<td>
<table>
<tr>
<td>
<label for="cons">Gauge Consigne</label>
</td>
</tr>
<tr>
<td>
<input id="cons" type="number" value="0" min="0" max="360"/>
</td>
</tr>
<tr>
<td>
<input id="slide" type="range" value="0" min="0" max="360"/>
</td>
</tr>
</table>
</td>
<td id="gauge_svg" width=300px height=200px style="padding:30px 40px">
</tr>
</table>
<style>
.line {
stroke: #bfbfbf;
stroke-width: 1;
fill: none;
}
.gauge-ring {
stroke: #EBEBEB;
}
</style>
<script>
class gauge {
constructor({id = "gauge_svg"}){
this.id=id
this.container=document.getElementById(id)
this.container.innerHTML = /*html*/`
<div padding="0px" border="0px">
<svg class="gauge-container" width="100%" height="100%" viewBox="0 0 100 100">
<g transform="translate(50 50)" transform-origin= 50 50 >
<rect x="-50" y="-50" width="100" height="100" style="fill:lemonchiffon;stroke:slategray;stroke-width:1px;"/>
<line class="gauge-needleCons" x1="0" y1="40" x2="0" y2="45" />
<circle class="gauge-centre" cx="0" cy="0" r="5" fill="#eee" stroke="#ccc" stroke-width="0.1 " ></circle>
<circle class="gauge-ring" cx="0" cy="0" r="40" fill="transparent" stroke-width="3.5"></circle>
<g class="ori">
<g class="gaugeG" transform="translate(40 0)" transform-origin= 40 0 >
<circle class="gauge-bullCons" cx="0" cy="0" r="10" fill="#eee" stroke="#ccc" stroke-width="0.1 " ></circle>
<text class="gauge-cons-text" x="0" y="0" stroke-width="0.5" font-size=" 0.5em" text-align= "center" text-anchor= "middle" >
<tspan class="gauge-consLabel"></tspan>
</text>
</g>
</g>
<rect class="gauge-slide" x="-50" y="-50" width="100" height="100" style="fill:transparent;stroke:blue;stroke-width:1px;"/>
</g>
</svg>
</div>
`
this.containerSVG=this.container.querySelector("svg")
this.bullCons = this.containerSVG.querySelector(".gauge-bullCons")
this.consLabel = this.containerSVG.querySelector(".gauge-consLabel")
this.gaugeconstext = this.containerSVG.querySelector(".gauge-cons-text")
this.gaugeG = this.containerSVG.querySelector(".gaugeG")
this.ori = this.containerSVG.querySelector(".ori")
this.slide = this.container.querySelector(".gauge-slide")
this.slideBound = this.container.getBoundingClientRect()
this.mouseValue = null
this.slide.addEventListener("pointerdown", e => this.mdown(e));
document.addEventListener("pointerup", this._handleUp = this.mup.bind(this));
this.dimpx=Math.max(
Number(this.container.width.split('px')[0]),
Number(this.container.height.split('px')[0]),
)
this.dimR=100/this.dimpx //rapport de convertion offset pointer/viewbox
}
#cartesian2Polar(x, y){
let distance = Math.round(Math.sqrt(x*x + y*y))
let radians = Math.atan2(y,x)
let polarCoor = { distance:distance, degrees:Math.round(radians*180/Math.PI) }
return polarCoor
}
mdown(){
this.slide.addEventListener("mousemove", this._handleMove = this.mouse.bind(this));
}
mup(){
this.slide.removeEventListener("mousemove",this._handleMove);
document.removeEventListener("mouseup", this._handleUp);
}
mouse(ev){
let posX = ev.offsetX*this.dimR-50 //50 offset centrage coordoné
let posY = ev.offsetY*this.dimR-50
this.ctrl(this.#cartesian2Polar(posX,posY).degrees)
}
ctrl(value){
this.bullCons.style.transform = `rotate(${value}deg)`
this.consLabel.innerHTML=value
this.ori.style.transform = `
rotate(${value}deg)
`;
this.gaugeconstext.style.transform = `
rotate(${-value}deg)
`;
}
}
let gauge_di = {};//Dictionnaire des instances
gauge_di[1] = new gauge({id : "gauge_svg"})
window.onload = function () {
const container = document.querySelector(".gauge-container");
const bullCons = container.querySelector(".gauge-bullCons");
const consLabel = container.querySelector(".gauge-consLabel");
const gaugeconstext = container.querySelector(".gauge-cons-text");
const gaugeG = container.querySelector(".gaugeG");
const ori = container.querySelector(".ori");
document.querySelector("#cons").addEventListener("change", event => {
gauge_di[1].ctrl(event.currentTarget.value)
});
document.querySelector("#slide").addEventListener("mousedown", event => {
document.querySelector("#slide").addEventListener("mousemove", event => {
gauge_di[1].ctrl(event.currentTarget.value)
});
});
}
</script>
</main>
<footer></footer>
</body>
</html> |
Partager