main.js 3.6 KB

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