index.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width" />
  6. <link rel="stylesheet" href="style.css">
  7. <title>Control Buttons</title>
  8. <style>
  9. #container1 {
  10. padding: 400px;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <div id='colorMixer' style='width:50px; height:50px; border-radius:50%; border: 3px solid grey'></div>
  16. <div id='buttonContainer' class='buttonContainer'></div>>
  17. <div id='container1'></div>
  18. <script>
  19. function Control(el, { value = 0,
  20. min = 0,
  21. max = 100,
  22. minAngle = 0,
  23. maxAngle = 360,
  24. wheelSpeed = 0.05,
  25. step = 1 } = {}) {
  26. const img = document.createElement('img')
  27. img.src = '1@3x.png'
  28. el.append(img);
  29. img.className = "colorButton";
  30. const ratio = (maxAngle - minAngle) / (max - min)
  31. const getAngle = () => (value - min) * ratio + minAngle
  32. this.setValue = newValue => {
  33. if (newValue > max)
  34. newValue = max
  35. if (newValue < min)
  36. newValue = min
  37. if (typeof this.onchange === 'function' && newValue !== value)
  38. this.onchange(newValue)
  39. value = newValue
  40. img.style.transform = `rotate(${getAngle()}deg)`
  41. }
  42. img.onmousewheel = (event) => {
  43. const { deltaY } = event
  44. //console.log(deltaY)
  45. this.setValue(value + deltaY * wheelSpeed)
  46. event.preventDefault()
  47. }
  48. img.onclick = (event) => {
  49. const { layerX } = event
  50. if (layerX < img.width / 2)
  51. this.setValue(value - step)
  52. else
  53. this.setValue(value + step)
  54. event.preventDefault()
  55. }
  56. const event2Deg = event => {
  57. const { layerX, layerY } = event
  58. const { width, height } = img
  59. const x = layerX - width / 2
  60. const y = height / 2 - layerY
  61. return ((Math.atan2(y, x) / (2 * Math.PI)) * 360 + 360) % 360
  62. }
  63. let prevAngle = null
  64. img.onmousedown = (event) => {
  65. prevAngle = event2Deg(event)
  66. }
  67. img.onmousemove = (event) => {
  68. if (prevAngle === null) return
  69. const currentAngle = event2Deg(event)
  70. const deltaValue = (prevAngle - currentAngle) / ratio
  71. this.setValue(value + deltaValue)
  72. prevAngle = currentAngle
  73. event.preventDefault()
  74. }
  75. img.onmouseup = (event) => {
  76. prevAngle = null
  77. }
  78. this.setValue(value)
  79. this.getValue = () => value
  80. }
  81. const volumeControl = new Control(container1, {
  82. value: 75,
  83. min: 0,
  84. max: 100,
  85. minAngle: -90, maxAngle: 90
  86. })
  87. volumeControl.onchange = value => console.log('ON CHANGE', value) //на каждый setValue
  88. console.log(volumeControl.getValue())
  89. //пришейте к нему тэг audio для громкости
  90. function setRGB() {
  91. colorMixer.style.backgroundColor=`rgb(${red.getValue()},${green.getValue()},${blue.getValue()})`;
  92. //какой то блок, которому меняем цвет через rgba и цвета крутилок
  93. }
  94. const red = new Control(buttonContainer, {min: 0, max: 255})
  95. red.onchange = setRGB
  96. const green = new Control(buttonContainer, {min: 0, max: 255})
  97. green.onchange = setRGB
  98. const blue = new Control(buttonContainer, {min: 0, max: 255})
  99. blue.onchange = setRGB
  100. ////сделать три крутилки для RGB
  101. ////и обновлять цвет блока/фона блока при вращении любой из трех
  102. //setTimeout(() => volumeControl.setValue(80), 2000)
  103. </script>
  104. </body>
  105. </html>