Procházet zdrojové kódy

куртилки трек забыл добавить

kurkabein před 2 roky
rodič
revize
fd0a9fb62c
3 změnil soubory, kde provedl 123 přidání a 0 odebrání
  1. binární
      functional_OOP/1@3x.png
  2. 18 0
      functional_OOP/index.html
  3. 105 0
      functional_OOP/main.js

binární
functional_OOP/1@3x.png


+ 18 - 0
functional_OOP/index.html

@@ -0,0 +1,18 @@
+<!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>Document</title>
+    <style>
+        #container1 {
+            padding: 400px;
+        }
+    </style>
+</head>
+<body>
+    <div id='container1'></div>
+    <script src="main.js"></script>
+</body>
+</html>

+ 105 - 0
functional_OOP/main.js

@@ -0,0 +1,105 @@
+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'
+    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 (newValue !== value && typeof this.onchange === "function")
+            this.onchange(newValue);
+        
+        value = newValue;
+        img.style.transform = `rotate(${getAngle()}deg)`;
+    }
+    this.setValue(value);
+    this.getValue = () => value;
+    img.onmousewheel = event => {
+        const {deltaY} = event
+        event.preventDefault()
+        this.setValue(value + deltaY * wheelSpeed);
+    }
+    
+    const rad2deg = rad => (rad * 360) / (2 * Math.PI)
+    const event2Angle = event => {
+        const {layerX,layerY} = event
+        const rad2deg = rad => (rad * 360) / (2 * Math.PI)
+        const x = layerX - img.width/2
+        const y = -(layerY - img.height/2)
+
+        /* console.log(x,y, rad2deg(Math.atan2(y,x))); */
+        return rad2deg(Math.atan2(y,x));
+    }
+    
+    img.onclick = event => {
+        const {layerX,layerY} = event
+        /* console.log(event); */
+        if(layerX < img.width/2){
+            this.setValue(value - step)
+        } else {
+            this.setValue(value + step)
+        }
+
+    }
+
+    let startAngle = null;
+    img.onmousedown = event => {
+        startAngle = event2Angle(event)
+        event.preventDefault();
+    }
+    img.onmousemove = event => {
+        if(startAngle === null) return
+        
+        const newAngle = event2Angle(event)
+        const angleDiff = newAngle - startAngle;
+        const valueDiff = angleDiff/ratio;
+        this.setValue(value-valueDiff);
+        startAngle = newAngle
+        event.preventDefault()
+    }
+    img.onmouseup = event => {
+        startAngle = null;
+        event.preventDefault();
+    }
+}
+/* let container1 = document.getElementById('container1'); */
+const volumeControl = new Control(container1, {value:25,min:0,max:100,minAngle: -90, maxAngle:90});
+
+//пришейте к нему тэг audio для громкости
+
+function setRGB(){
+    document.body.style.backgroundColor = `rgb(${red.getValue()},${green.getValue()},${blue.getValue()})`;
+    //какой то блок, которому меняем цвет через rgba и цвета крутилок
+}
+const red = new Control(container1, {min: 0, max: 255})
+red.onchange = setRGB
+const blue = new Control(container1, {min: 0, max: 255})
+blue.onchange = setRGB
+const green = new Control(container1, {min:0, max: 255})
+green.onchange = setRGB
+//const red  = new Control(container1, {min: 0, max: 255})
+//volumeControl.onchange = setRGB
+//const green  = new Control(container1, {min: 0, max: 255})
+//volumeControl.onchange = setRGB
+//const blue  = new Control(container1, {min: 0, max: 255})
+//volumeControl.onchange = setRGB
+
+////сделать три крутилки для RGB
+////и обновлять цвет блока/фона блока при вращении любой из трех
+
+//setTimeout(() => volumeControl.setValue(80), 2000)
+