|
| 1 | +--- |
| 2 | +title: Circular time |
| 3 | + |
| 4 | +--- |
| 5 | + |
| 6 | +import ZumerPlay from '/src/components/ZumerCode.astro'; |
| 7 | +import htmlCal from '/src/assets/circular.html?raw'; |
| 8 | + |
| 9 | + |
| 10 | +export const CSS = ` |
| 11 | +.grid-container { |
| 12 | + display: grid; |
| 13 | + grid-template-rows: 5% 95%; |
| 14 | + height: 100vh; |
| 15 | +} |
| 16 | +* { |
| 17 | + user-select: none |
| 18 | +} |
| 19 | +.satellite { |
| 20 | + border: none; |
| 21 | +} |
| 22 | +.disc{ |
| 23 | +background: white; font-size: 13px; |
| 24 | +} |
| 25 | +.time { |
| 26 | +--o-fill: red; --o-stroke: red; --o-stroke-width: 2; fill-opacity: 0.3 |
| 27 | + |
| 28 | +} |
| 29 | +.panel{ |
| 30 | + pointer-events: all; cursor: grab |
| 31 | +} |
| 32 | +.control { |
| 33 | + background: black; cursor: pointer;color: white |
| 34 | +} |
| 35 | +.time-display { |
| 36 | + color: var(--o-gray-dark); |
| 37 | + font-size: 25px; |
| 38 | +} |
| 39 | +` |
| 40 | + |
| 41 | +export const vue = ` |
| 42 | +{ |
| 43 | + data() { |
| 44 | + return { |
| 45 | + isDragging: false, |
| 46 | + startAngle: 0, |
| 47 | + rotationAngle: 359, |
| 48 | + progressValue: 1, |
| 49 | + }; |
| 50 | + }, |
| 51 | + computed: { |
| 52 | + tiempoRestante() { |
| 53 | + const gradosRestantes = 360 - this.rotationAngle; |
| 54 | + const segundos = Math.round(gradosRestantes / 6); |
| 55 | + const minutos = Math.round(segundos / 60); |
| 56 | + console.log(this.rotationAngle, this.progressValue) |
| 57 | + return segundos; |
| 58 | + }, |
| 59 | + }, |
| 60 | + methods: { |
| 61 | + getAngle(event) { |
| 62 | + const orbit7 = this.$refs.time_panel; |
| 63 | + const rect = orbit7.getBoundingClientRect(); |
| 64 | + const centerX = rect.left + rect.width / 2; |
| 65 | + const centerY = rect.top + rect.height / 2; |
| 66 | + const clientX = event.clientX || event.touches[0].clientX; |
| 67 | + const clientY = event.clientY || event.touches[0].clientY; |
| 68 | + const deltaX = clientX - centerX; |
| 69 | + const deltaY = clientY - centerY; |
| 70 | + return Math.atan2(deltaY, deltaX) * (180 / Math.PI); |
| 71 | + }, |
| 72 | + startDrag(event) { |
| 73 | + this.isDragging = true; |
| 74 | + this.startAngle = Math.round(this.getAngle(event) - this.rotationAngle); |
| 75 | +
|
| 76 | + // Add event listeners for dragging |
| 77 | + window.addEventListener('mousemove', this.duringDrag); |
| 78 | + window.addEventListener('touchmove', this.duringDrag); |
| 79 | + window.addEventListener('mouseup', this.stopDrag); |
| 80 | + window.addEventListener('touchend', this.stopDrag); |
| 81 | + }, |
| 82 | + duringDrag(event) { |
| 83 | + if (!this.isDragging) return; |
| 84 | +
|
| 85 | + // Calculate the new rotation angle and round it to the nearest integer |
| 86 | + let angle = Math.round(this.getAngle(event) - this.startAngle); |
| 87 | +
|
| 88 | + // Ensure the angle is always between 0 and 360 |
| 89 | + this.rotationAngle = (angle % 360 + 360) % 360; |
| 90 | + // Update the progress value based on the rotation angle |
| 91 | + this.progressValue = (360 - this.rotationAngle); |
| 92 | + |
| 93 | + }, |
| 94 | + stopDrag() { |
| 95 | + this.isDragging = false; |
| 96 | + let iconControl = this.$refs.icon_control |
| 97 | + iconControl.setAttribute('d', 'M320-203v-560l440 280-440 280Zm60-280Zm0 171 269-171-269-171v342Z') |
| 98 | + // Remove event listeners |
| 99 | + window.removeEventListener('mousemove', this.duringDrag); |
| 100 | + window.removeEventListener('touchmove', this.duringDrag); |
| 101 | + window.removeEventListener('mouseup', this.stopDrag); |
| 102 | + window.removeEventListener('touchend', this.stopDrag); |
| 103 | + }, |
| 104 | + toggleRestore(e) { |
| 105 | + let iconControl = this.$refs.icon_control |
| 106 | + if (this.isRestoring) { |
| 107 | + // Pause restoration |
| 108 | + iconControl.setAttribute('d', 'M320-203v-560l440 280-440 280Zm60-280Zm0 171 269-171-269-171v342Z') |
| 109 | + clearInterval(this.restoreInterval); |
| 110 | + this.isRestoring = false; |
| 111 | + } else if (!this.isRestoring && this.rotationAngle !== 359) { |
| 112 | + // Start restoration |
| 113 | + iconControl.setAttribute('d', 'M525-200v-560h235v560H525Zm-325 0v-560h235v560H200Zm385-60h115v-440H585v440Zm-325 0h115v-440H260v440Zm0-440v440-440Zm325 0v440-440Z') |
| 114 | + this.startRestoration(); |
| 115 | + } else { |
| 116 | + iconControl.setAttribute('d', '') |
| 117 | + } |
| 118 | + }, |
| 119 | + startRestoration() { |
| 120 | + // Stop any existing animation |
| 121 | + if (this.restoreInterval) { |
| 122 | + clearInterval(this.restoreInterval); |
| 123 | + } |
| 124 | +
|
| 125 | + // Calculate the delta angle and duration |
| 126 | + const deltaAngle = this.startAngle - this.rotationAngle; |
| 127 | + const duration = Math.abs(deltaAngle) * 1000; |
| 128 | +
|
| 129 | + // Calculate the step size and interval time |
| 130 | + const step = deltaAngle > 0 ? 1 : -1; |
| 131 | + const intervalTime = 1000 / 6 ; |
| 132 | +
|
| 133 | + // Start the animation |
| 134 | + this.isRestoring = true; |
| 135 | + this.restoreInterval = setInterval(() => { |
| 136 | + if (this.rotationAngle !== 359 && this.isRestoring && this.progressValue !== 1) { |
| 137 | + this.rotationAngle -= step; |
| 138 | + this.rotationAngle = (this.rotationAngle % 360 + 360) % 360; |
| 139 | + this.progressValue = 360 - this.rotationAngle; |
| 140 | + console.log(this.rotationAngle,this.isRestoring,this.progressValue) |
| 141 | + } else { |
| 142 | + this.resetToZero |
| 143 | + } |
| 144 | + }, intervalTime); |
| 145 | + }, |
| 146 | + startLongPress() { |
| 147 | + // Start long press timer |
| 148 | + this.longPressTimer = setTimeout(() => { |
| 149 | + this.resetToZero(); |
| 150 | + }, 1000); |
| 151 | + }, |
| 152 | + endLongPress() { |
| 153 | + // Clear long press timer if not triggered |
| 154 | + clearTimeout(this.longPressTimer); |
| 155 | + }, |
| 156 | + resetToZero() { |
| 157 | + |
| 158 | + clearInterval(this.restoreInterval); |
| 159 | + this.isRestoring = false; |
| 160 | + |
| 161 | + |
| 162 | + this.rotationAngle = 359; |
| 163 | + this.progressValue = 1; |
| 164 | + }, |
| 165 | + |
| 166 | +
|
| 167 | + } |
| 168 | +} |
| 169 | +` |
| 170 | + |
| 171 | + |
| 172 | +<ZumerPlay name={'scifi1'} framework="vue" htmlCode={htmlCal} cssCode={CSS} jsCode={vue} height={'500px'} /> |
| 173 | + |
| 174 | +**Credits:** reference author repo TODO |
| 175 | + |
| 176 | + |
| 177 | +## Show & Tell |
| 178 | + |
| 179 | +Join to our [Github Discussion](https://github.com/zumerlab/orbit/discussions/15) and show off what have you done with Orbit. |
| 180 | + |
| 181 | + |
0 commit comments