index.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. function Control(
  2. el,
  3. {
  4. value = 10,
  5. min = 0,
  6. max = 100,
  7. minAngle = -90,
  8. maxAngle = 90,
  9. wheelSpeed = 0.1,
  10. step = 1,
  11. } = {}
  12. ) {
  13. const img = document.createElement("img");
  14. img.src = "img/1@3x.png";
  15. el.append(img);
  16. const ratio = (maxAngle - minAngle) / (max - min); //цена единицы value в градусах
  17. const getAngle = () => (value - min) * ratio + minAngle; //подсчитали отступ в единицах, пересчитали в градусы и прибавили к стартовому углу
  18. this.setValue = (newValue) => {
  19. if (newValue < min) {
  20. newValue = min;
  21. }
  22. if (newValue > max) {
  23. newValue = max;
  24. }
  25. value = newValue;
  26. if (typeof this.onchange === "function") {
  27. this.onchange(value);
  28. }
  29. img.style.transform = `rotate(${getAngle()}deg)`;
  30. };
  31. this.getValue = () => value;
  32. img.onmousewheel = (e) => {
  33. const { deltaY } = e;
  34. e.preventDefault();
  35. this.setValue(value + deltaY * wheelSpeed);
  36. };
  37. img.onclick = (e) => {
  38. const { layerX } = e;
  39. console.log(e, layerX);
  40. if (layerX > img.width / 2) this.setValue(value + step);
  41. else this.setValue(value - step);
  42. };
  43. // audio.onmousewheel = (e) => {
  44. // this.setValue(value + deltaY * wheelSpeed);
  45. // }
  46. this.setValue(value);
  47. }
  48. function setRGB() {
  49. let a = red.getValue();
  50. let b = green.getValue();
  51. let c = blue.getValue();
  52. let rgbDone = (div.style.background = `rgb(${a}, ${b}, ${c})`);
  53. return console.log(rgbDone);
  54. }
  55. const red = new Control(container1, { min: 0, max: 255 });
  56. red.onchange = setRGB;
  57. const green = new Control(container1, { min: 0, max: 255 });
  58. green.onchange = setRGB;
  59. const blue = new Control(container1, { min: 0, max: 255 });
  60. blue.onchange = setRGB;
  61. const volumeControl = new Control(container1, { value: 50 });
  62. volumeControl.onchange = (value) => {
  63. audio.volume = value / 100;
  64. console.log("ON CHANGE", value);
  65. };
  66. const audio = document.createElement("audio");
  67. audio.setAttribute("controls", "");
  68. audio.src = "/media/klychko.mp3";
  69. document.body.append(audio);
  70. const div = document.createElement("div");
  71. div.style.width = "300px";
  72. div.style.height = "300px";
  73. document.body.append(div);
  74. ////сделать три крутилки для RGB
  75. ////и обновлять цвет блока/фона блока при вращении любой из трех
  76. //setTimeout(() => volumeControl.setValue(80), 2000)