index.js 3.2 KB

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