js.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. function Control(
  2. el,
  3. {
  4. min = 0,
  5. max = 255,
  6. step = 1,
  7. value = 0,
  8. onChange,
  9. minAngle = 0,
  10. wheelStepRatio = 0.01,
  11. maxAngle = 360,
  12. src = "1@3x.png",
  13. } = {}
  14. ) {
  15. this.value = value;
  16. const img = document.createElement("img");
  17. el.appendChild(img);
  18. img.src = src;
  19. const angleRatio = (maxAngle - minAngle) / (max - min);
  20. const getAngle = () => (value - min) * angleRatio + minAngle;
  21. const updateImg = (angle = getAngle()) =>
  22. (img.style.transform = `rotate(${angle}deg)`);
  23. updateImg();
  24. const updateValue = (st = step) => {
  25. value >= max ? (value -= max) : value < min ? (value += max) : value;
  26. value += st;
  27. onChange(Math.floor(value));
  28. updateImg();
  29. };
  30. img.onclick = (e) => {
  31. const { x: imgX, width: imgWidth } = img.getBoundingClientRect();
  32. const xOnImg = e.clientX - imgX;
  33. const st = xOnImg > imgWidth / 2 ? step : -step;
  34. updateValue(st);
  35. };
  36. img.onmousewheel = (e) => {
  37. const st = e.deltaY * wheelStepRatio;
  38. updateValue(st);
  39. };
  40. const toDeg = (rad) => (360 / (2 * Math.PI)) * rad;
  41. let startAngle;
  42. const getAngleByCoords = (x, y) => {
  43. const { x: imgX, y: imgY, width, height } = img.getBoundingClientRect();
  44. const xOnImg = x - imgX - width / 2;
  45. const yOnImg = y - imgY - height / 2;
  46. const angle = toDeg(Math.atan2(yOnImg, xOnImg));
  47. return angle;
  48. };
  49. img.onmousedown = (e) => {
  50. startAngle = getAngleByCoords(e.clientX, e.clientY);
  51. e.preventDefault();
  52. };
  53. img.onmousemove = (e) => {
  54. if (e.buttons && startAngle) {
  55. const angleDiff = getAngleByCoords(e.clientX, e.clientY) - startAngle;
  56. const angle = getAngle();
  57. updateImg(angle + angleDiff);
  58. }
  59. };
  60. img.onmouseup = (e) => {
  61. const angleDiff = getAngleByCoords(e.clientX, e.clientY) - startAngle;
  62. updateValue(angleDiff / angleRatio);
  63. startAngle = null;
  64. };
  65. }
  66. let rgb = { red: 0, green: 0, blue: 0 };
  67. function mixPalette(colors) {
  68. let { red, green, blue } = colors;
  69. let paletteForMix = document.querySelector("body");
  70. paletteForMix.style = `width:500px;height:500px;background:rgb(${red},${green},${blue})`;
  71. }
  72. let soundlRed = new Control(sound1, {
  73. max: 255,
  74. onChange: (value) => {
  75. sound1.style = `width:500px;background:rgb(${value},0,0)`;
  76. rgb.red = value;
  77. mixPalette(rgb);
  78. },
  79. });
  80. let soundlGreen = new Control(sound2, {
  81. max: 255,
  82. onChange: (value) => {
  83. sound2.style = `width:500px;background:rgb(0,${value},0)`;
  84. rgb.green = value;
  85. mixPalette(rgb);
  86. },
  87. });
  88. let soundlBlue = new Control(sound3, {
  89. max: 255,
  90. onChange: (value) => {
  91. sound3.style = `width:500px;background:rgb(0,0,${value})`;
  92. rgb.blue = value;
  93. mixPalette(rgb);
  94. },
  95. });