Browse Source

add Music and RGB control

LenDoc 2 years ago
parent
commit
7341234bbb
4 changed files with 133 additions and 0 deletions
  1. BIN
      js/10/control/1@3x.png
  2. 22 0
      js/10/control/index.html
  3. 111 0
      js/10/control/index.js
  4. BIN
      js/10/control/music.mp3

BIN
js/10/control/1@3x.png


+ 22 - 0
js/10/control/index.html

@@ -0,0 +1,22 @@
+<!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">
+
+</head>
+
+<body>
+    <div id="container1"> </div>
+    <p>Tyler, The Creator EARFQUAKE</p>
+    <div id="containerMusic">
+        <audio class="music" controls>
+        <source src="music.mp3" type="audio/mpeg">
+    </audio>
+    </div>
+    <script src="index.js"></script>
+</body>
+
+</html>

+ 111 - 0
js/10/control/index.js

@@ -0,0 +1,111 @@
+function Control(el, {
+    value = 0,
+    min = 0,
+    max = 100,
+    minAngle = 0,
+    maxAngle = 360,
+    wheelSpeed = 0.05,
+    step = 1
+} = {}) {
+
+    const img = document.createElement('img')
+    img.src = '1@3x.png'
+    el.append(img)
+
+    const ratio = (maxAngle - minAngle) / (max - min)
+    const getAngle = () => (value - min) * ratio + minAngle
+
+    this.setValue = newValue => {
+        if (newValue > max)
+            newValue = max
+        if (newValue < min)
+            newValue = min
+
+        if (typeof this.onchange === 'function' && newValue !== value)
+            this.onchange(newValue)
+
+        value = newValue
+
+        img.style.transform = `rotate(${getAngle()}deg)`
+    }
+
+    img.onmousewheel = (event) => {
+        const { deltaY } = event
+        //console.log(deltaY)
+        this.setValue(value + deltaY * wheelSpeed)
+        event.preventDefault()
+    }
+
+    img.onclick = (event) => {
+        const { layerX } = event
+        if (layerX < img.width / 2)
+            this.setValue(value - step)
+        else
+            this.setValue(value + step)
+        event.preventDefault()
+    }
+
+    const event2Deg = event => {
+        const { layerX, layerY } = event
+        const { width, height } = img
+        const x = layerX - width / 2
+        const y = height / 2 - layerY
+
+        //console.log(x, y, width, height)
+
+        return ((Math.atan2(y, x) / (2 * Math.PI)) * 360 + 360) % 360
+    }
+
+    let prevAngle = null
+
+    img.onmousedown = (event) => {
+        prevAngle = event2Deg(event)
+    }
+
+    img.onmousemove = (event) => {
+        if (prevAngle === null) return
+
+        const currentAngle = event2Deg(event)
+        const deltaValue = (prevAngle - currentAngle) / ratio
+            //console.log(prevAngle - currentAngle, deltaValue)
+        this.setValue(value + deltaValue)
+        prevAngle = currentAngle
+        event.preventDefault()
+    }
+    img.onmouseup = (event) => {
+        prevAngle = null
+    }
+
+    this.setValue(value)
+    this.getValue = () => value
+}
+
+const volumeControl = new Control(container1, {
+    value: 75,
+    min: 0,
+    max: 100,
+    minAngle: -90,
+    maxAngle: 90
+})
+volumeControl.onchange = value => console.log('ON CHANGE', value) //на каждый setValue
+console.log(volumeControl.getValue())
+
+// тэг audio для громкости
+
+const music = document.querySelector(".music");
+const Musvolume = new Control(containerMusic, { min: 0, max: 100 });
+Musvolume.onchange = (value) => { music.volume = value / 100 };
+
+
+const red = new Control(container1, { min: 0, max: 255 })
+const green = new Control(container1, { min: 0, max: 255 })
+const blue = new Control(container1, { min: 0, max: 255 })
+
+function setRGB() {
+
+    document.body.style.backgroundColor = `rgb(${red.getValue()},${green.getValue()},${blue.getValue()})`
+}
+
+red.onchange = setRGB
+green.onchange = setRGB
+blue.onchange = setRGB

BIN
js/10/control/music.mp3