Browse Source

<HW_Functional OOP+rgb> done

Mark 1 year ago
parent
commit
44699677b5
5 changed files with 191 additions and 0 deletions
  1. 4 0
      10/index.html
  2. 96 0
      10/main.js
  3. BIN
      10/крутилка/1@3x.png
  4. 20 0
      10/крутилка/index.html
  5. 71 0
      10/крутилка/index.js

+ 4 - 0
10/index.html

@@ -9,6 +9,10 @@
 </head>
 
 <body>
+   <div id="login">
+   </div>
+   <div id="pv1">
+   </div>
    <script src="main.js"></script>
 </body>
 

+ 96 - 0
10/main.js

@@ -0,0 +1,96 @@
+// ДЗ: Functional OOP
+// Password
+function Password(parent, open) {
+   let pass = document.createElement('input')
+   pass.placeholder = 'enter password'
+   let check = document.createElement('input')
+   check.type = 'checkbox'
+   parent.append(pass, check)
+   check.onchange = () => {
+      this.setOpen(check.checked)
+   }
+   pass.oninput = () => {
+      if (typeof this.onChange === "function") {
+         this.onChange(pass.value);
+      }
+   };
+   this.setValue = (value) => (pass.value = value);
+   this.getValue = () => pass.value;
+   this.setOpen = function (open1) {
+      open = open1
+      check.checked = open
+      pass.type = open ? 'text' : 'password'
+      if (typeof this.onOpenChange === 'function') {
+         this.onOpenChange(open)
+      }
+   }
+   this.getOpen = function () {
+      return open
+   }
+}
+let p = new Password(document.body, false)
+p.onChange = data => console.log(data)
+p.onOpenChange = open => console.log(open)
+p.setValue('qwerty')
+console.log(p.getValue())
+p.setOpen(false)
+console.log(p.getOpen())
+
+// LoginForm
+
+function LoginForm(parent) {
+   let login = document.createElement('input')
+   login.placeholder = 'enter login'
+   let button = document.createElement('button')
+   button.innerText = 'push'
+   button.disabled = true
+   let pass = document.createElement('input')
+   pass.placeholder = 'enter password'
+   let check = document.createElement('input')
+   check.type = 'checkbox'
+   parent.append(login, pass, check, button)
+
+   check.onchange = () => {
+      this.setOpen(check.checked)
+   }
+   pass.oninput = () => {
+      if (typeof this.onChange === "function") {
+         this.onChange(pass.value);
+      }
+   }
+   this.setValue = (value) => (pass.value = value);
+   this.getValue = () => pass.value;
+   this.setOpen = function (newOpen) {
+      open = newOpen
+      check.checked = open
+      pass.type = open ? 'text' : 'password'
+      if (typeof this.onOpenChange === 'function') {
+         this.onOpenChange(open)
+      }
+   }
+   this.getOpen = function () {
+      return open
+   }
+   parent.oninput = function () {
+      if ((!login.value || !pass.value)) {
+         button.disabled = true
+      }
+      else {
+         button.disabled = false
+      }
+   }
+}
+let Form = new LoginForm(login)
+Form.onChange = data => console.log(data)
+Form.onOpenChange = open => console.log(open)
+Form.setValue('qwerty')
+console.log(Form.getValue())
+Form.setOpen(false)
+console.log(Form.getOpen())
+
+
+
+
+
+
+

BIN
10/крутилка/1@3x.png


+ 20 - 0
10/крутилка/index.html

@@ -0,0 +1,20 @@
+<!DOCTYPE html>
+<html>
+
+<head>
+   <meta charset="utf-8" />
+   <meta name="viewport" content="width=device-width" />
+   <title>Другой тайтл ПРИВЕТ 17й</title>
+   <style>
+      #container1 {
+         padding: 400px;
+      }
+   </style>
+</head>
+
+<body>
+   <div id='container1'></div>
+   <script src='index.js'></script>
+</body>
+
+</html>

+ 71 - 0
10/крутилка/index.js

@@ -0,0 +1,71 @@
+function Control(el, { value = 50,
+   min = 0,
+   max = 100,
+   minAngle = -90, //9 часов
+   maxAngle = 90,  //3 часа
+   wheelSpeed = 0.1,
+   imgUrl = '1@3x.png',
+   step = 3 } = {}) {
+
+   const img = document.createElement('img')
+   img.src = imgUrl
+   el.append(img)
+
+   //из value с учетом min max и minAngle maxAngle посчиитать угол
+   const ratio = (maxAngle - minAngle) / (max - min)
+   const getAngle = () => minAngle + ratio * value
+
+   this.setValue = newValue => {
+      if (newValue < min) {
+         newValue = min
+      }
+      if (newValue > max) {
+         newValue = max
+      }
+      if (typeof this.onchange === 'function' && newValue !== value) {
+         this.onchange(newValue)
+      }
+      value = newValue
+      img.style.transform = `rotate(${getAngle()}deg)`
+   }
+
+   img.onmousewheel = (event) => {
+      const { deltaY } = event
+      this.setValue(value + deltaY * wheelSpeed)
+      event.preventDefault()
+   }
+
+   img.onclick = (event) => {
+      const { layerX } = event
+      this.setValue(value + (layerX < img.width / 2 ? -step : step))
+   }
+
+   this.getValue = () => value
+   this.setValue(value)
+}
+
+
+
+const volumeControl = new Control(container1, { value: 0 })
+volumeControl.onchange = value => console.log('ON CHANGE', value) //на каждый setValue
+volumeControl.setValue(75)
+console.log(volumeControl.getValue())
+//пришейте к нему тэг audio для громкости
+//
+
+
+function setRGB() {
+   document.body.style.backgroundColor = `rgba(${red.getValue()},${green.getValue()},${blue.getValue()},1)`;
+}
+
+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
+
+//сделать три крутилки для RGB
+//и обновлять цвет блока/фона блока при вращении любой из трех
+
+setTimeout(() => volumeControl.setValue(80), 2000)