index.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. function Control(
  2. el,
  3. {
  4. value = 10,
  5. min = 0,
  6. max = 100,
  7. minAngle = -90,
  8. maxAngle = 90,
  9. wheelSpeed = 0.01,
  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);
  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. this.setValue(value);
  44. }
  45. function setRGB() {
  46. let a = red.getValue(value * wheelSpeed);
  47. let b = green.getValue(value * wheelSpeed);
  48. let c = blue.getValue(value * wheelSpeed);
  49. let rgbDone = (div.style.background = `rgb(${a}, ${b}, ${c})`);
  50. return console.log(rgbDone);
  51. }
  52. const red = new Control(container1, { min: 0, max: 255 });
  53. red.onchange = setRGB();
  54. const green = new Control(container1, { min: 0, max: 255 });
  55. green.onchange = setRGB();
  56. const blue = new Control(container1, { min: 0, max: 255 });
  57. blue.onchange = setRGB();
  58. const volumeControl = new Control(container1, { value: 50 });
  59. volumeControl.onchange = (value) => {
  60. audio.volume = value / 100;
  61. console.log("ON CHANGE", value);
  62. };
  63. const audio = document.createElement("audio");
  64. audio.setAttribute("controls", "");
  65. audio.src = "/media/klychko.mp3";
  66. document.body.append(audio);
  67. const div = document.createElement("div");
  68. div.style.width = "300px";
  69. div.style.height = "300px";
  70. document.body.append(div);