main.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. function Control(el, { value = 0,
  2. min = 0,
  3. max = 100,
  4. minAngle = 0,
  5. maxAngle = 270,
  6. width = 200,
  7. wheelSpeed = 0.1,
  8. step = 1 } = {}) {
  9. const img = document.createElement('img')
  10. img.src = '1@3x.png'
  11. img.width = width
  12. el.append(img)
  13. const ratio = (maxAngle - minAngle) / (max - min)// сколько градусов 1 ед. велью
  14. const getAngle = () => (value - min) * ratio + minAngle // текущий угол ползунка
  15. this.setValue = newValue => { //проверить, вдруг в этом объекте есть onchange
  16. if (newValue > max) newValue = max
  17. if (newValue < min) newValue = min
  18. if (typeof this.onchange === 'function') {
  19. this.onchange(value)
  20. }
  21. value = newValue //и запустить его с новым value
  22. img.style.transform = `rotate(${getAngle()}deg)`
  23. }
  24. this.getValue = () => value
  25. this.onchange = value => value
  26. img.onmousewheel = e => {
  27. const { deltaY } = e
  28. const newValue = value + deltaY * wheelSpeed
  29. this.setValue(newValue)
  30. e.preventDefault()
  31. }
  32. img.onclick = e => {
  33. const { layerX } = e
  34. const { width } = img
  35. layerX > width / 2 ? this.setValue(value + step) : this.setValue(value - step)
  36. }
  37. const toDeg = rad => ((rad * 180) / Math.PI + 360 + 90) % 360
  38. let prevMouseAngle = null
  39. img.onmousedown = e => {
  40. const y = e.layerY - img.height / 2
  41. const x = e.layerX - img.width / 2
  42. prevMouseAngle = toDeg(Math.atan2(y, x))
  43. e.preventDefault()
  44. }
  45. img.onmousemove = e => {
  46. if (prevMouseAngle === null) return
  47. const y = e.layerY - img.height / 2
  48. const x = e.layerX - img.width / 2
  49. let currentAngle = toDeg(Math.atan2(y, x))
  50. let moveAngleDiff = (currentAngle - prevMouseAngle)
  51. this.setValue(value + moveAngleDiff / ratio)
  52. prevMouseAngle = currentAngle
  53. }
  54. img.onmouseout = img.onmouseup = () => {
  55. prevMouseAngle = null
  56. }
  57. this.setValue(value)
  58. }
  59. // const volumeControl = new Control(container1, { value: 13, min: 0, max: 100, minAngle: 0, maxAngle: 270, width: 150 })
  60. // volumeControl.onchange = value => console.log(value) //на каждый setValue
  61. // console.log(volumeControl.getValue())
  62. // setTimeout(() => volumeControl.setValue(80), 2000)
  63. //пришейте к нему тэг audio для громкости
  64. function setRGB() {
  65. box.style.backgroundColor = `rgb(${red.getValue().toFixed(0)},${green.getValue().toFixed(0)},${blue.getValue().toFixed(0)})`
  66. box.children[0].innerText = `color rgb:${red.getValue().toFixed(0)},${green.getValue().toFixed(0)},${blue.getValue().toFixed(0)}`
  67. }
  68. const red = new Control(container1, { min: 0, max: 255, width: 150 })
  69. red.onchange = setRGB
  70. const green = new Control(container1, { min: 0, max: 255, width: 150 })
  71. green.onchange = setRGB
  72. const blue = new Control(container1, { min: 0, max: 255, width: 150 })
  73. blue.onchange = setRGB