index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. function Control(el, {
  2. value = 0,
  3. min = 0,
  4. max = 100,
  5. minAngle = 0,
  6. maxAngle = 360,
  7. wheelSpeed = 0.05,
  8. step = 1
  9. } = {}) {
  10. const img = document.createElement('img')
  11. img.src = '1@3x.png'
  12. el.append(img)
  13. const ratio = (maxAngle - minAngle) / (max - min)
  14. const getAngle = () => (value - min) * ratio + minAngle
  15. this.setValue = newValue => {
  16. if (newValue > max)
  17. newValue = max
  18. if (newValue < min)
  19. newValue = min
  20. if (typeof this.onchange === 'function' && newValue !== value)
  21. this.onchange(newValue)
  22. value = newValue
  23. img.style.transform = `rotate(${getAngle()}deg)`
  24. }
  25. img.onmousewheel = (event) => {
  26. const { deltaY } = event
  27. //console.log(deltaY)
  28. this.setValue(value + deltaY * wheelSpeed)
  29. event.preventDefault()
  30. }
  31. img.onclick = (event) => {
  32. const { layerX } = event
  33. if (layerX < img.width / 2)
  34. this.setValue(value - step)
  35. else
  36. this.setValue(value + step)
  37. event.preventDefault()
  38. }
  39. const event2Deg = event => {
  40. const { layerX, layerY } = event
  41. const { width, height } = img
  42. const x = layerX - width / 2
  43. const y = height / 2 - layerY
  44. //console.log(x, y, width, height)
  45. return ((Math.atan2(y, x) / (2 * Math.PI)) * 360 + 360) % 360
  46. }
  47. let prevAngle = null
  48. img.onmousedown = (event) => {
  49. prevAngle = event2Deg(event)
  50. }
  51. img.onmousemove = (event) => {
  52. if (prevAngle === null) return
  53. const currentAngle = event2Deg(event)
  54. const deltaValue = (prevAngle - currentAngle) / ratio
  55. //console.log(prevAngle - currentAngle, deltaValue)
  56. this.setValue(value + deltaValue)
  57. prevAngle = currentAngle
  58. event.preventDefault()
  59. }
  60. img.onmouseup = (event) => {
  61. prevAngle = null
  62. }
  63. this.setValue(value)
  64. this.getValue = () => value
  65. }
  66. const volumeControl = new Control(container1, {
  67. value: 75,
  68. min: 0,
  69. max: 100,
  70. minAngle: -90,
  71. maxAngle: 90
  72. })
  73. volumeControl.onchange = value => console.log('ON CHANGE', value) //на каждый setValue
  74. console.log(volumeControl.getValue())
  75. // тэг audio для громкости
  76. const music = document.querySelector(".music");
  77. const Musvolume = new Control(containerMusic, { min: 0, max: 100 });
  78. Musvolume.onchange = (value) => { music.volume = value / 100 };
  79. const red = new Control(container1, { min: 0, max: 255 })
  80. const green = new Control(container1, { min: 0, max: 255 })
  81. const blue = new Control(container1, { min: 0, max: 255 })
  82. function setRGB() {
  83. document.body.style.backgroundColor = `rgb(${red.getValue()},${green.getValue()},${blue.getValue()})`
  84. }
  85. red.onchange = setRGB
  86. green.onchange = setRGB
  87. blue.onchange = setRGB