From 948c1006729bfdcd2b5937e7c240c4df3294290e Mon Sep 17 00:00:00 2001 From: Syndamia Date: Tue, 3 Jan 2023 16:50:55 +0200 Subject: [WGL/HW11] Simplified drag bar logic --- .../Homework 11 - The graph/Homework.html | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/Year 2/Computer Graphics with WebGL/Homework 11 - The graph/Homework.html b/Year 2/Computer Graphics with WebGL/Homework 11 - The graph/Homework.html index 70a553c..18760da 100644 --- a/Year 2/Computer Graphics with WebGL/Homework 11 - The graph/Homework.html +++ b/Year 2/Computer Graphics with WebGL/Homework 11 - The graph/Homework.html @@ -95,17 +95,20 @@ slright = document.getElementById("sl-right"); var inside = false; + var clicked = 0; slleft.onmouseenter = slright.onmouseenter = function () { inside = true; } slleft.onmouseleave = slright.onmouseleave = function () { inside = false; + clicked = 0; } - var clicked = 0; + var initialPosition = 0; var current = 0; var leftCurrent = 0; var rightCurrent = 0; + slleft.onmousedown = slright.onmousedown = function (e) { clicked = (this.id == "sl-left") ? 1 : 2; initialPosition = e.clientX; @@ -126,10 +129,8 @@ slleft.onmousemove = function (e) { if (!inside || clicked != 1) return; var newCurr = current + initialPosition - e.clientX; - if ((newCurr > leftCurrent) && // if we move left - (slright.offsetWidth * 0.4 - leftCurrent < 10)) return; - if ((newCurr < leftCurrent) && // if we move right - (slleft.offsetWidth * 0.4 - leftCurrent >= slright.offsetWidth * 0.6 - rightCurrent - 50)) return; + if ((newCurr > leftCurrent) && (slleftPos() < 10)) return; + if ((newCurr < leftCurrent) && (slleftPos() >= slrightPos() - 50)) return; this.style.transform = "translateX(calc(-60% - " + (leftCurrent = newCurr) + "px))"; changed = true; @@ -138,10 +139,8 @@ slright.onmousemove = function (e) { if (!inside || clicked != 2) return; var newCurr = current + initialPosition - e.clientX; - if ((newCurr < rightCurrent) && // if we move right - (slright.offsetWidth * 0.4 + rightCurrent < 10)) return; - if ((newCurr > rightCurrent) && // if we move left - (slleft.offsetWidth * 0.4 - leftCurrent >= slright.offsetWidth * 0.6 - rightCurrent - 50)) return; + if ((newCurr < rightCurrent) && (slrightPos() > slright.offsetWidth - 10)) return; + if ((newCurr > rightCurrent) && (slleftPos() >= slrightPos() - 50)) return; this.style.transform = "translateX(calc(60% - " + (rightCurrent = newCurr) + "px))"; changed = true; -- cgit v1.3.1-23-gd82d