Pārlūkot izejas kodu

spin-buttons task done

miskson 3 gadi atpakaļ
vecāks
revīzija
22da72e701
4 mainītis faili ar 158 papildinājumiem un 0 dzēšanām
  1. BIN
      spin-btns/1@3x.png
  2. 50 0
      spin-btns/index.html
  3. 108 0
      spin-btns/scripts.js
  4. BIN
      spin-btns/track.mp3

BIN
spin-btns/1@3x.png


+ 50 - 0
spin-btns/index.html

@@ -0,0 +1,50 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta http-equiv="X-UA-Compatible" content="IE=edge">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>Spin buttons</title>
+</head>
+<body>
+    <h1>Spin buttons</h1>
+    <div id="wrapper-colors">
+        <h2>Colors</h2>
+        <div id="color-window"></div>
+        <br>
+        <div>
+            <label for="" style="color: red;">Red<div  class="spin" id='red'></div></label>
+            <label for="" style="color: rgba(172, 255, 47, 0.774);"> Green<div  class="spin" id='green'></div></label>
+            <label for="" style="color: rgb(0, 110, 255);"> Blue<div  class="spin" id='blue'></div></label>
+        </div>
+    </div>
+    <div id="wrapper-audio">
+        <h2>Audio</h2>
+        <audio id="track" src="./track.mp3" controls></audio>
+        <label for="">Vol.<div class="spin" id="audio-spin"></div></label>
+    </div>
+    <script src="./scripts.js"></script>
+</body>
+<style>
+    #wrapper-colors {
+        width: fit-content;
+    }
+
+    #color-window {
+        border: 1px solid black;
+        width: 100%;
+        height: 100px;
+    }
+    .spin {
+        width: 80px;
+        height: 80px;
+        display: inline-block;
+    }
+    label {
+        font-family: 'Courier New', Courier, monospace;
+        background-color: black;
+        color: white;
+        border-radius: 0 0 60px 0;
+    }
+</style>
+</html>

+ 108 - 0
spin-btns/scripts.js

@@ -0,0 +1,108 @@
+function Control(el, { value = 0,
+    min = 0,
+    max = 100,
+    minAngle = 0,
+    maxAngle = 360,
+    wheelSpeed = 0.1,
+    step = 1 } = {}) {
+
+    const img = document.createElement('img')
+    img.src = '1@3x.png'
+    img.style.width = '100%'
+    img.style.height = 'auto'
+    el.append(img)
+    
+    let forceChange = new Event('change')
+    this.currentValue = document.createElement('input')
+    this.currentValue.type = 'number'
+    this.currentValue.value = value
+
+    const ratio = (maxAngle - minAngle) / (max - min)
+    const getAngle = () => (value - min) * ratio + minAngle
+
+    this.getValue = () => {
+        return value
+    }
+
+    this.setValue = newValue => {
+        if (newValue > max) {
+            newValue = max
+        }
+        if (newValue < min) {
+            newValue = min
+        }
+        value = newValue
+        this.currentValue.value = value
+        this.currentValue.dispatchEvent(forceChange)
+        img.style.transform = `rotate(${getAngle()}deg)`
+    }
+
+    img.onmousewheel = e => {
+        const { deltaY } = e
+
+        const newValue = value + deltaY * wheelSpeed
+        this.setValue(newValue)
+
+        e.preventDefault()
+    }
+
+    img.onclick = e => {
+        console.log(e, img.width)
+
+        const { layerX } = e
+        const { width } = img
+        
+        layerX > width / 2? this.setValue(value + step) : this.setValue(value - step)
+        console.log(value)
+    }
+
+    const toDeg = rad => ((rad * 180) / Math.PI + 360 + 90) % 360
+
+    let prevMouseAngle = null
+
+    img.onmousedown = e => {
+        const y = e.layerY - img.height / 2
+        const x = e.layerX - img.width / 2
+        prevMouseAngle = toDeg(Math.atan2(y, x))
+        e.preventDefault()
+    }
+
+    img.onmousemove = e => {
+        if (prevMouseAngle === null) return
+        console.log(value)
+        const y = e.layerY - img.height / 2
+        const x = e.layerX - img.width / 2
+        let currentAngle = toDeg(Math.atan2(y, x))
+        let moveAngleDiff = (currentAngle - prevMouseAngle)
+        this.setValue(value + moveAngleDiff / ratio)
+        prevMouseAngle = currentAngle
+    }
+
+    img.onmouseout = img.onmouseup = () => {
+        prevMouseAngle = null
+    }
+
+    this.setValue(value)
+}
+
+
+let track = document.getElementById('track')
+let audioSpin = document.getElementById('audio-spin')
+const volumeControl = new Control(audioSpin, { value: 50, min: 0, max: 100, minAngle: 0, maxAngle: 360 })
+track.volume = volumeControl.getValue() / 100
+volumeControl.currentValue.onchange = () =>  track.volume = volumeControl.getValue() / 100
+
+
+// RGB
+const Red  = new Control(red, {min: 0, max: 255})
+const Green  = new Control(green, {value: 200, min: 0, max: 255})
+const Blue  = new Control(blue, {value: 125, min: 0, max: 255})
+
+let setRGB = (element) => {
+    element.style.backgroundColor = `rgb(${Red.getValue()||0}, ${Green.getValue()||0}, ${Blue.getValue()||0})`
+}
+setRGB(document.getElementById('color-window'))
+
+Red.currentValue.onchange = () => setRGB(document.getElementById('color-window'))
+Green.currentValue.onchange = () => setRGB(document.getElementById('color-window'))
+Blue.currentValue.onchange = () => setRGB(document.getElementById('color-window'))

BIN
spin-btns/track.mp3