From 74fee1faf9690e59c54d2e6c70cb2d1b9362d56f Mon Sep 17 00:00:00 2001 From: Rodgath <25994782+Rodgath@users.noreply.github.com> Date: Sat, 5 Aug 2023 06:09:48 +0300 Subject: [PATCH 1/5] Create Judo Spin Box wrapper --- src/judo-spin.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/judo-spin.js b/src/judo-spin.js index d11e4eb..2d6e35d 100644 --- a/src/judo-spin.js +++ b/src/judo-spin.js @@ -19,8 +19,11 @@ function judoSpin(element, options) { /* Current image starts at 0 or based on options entry */ let currImagePos = convertCurrImageNumber(options.currImage) - 1; - /* Set image box element */ + /* Set judo box element */ let judoSpinBox; + + /* Set judo box wrapper element */ + let judoSpinWrapper; /* Check if 'element' is a valid DOM element */ if (element instanceof HTMLElement || element instanceof Node) { @@ -117,6 +120,16 @@ function judoSpin(element, options) { } })(judoSpinBox); + /* Create judoSpinBox wrapper */ + (function(judoSpinBox) { + judoSpinWrapper = document.createElement('div'); + judoSpinWrapper.classList.add('judo-spin-wrapper'); + judoSpinWrapper.style.width = 'fit-content'; + judoSpinWrapper.style.margin = '0 auto'; + judoSpinBox.parentNode.insertBefore(judoSpinWrapper, judoSpinBox); + judoSpinWrapper.appendChild(judoSpinBox); + })(judoSpinBox); + /* Handle mousedown event */ const handleMouseDown = event => invokeMotion(event); From 9ed36fd4884f3637ce0e7cac184d6e47d67da980 Mon Sep 17 00:00:00 2001 From: Rodgath <25994782+Rodgath@users.noreply.github.com> Date: Sat, 5 Aug 2023 06:10:41 +0300 Subject: [PATCH 2/5] Create the scroller draggable element --- demo/css/style.css | 29 ++++++++++++++ demo/index.html | 4 ++ demo/js/judo-spin.js | 87 +++++++++++++++++++++++++++++++++++++++- demo/js/judo-spin.min.js | 2 +- dist/js/judo-spin.js | 87 +++++++++++++++++++++++++++++++++++++++- dist/js/judo-spin.min.js | 2 +- src/judo-spin.js | 72 +++++++++++++++++++++++++++++++++ 7 files changed, 279 insertions(+), 4 deletions(-) diff --git a/demo/css/style.css b/demo/css/style.css index 33ab5cc..de84cf7 100644 --- a/demo/css/style.css +++ b/demo/css/style.css @@ -179,4 +179,33 @@ body { .code-box pre { margin : 0; font-size: 13px +} + + + + + + +#judo-scroller { + position: relative; + width: 100%; + height: 20px; + background-color: #f1f1f1; + margin: 20px auto; + border-radius: 10px; + cursor: pointer; +} + +#judo-draggable { + position: absolute; + top: 0; + left: 0; + width: 60px; + height: 20px; + background-color: #4caf50; + border-radius: 20px; + cursor: pointer; + display: flex; + align-items: center; + justify-content: center; } \ No newline at end of file diff --git a/demo/index.html b/demo/index.html index 2000be7..63eac3b 100644 --- a/demo/index.html +++ b/demo/index.html @@ -66,6 +66,10 @@

360° degrees image spinner and rotation using JavaScript.

Image 36 +
+
+
+
HTML Code
diff --git a/demo/js/judo-spin.js b/demo/js/judo-spin.js index d11e4eb..4480ef7 100644 --- a/demo/js/judo-spin.js +++ b/demo/js/judo-spin.js @@ -19,8 +19,11 @@ function judoSpin(element, options) { /* Current image starts at 0 or based on options entry */ let currImagePos = convertCurrImageNumber(options.currImage) - 1; - /* Set image box element */ + /* Set judo box element */ let judoSpinBox; + + /* Set judo box wrapper element */ + let judoSpinWrapper; /* Check if 'element' is a valid DOM element */ if (element instanceof HTMLElement || element instanceof Node) { @@ -117,6 +120,16 @@ function judoSpin(element, options) { } })(judoSpinBox); + /* Create judoSpinBox wrapper */ + (function(judoSpinBox) { + judoSpinWrapper = document.createElement('div'); + judoSpinWrapper.classList.add('judo-spin-wrapper'); + judoSpinWrapper.style.width = 'fit-content'; + judoSpinWrapper.style.margin = '0 auto'; + judoSpinBox.parentNode.insertBefore(judoSpinWrapper, judoSpinBox); + judoSpinWrapper.appendChild(judoSpinBox); + })(judoSpinBox); + /* Handle mousedown event */ const handleMouseDown = event => invokeMotion(event); @@ -248,6 +261,78 @@ function judoSpin(element, options) { return direction; } + + + function scrollerHandler() { + + const judoScroller = document.getElementById('judo-scroller'); + const judoDraggable = document.getElementById('judo-draggable'); + + /* Flag to track if the button is being dragged */ + let dragging = false; + + /* Add event listeners to the draggable button for dragging */ + judoDraggable.addEventListener('mousedown', handleDragStart); + judoDraggable.addEventListener('touchstart', handleDragStart); + + /* Handle drag start event */ + function handleDragStart(event) { + event.preventDefault(); + dragging = true; + + document.addEventListener('mousemove', handleDragMove); + document.addEventListener('touchmove', handleDragMove); + } + + /* Handle drag move event */ + function handleDragMove(event) { + if (dragging) { + const judoScrollerRect = judoScroller.getBoundingClientRect(); + const buttonWidth = judoDraggable.offsetWidth; + const minScrollPos = 0; + const maxScrollPos = judoScroller.offsetWidth - buttonWidth; + + let scrollPos; + + if (event.type === 'touchmove') { + scrollPos = event.touches[0].clientX - judoScrollerRect.left - buttonWidth / 2; + } else { + scrollPos = event.clientX - judoScrollerRect.left - buttonWidth / 2; + } + + /* Ensure the scroll position stays within bounds */ + scrollPos = Math.max(minScrollPos, Math.min(maxScrollPos, scrollPos)); + + /* Update the draggable button position */ + judoDraggable.style.left = `${scrollPos}px`; + + /* Calculate the current angle based on the scroll position */ + const maxRotation = 360; // 360 degrees for a full rotation + const anglePerScroll = maxRotation / maxScrollPos; + currentAngle = Math.floor(scrollPos * anglePerScroll); + + /* Adjust currentAngle to be within the range of 0 to 359 degrees */ + currentAngle = (currentAngle % maxRotation + maxRotation) % maxRotation; + + /* Show the corresponding image based on the current angle */ + showImageForAngle(currentAngle); + } + } + + /* Handle drag end event */ + function handleDragEnd() { + dragging = false; + /* Remove the mousemove and touchmove event listeners */ + document.removeEventListener('mousemove', handleDragMove); + document.removeEventListener('touchmove', handleDragMove); + } + + /* Add event listeners for mouseup and touchend events */ + document.addEventListener('mouseup', handleDragEnd); + document.addEventListener('touchend', handleDragEnd); + } + + scrollerHandler(); } /* Call the function for elements with the 'data-judo-spin' attribute */ diff --git a/demo/js/judo-spin.min.js b/demo/js/judo-spin.min.js index 31e53c2..0cb3e44 100644 --- a/demo/js/judo-spin.min.js +++ b/demo/js/judo-spin.min.js @@ -1 +1 @@ -"use strict";function judoSpin(e,t){t||(o=e.dataset.judoSpin)&&(t=JSON.parse(o));var n,o,r=function(e){e=parseInt(e,10);return isNaN(e)?1:Math.abs(e)}((t=Object.assign({},{currImg:1,images:[]},t)).currImage)-1,u=e instanceof HTMLElement||e instanceof Node?e:document.querySelector(e),i=function(){var e;e=(t.hasOwnProperty("images")&&Array.isArray(t.images)&&0 invokeMotion(event); @@ -262,6 +275,78 @@ function judoSpin(element, options) { return direction; } + + + function scrollerHandler() { + + const judoScroller = document.getElementById('judo-scroller'); + const judoDraggable = document.getElementById('judo-draggable'); + + /* Flag to track if the button is being dragged */ + let dragging = false; + + /* Add event listeners to the draggable button for dragging */ + judoDraggable.addEventListener('mousedown', handleDragStart); + judoDraggable.addEventListener('touchstart', handleDragStart); + + /* Handle drag start event */ + function handleDragStart(event) { + event.preventDefault(); + dragging = true; + + document.addEventListener('mousemove', handleDragMove); + document.addEventListener('touchmove', handleDragMove); + } + + /* Handle drag move event */ + function handleDragMove(event) { + if (dragging) { + const judoScrollerRect = judoScroller.getBoundingClientRect(); + const buttonWidth = judoDraggable.offsetWidth; + const minScrollPos = 0; + const maxScrollPos = judoScroller.offsetWidth - buttonWidth; + + let scrollPos; + + if (event.type === 'touchmove') { + scrollPos = event.touches[0].clientX - judoScrollerRect.left - buttonWidth / 2; + } else { + scrollPos = event.clientX - judoScrollerRect.left - buttonWidth / 2; + } + + /* Ensure the scroll position stays within bounds */ + scrollPos = Math.max(minScrollPos, Math.min(maxScrollPos, scrollPos)); + + /* Update the draggable button position */ + judoDraggable.style.left = `${scrollPos}px`; + + /* Calculate the current angle based on the scroll position */ + const maxRotation = 360; // 360 degrees for a full rotation + const anglePerScroll = maxRotation / maxScrollPos; + currentAngle = Math.floor(scrollPos * anglePerScroll); + + /* Adjust currentAngle to be within the range of 0 to 359 degrees */ + currentAngle = (currentAngle % maxRotation + maxRotation) % maxRotation; + + /* Show the corresponding image based on the current angle */ + showImageForAngle(currentAngle); + } + } + + /* Handle drag end event */ + function handleDragEnd() { + dragging = false; + /* Remove the mousemove and touchmove event listeners */ + document.removeEventListener('mousemove', handleDragMove); + document.removeEventListener('touchmove', handleDragMove); + } + + /* Add event listeners for mouseup and touchend events */ + document.addEventListener('mouseup', handleDragEnd); + document.addEventListener('touchend', handleDragEnd); + } + + scrollerHandler(); } /* Call the function for elements with the 'data-judo-spin' attribute */ diff --git a/dist/js/judo-spin.min.js b/dist/js/judo-spin.min.js index c713ad8..d445ec2 100644 --- a/dist/js/judo-spin.min.js +++ b/dist/js/judo-spin.min.js @@ -12,4 +12,4 @@ * @licenseMIT https://github.com/Rodgath/judo-spin/blob/main/LICENSE * @demoExample https://rodgath.github.io/judo-spin/demo/ */ -"use strict";function judoSpin(e,t){t||(o=e.dataset.judoSpin)&&(t=JSON.parse(o));var n,o,r=function(e){e=parseInt(e,10);return isNaN(e)?1:Math.abs(e)}((t=Object.assign({},{currImg:1,images:[]},t)).currImage)-1,u=e instanceof HTMLElement||e instanceof Node?e:document.querySelector(e),i=function(){var e;e=(t.hasOwnProperty("images")&&Array.isArray(t.images)&&0 Date: Sat, 5 Aug 2023 06:32:46 +0300 Subject: [PATCH 3/5] Add Judo scroller programmatically and append to Judo wrapper --- demo/css/style.css | 29 ----------------------------- demo/index.html | 4 ---- demo/js/judo-spin.js | 36 ++++++++++++++++++++++++++++++++---- demo/js/judo-spin.min.js | 2 +- dist/js/judo-spin.js | 36 ++++++++++++++++++++++++++++++++---- dist/js/judo-spin.min.js | 2 +- src/judo-spin.js | 36 ++++++++++++++++++++++++++++++++---- 7 files changed, 98 insertions(+), 47 deletions(-) diff --git a/demo/css/style.css b/demo/css/style.css index de84cf7..33ab5cc 100644 --- a/demo/css/style.css +++ b/demo/css/style.css @@ -179,33 +179,4 @@ body { .code-box pre { margin : 0; font-size: 13px -} - - - - - - -#judo-scroller { - position: relative; - width: 100%; - height: 20px; - background-color: #f1f1f1; - margin: 20px auto; - border-radius: 10px; - cursor: pointer; -} - -#judo-draggable { - position: absolute; - top: 0; - left: 0; - width: 60px; - height: 20px; - background-color: #4caf50; - border-radius: 20px; - cursor: pointer; - display: flex; - align-items: center; - justify-content: center; } \ No newline at end of file diff --git a/demo/index.html b/demo/index.html index 63eac3b..2000be7 100644 --- a/demo/index.html +++ b/demo/index.html @@ -66,10 +66,6 @@

360° degrees image spinner and rotation using JavaScript.

Image 36
-
-
-
-
HTML Code
diff --git a/demo/js/judo-spin.js b/demo/js/judo-spin.js index 4480ef7..40d94f5 100644 --- a/demo/js/judo-spin.js +++ b/demo/js/judo-spin.js @@ -262,12 +262,40 @@ function judoSpin(element, options) { return direction; } - + /* Handle draggable image scroller */ function scrollerHandler() { - - const judoScroller = document.getElementById('judo-scroller'); - const judoDraggable = document.getElementById('judo-draggable'); + /* Create Judo scroller element */ + const judoScroller = document.createElement('div'); + judoScroller.id = 'judo-scroller'; + judoScroller.style.cssText = ` + position: relative; + width: 100%; + height: 20px; + background-color: #f1f1f1; + margin: 20px auto; + border-radius: 10px; + cursor: pointer;`; + + /* Create Judo draggable element */ + const judoDraggable = document.createElement('div'); + judoDraggable.id = 'judo-draggable'; + judoDraggable.style.cssText = `position: absolute; + top: 0; + left: 0; + width: 60px; + height: 20px; + background-color: #4caf50; + border-radius: 20px; + cursor: pointer; + display: flex; + align-items: center; + justify-content: center;`; + + /* Append elements to respective parent nodes */ + judoScroller.appendChild(judoDraggable); + judoSpinWrapper.appendChild(judoScroller); + /* Flag to track if the button is being dragged */ let dragging = false; diff --git a/demo/js/judo-spin.min.js b/demo/js/judo-spin.min.js index 0cb3e44..f7ab9d8 100644 --- a/demo/js/judo-spin.min.js +++ b/demo/js/judo-spin.min.js @@ -1 +1 @@ -"use strict";function judoSpin(e,t){t||(r=e.dataset.judoSpin)&&(t=JSON.parse(r));var n,o,r,u=function(e){e=parseInt(e,10);return isNaN(e)?1:Math.abs(e)}((t=Object.assign({},{currImg:1,images:[]},t)).currImage)-1,i=e instanceof HTMLElement||e instanceof Node?e:document.querySelector(e),s=function(){var e;e=(t.hasOwnProperty("images")&&Array.isArray(t.images)&&0 Date: Sat, 5 Aug 2023 06:34:53 +0300 Subject: [PATCH 4/5] Rename 'scrollerHandler' to 'draggableScroller' --- demo/js/judo-spin.js | 6 +++--- dist/js/judo-spin.js | 6 +++--- src/judo-spin.js | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/demo/js/judo-spin.js b/demo/js/judo-spin.js index 40d94f5..b56c4c0 100644 --- a/demo/js/judo-spin.js +++ b/demo/js/judo-spin.js @@ -262,8 +262,8 @@ function judoSpin(element, options) { return direction; } - /* Handle draggable image scroller */ - function scrollerHandler() { + /* Handle draggable scroller */ + function draggableScroller() { /* Create Judo scroller element */ const judoScroller = document.createElement('div'); @@ -360,7 +360,7 @@ function judoSpin(element, options) { document.addEventListener('touchend', handleDragEnd); } - scrollerHandler(); + draggableScroller(); } /* Call the function for elements with the 'data-judo-spin' attribute */ diff --git a/dist/js/judo-spin.js b/dist/js/judo-spin.js index d655f63..de7eddf 100644 --- a/dist/js/judo-spin.js +++ b/dist/js/judo-spin.js @@ -276,8 +276,8 @@ function judoSpin(element, options) { return direction; } - /* Handle draggable image scroller */ - function scrollerHandler() { + /* Handle draggable scroller */ + function draggableScroller() { /* Create Judo scroller element */ const judoScroller = document.createElement('div'); @@ -374,7 +374,7 @@ function judoSpin(element, options) { document.addEventListener('touchend', handleDragEnd); } - scrollerHandler(); + draggableScroller(); } /* Call the function for elements with the 'data-judo-spin' attribute */ diff --git a/src/judo-spin.js b/src/judo-spin.js index 40d94f5..b56c4c0 100644 --- a/src/judo-spin.js +++ b/src/judo-spin.js @@ -262,8 +262,8 @@ function judoSpin(element, options) { return direction; } - /* Handle draggable image scroller */ - function scrollerHandler() { + /* Handle draggable scroller */ + function draggableScroller() { /* Create Judo scroller element */ const judoScroller = document.createElement('div'); @@ -360,7 +360,7 @@ function judoSpin(element, options) { document.addEventListener('touchend', handleDragEnd); } - scrollerHandler(); + draggableScroller(); } /* Call the function for elements with the 'data-judo-spin' attribute */ From 6269f30f98c9df5360a861b8c3587b13b02dd8aa Mon Sep 17 00:00:00 2001 From: Rodgath <25994782+Rodgath@users.noreply.github.com> Date: Sat, 5 Aug 2023 06:45:54 +0300 Subject: [PATCH 5/5] Make the draggable scroller 3Dish --- demo/js/judo-spin.js | 24 ++++++++++++++++++++---- demo/js/judo-spin.min.js | 2 +- dist/js/judo-spin.js | 24 ++++++++++++++++++++---- dist/js/judo-spin.min.js | 2 +- src/judo-spin.js | 24 ++++++++++++++++++++---- 5 files changed, 62 insertions(+), 14 deletions(-) diff --git a/demo/js/judo-spin.js b/demo/js/judo-spin.js index b56c4c0..be5488e 100644 --- a/demo/js/judo-spin.js +++ b/demo/js/judo-spin.js @@ -271,28 +271,44 @@ function judoSpin(element, options) { judoScroller.style.cssText = ` position: relative; width: 100%; - height: 20px; + height: 12px; background-color: #f1f1f1; margin: 20px auto; border-radius: 10px; - cursor: pointer;`; + cursor: pointer; + box-shadow: inset 1px 1px 3px #d7d7d7;`; /* Create Judo draggable element */ const judoDraggable = document.createElement('div'); judoDraggable.id = 'judo-draggable'; - judoDraggable.style.cssText = `position: absolute; + judoDraggable.style.cssText = ` + position: absolute; top: 0; left: 0; width: 60px; - height: 20px; + height: calc(100% + 10px); background-color: #4caf50; border-radius: 20px; cursor: pointer; + transform: translateY(-5px); + box-shadow: 0 0 5px rgba(0, 0, 0, 0.45); display: flex; align-items: center; justify-content: center;`; + /* Create a new element (pseudo-element) */ + const pseudoElement = document.createElement('span'); + pseudoElement.textContent = '| | |'; + pseudoElement.style.cssText = ` + color: #fff; + position: relative; + height: 10px; + line-height: 10px; + overflow: hidden; + text-shadow: 1px 0 2px #a8a8a8;`; + /* Append elements to respective parent nodes */ + judoDraggable.appendChild(pseudoElement); judoScroller.appendChild(judoDraggable); judoSpinWrapper.appendChild(judoScroller); diff --git a/demo/js/judo-spin.min.js b/demo/js/judo-spin.min.js index f7ab9d8..4bfdaf5 100644 --- a/demo/js/judo-spin.min.js +++ b/demo/js/judo-spin.min.js @@ -1 +1 @@ -"use strict";function judoSpin(e,t){t||(r=e.dataset.judoSpin)&&(t=JSON.parse(r));var n,o,r,i=function(e){e=parseInt(e,10);return isNaN(e)?1:Math.abs(e)}((t=Object.assign({},{currImg:1,images:[]},t)).currImage)-1,u=e instanceof HTMLElement||e instanceof Node?e:document.querySelector(e),s=function(){var e;e=(t.hasOwnProperty("images")&&Array.isArray(t.images)&&0