Browse Source

HW js10 regulator_part

ivar_n 2 years ago
parent
commit
a4fa99ed2c
5 changed files with 132 additions and 0 deletions
  1. BIN
      js/10/regulator/1@3x.png
  2. 21 0
      js/10/regulator/index.html
  3. 111 0
      js/10/regulator/index.js
  4. 0 0
      js/10/tasks/index.html
  5. 0 0
      js/10/tasks/index.js

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


+ 21 - 0
js/10/regulator/index.html

@@ -0,0 +1,21 @@
+<!DOCTYPE HTML>
+<html>
+    <head>
+        <meta charset="utf-8">
+        <meta name="viewport" content="width=device-width">
+        <title>Другой тайтл ПРИВЕТ 17й</title>
+        <style>
+            #container1 {
+                padding: 40px;
+            }
+        </style>
+    </head>
+    <body>
+        <div id="color" style="height: 200px; width: 100%; background-color:  rgb(0, 0, 0);"></div>
+
+        <div id="container1" ></div>
+
+
+        <script src="index.js"></script>
+    </body>
+</html>

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

@@ -0,0 +1,111 @@
+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
+        }
+        value = newValue
+        img.style.transform = `rotate(${getAngle()}deg)`
+
+        if (typeof this.onchange === 'function') {
+            this.onchange(value)
+        }
+    }
+
+    this.getValue = () => value
+
+
+
+
+
+    img.onmousewheel = e => {
+        const {deltaY} = e
+        const newValue = value + deltaY*wheelSpeed
+        this.setValue(newValue)
+        e.preventDefault()
+    }
+
+    img.onclick = e => {
+        // console.log(e)
+        const {offsetX, layerX} = e
+        const center = img.width/2
+        if (layerX < center) {
+            this.setValue(value + step)
+        }
+        if (layerX > center) {
+            this.setValue(value - step)
+        }
+    }
+
+    //перевод радианов в градусы и смещение в координаты от 0 до 360 градусов
+    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(x,y))
+
+        e.preventDefault()
+    }
+
+    img.onmousemove = e => {
+        if (prevMouseAngle === null) return
+        const y = e.layerY - img.height/2
+        const x = e.layerX - img.width/2
+        let currentAngle = toDeg(Math.atan2(x,y))
+        let moveAngleDiff = (currentAngle - prevMouseAngle)
+
+        this.setValue(value - moveAngleDiff/ratio)
+        prevMouseAngle = currentAngle
+
+        console.log()
+    }
+
+    img.onmouseout = img.onmouseup = e => {
+        prevMouseAngle = null
+    }
+
+
+    this.setValue(value)
+}
+
+function setRgb() {
+    color.style.backgroundColor = `rgb(${red.getValue()}, ${green.getValue()}, ${blue.getValue()})`
+}
+
+// const volumeControl  = new Control(container1, {value: 50})
+//     volumeControl.onchange = value => console.log(value)
+
+const red = new Control(container1, {min:0, max:255})
+    red.onchange = setRgb
+
+const green = new Control(container1, {min:0, max:255})
+    green.onchange = setRgb
+
+const blue = new Control(container1, {min:0, max:255})
+    blue.onchange = setRgb
+
+// console.log(volumeControl.getValue())
+// setTimeout(() => volumeControl.setValue(80), 2000)
+
+
+
+

js/10/index.html → js/10/tasks/index.html


js/10/index.js → js/10/tasks/index.js