var containersRy = document.querySelector(".container");
var svg = document.querySelector(".typeRange");
var output = document.querySelector(".output");
var outline = document.querySelector(".outline");
var fill = document.querySelector(".fill");
var center = document.querySelector(".center");
var needle = document.querySelector(".needle");
var initialValue = document.querySelector(".initialValue");
var NS = "http:\/\/www.w3.org/2000/svg";
var W = parseInt(window.getComputedStyle(svg, null).getPropertyValue("width"));
var initVal = initialValue.value;
var scale = document.querySelector(".scale");
for (var sa = -180; sa <= 0; sa += 18) {
var sx1 = cx + sr1 * Math.cos(sa * rad);
var sy1 = cy + sr1 * Math.sin(sa * rad);
var sx2 = cx + sr2 * Math.cos(sa * rad);
var sy2 = cy + sr2 * Math.sin(sa * rad);
var sxT = cx + srT * Math.cos(sa * rad);
var syT = cy + srT * Math.sin(sa * rad);
var scaleLine = document.createElementNS(NS, "line");
setSVGAttributes(scaleLine, scaleLineObj);
scale.appendChild(scaleLine);
var scaleText = document.createElementNS(NS, "text");
setSVGAttributes(scaleText, scaleTextObj);
scaleText.textContent = n * 10;
scale.appendChild(scaleText);
function drawInput(cx, cy, r1, offset, delta, a) {
var d1 = getD1(cx, cy, r1, offset, delta);
var d2 = getD2(cx, cy, r1, offset, delta, a);
outline.setAttributeNS(null, "d", d1);
fill.setAttributeNS(null, "d", d2);
drawNeedle(cx, cy, r1, a);
function updateInput(p, cx, cy, r1, offset, delta) {
var a = Math.atan2(ly, lx) / rad - 180;
drawInput(cx, cy, r1, offset, delta, a);
output.innerHTML = Math.round((a + 180) / 1.8);
initialValue.value = Math.round((a + 180) / 1.8);
function getD1(cx, cy, r1, offset, delta) {
"M " + x1 + ", " + y1 + " A" + r1 + "," + r1 + " 0 0 0 " + x2 + "," + y2 + " H" + (offset + delta) + " A" + r2 + "," + r2 + " 0 0 1 " + x3 + "," + y3 + " z";
function getD2(cx, cy, r1, offset, delta, a) {
var x4 = cx + r1 * Math.cos(a);
var y4 = cy + r1 * Math.sin(a);
var x5 = cx + r2 * Math.cos(a);
var y5 = cy + r2 * Math.sin(a);
"M " + x4 + ", " + y4 + " A" + r1 + "," + r1 + " 0 0 0 " + x2 + "," + y2 + " H" + (offset + delta) + " A" + r2 + "," + r2 + " 0 0 1 " + x5 + "," + y5 + " z";
function drawNeedle(cx, cy, r1, a) {
var nx1 = cx + 5 * Math.cos((a - 90) * rad);
var ny1 = cy + 5 * Math.sin((a - 90) * rad);
var nx2 = cx + (r1 + 15) * Math.cos(a * rad);
var ny2 = cy + (r1 + 15) * Math.sin(a * rad);
var nx3 = cx + 5 * Math.cos((a + 90) * rad);
var ny3 = cy + 5 * Math.sin((a + 90) * rad);
var points = nx1 + "," + ny1 + " " + nx2 + "," + ny2 + " " + nx3 + "," + ny3;
needle.setAttributeNS(null, "points", points);
function oMousePos(elmt, evt) {
var ClientRect = elmt.getBoundingClientRect();
x: Math.round(evt.clientX - ClientRect.left),
y: Math.min(Math.round(evt.clientY - ClientRect.top), cy)
function clearRect(node) {
while (node.firstChild) {
node.removeChild(node.firstChild);
function setSVGAttributes(elmt, oAtt) {
elmt.setAttributeNS(null, prop, oAtt[prop]);
window.addEventListener("load", function() {
var pa = (initVal * 1.8) - 180;
p.x = cx + r1 * Math.cos(pa * rad);
p.y = cy + r1 * Math.sin(pa * rad);
updateInput(p, cx, cy, r1, offset, delta)
initialValue.addEventListener("input", function() {
var newVal = (!isNaN(val) && val >= 0 && val <= 100) ? val : 18;
var pa = (newVal * 1.8) - 180;
p.x = cx + r1 * Math.cos(pa * rad);
p.y = cy + r1 * Math.sin(pa * rad);
updateInput(p, cx, cy, r1, offset, delta)
svg.addEventListener("mousedown", function(evt) {
this.classList.add("focusable");
var mousePos = oMousePos(svg, evt);
updateInput(mousePos, cx, cy, r1, offset, delta);
svg.addEventListener("mouseup", function(evt) {
this.classList.remove("focusable");
svg.addEventListener("mouseout", function(evt) {
this.classList.remove("focusable");
svg.addEventListener("mousemove", function(evt) {
var mousePos = oMousePos(svg, evt);
updateInput(mousePos, cx, cy, r1, offset, delta);