Browse Source

Rgb Control

makstravm 3 years ago
parent
commit
e5b7d3cc15
4 changed files with 142 additions and 0 deletions
  1. BIN
      HW10/1@3x.png
  2. 24 0
      HW10/index.html
  3. 89 0
      HW10/main.js
  4. 29 0
      HW10/style.css

BIN
HW10/1@3x.png


+ 24 - 0
HW10/index.html

@@ -0,0 +1,24 @@
+<!DOCTYPE html>
+<html lang="en-ru">
+
+<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">
+  <link rel="stylesheet" href="style.css">
+  <title>HW9</title>
+
+</head>
+
+
+<body>
+  <div class="container">
+    <div id='container1'></div>
+    <div id="box"><span></span></div>
+  </div>
+
+  <script src="main.js"></script>
+
+</body>
+
+</html>

+ 89 - 0
HW10/main.js

@@ -0,0 +1,89 @@
+function Control(el, { value = 0,
+  min = 0,
+  max = 100,
+  minAngle = 0,
+  maxAngle = 270,
+  width = 200,
+  wheelSpeed = 0.1,
+  step = 1 } = {}) {
+
+  const img = document.createElement('img')
+  img.src = '1@3x.png'
+  img.width = width
+  el.append(img)
+
+  const ratio = (maxAngle - minAngle) / (max - min)// сколько градусов 1 ед. велью
+  const getAngle = () => (value - min) * ratio + minAngle // текущий угол ползунка
+
+  this.setValue = newValue => { //проверить, вдруг в этом объекте есть onchange
+    if (newValue > max) newValue = max
+    if (newValue < min) newValue = min
+    if (typeof this.onchange === 'function') {
+      this.onchange(value)
+    }
+    value = newValue //и запустить его с новым value
+    img.style.transform = `rotate(${getAngle()}deg)`
+  }
+  this.getValue = () => value
+  this.onchange = value => value
+
+  img.onmousewheel = e => {
+    const { deltaY } = e
+    const newValue = value + deltaY * wheelSpeed
+    this.setValue(newValue)
+    e.preventDefault()
+  }
+
+  img.onclick = e => {
+    const { layerX } = e
+    const { width } = img
+    layerX > width / 2 ? this.setValue(value + step) : this.setValue(value - step)
+  }
+
+  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
+    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)
+}
+
+// const volumeControl = new Control(container1, { value: 13, min: 0, max: 100, minAngle: 0, maxAngle: 270, width: 150 })
+// volumeControl.onchange = value => console.log(value) //на каждый setValue
+// console.log(volumeControl.getValue())
+// setTimeout(() => volumeControl.setValue(80), 2000)
+//пришейте к нему тэг audio для громкости
+
+function setRGB() {
+  box.style.backgroundColor = `rgb(${red.getValue().toFixed(0)},${green.getValue().toFixed(0)},${blue.getValue().toFixed(0)})`
+  box.children[0].innerText = `color rgb:${red.getValue().toFixed(0)},${green.getValue().toFixed(0)},${blue.getValue().toFixed(0)}`
+}
+
+const red = new Control(container1, { min: 0, max: 255, width: 150 })
+red.onchange = setRGB
+const green = new Control(container1, { min: 0, max: 255, width: 150 })
+green.onchange = setRGB
+const blue = new Control(container1, { min: 0, max: 255, width: 150 })
+blue.onchange = setRGB
+
+
+

+ 29 - 0
HW10/style.css

@@ -0,0 +1,29 @@
+body {
+  font-size: 10px;
+  line-height: 10px;
+  box-sizing: border-box;
+  font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
+}
+.container {
+  max-width: 1200px;
+  padding: 15px;
+  text-align: center;
+}
+#box {
+  width: 100%;
+  height: 35vh;
+  border: 1px solid #000000;
+  display: flex;
+  align-items: center;
+  justify-content: center;
+  font-size: 2em;
+}
+img {
+  border-radius: 50%;
+  overflow: hidden;
+}
+span {
+  background-color: #fff;
+  padding: 0 10px;
+  line-height: 2em;
+}