main.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. let container1 = document.querySelector(".container");
  2. let audio = document.querySelector("audio");
  3. function Control(el, {value=0,
  4. min=0,
  5. max=100,
  6. minAngle=0,
  7. maxAngle=360,
  8. wheelSpeed=0.05,
  9. step=1}={}){
  10. console.log(step);
  11. const img = document.createElement('img')
  12. img.src = '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 volumeControl = new Control(container1, {value: 0.5,
  68. min: 0,
  69. max: 1,
  70. minAngle: -90, maxAngle: 90, step: 0.1})
  71. volumeControl.onchange = () => audio.volume = volumeControl.getValue(); //на каждый setValue
  72. //пришейте к нему тэг audio для громкости
  73. function setRGB(){
  74. container1.style.backgroundColor = `rgb(${red.getValue()}, ${green.getValue()}, ${blue.getValue()})`
  75. }
  76. const red = new Control(container1, {min: 0, max: 255})
  77. red.onchange = setRGB
  78. const green = new Control(container1, {min: 0, max: 255})
  79. green.onchange = setRGB
  80. const blue = new Control(container1, {min: 0, max: 255})
  81. blue.onchange = setRGB
  82. setRGB();
  83. ////сделать три крутилки для RGB
  84. ////и обновлять цвет блока/фона блока при вращении любой из трех
  85. //setTimeout(() => volumeControl.setValue(80), 2000)