script.js 3.1 KB

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