script.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. function Control(el, {min=0,
  2. max=100,
  3. step=1,
  4. value=100,
  5. onChange,
  6. minAngle=0,
  7. wheelStepRatio=0.01,
  8. maxAngle=360,
  9. src='img/1@3x.png'} = {}){
  10. const img = document.createElement('img');
  11. el.appendChild(img);
  12. img.src = src;
  13. const angleRatio = (maxAngle - minAngle) / (max - min);
  14. const getAngle = () => {
  15. const offset = value - min;
  16. const angleOffset = offset * angleRatio;
  17. const angle = minAngle + angleOffset;
  18. return angle
  19. };
  20. const updateImg = (angle=getAngle()) => img.style.transform = `rotate(${angle}deg)`;
  21. updateImg();
  22. const updateValue = (st=step) => {
  23. value >= max ? value -= max : value < 0 ? value += max : value;
  24. value += st;
  25. updateImg();
  26. };
  27. img.onclick = (e) => {
  28. console.log(e);
  29. const {x: imgX, width: imgWidth } = img.getBoundingClientRect();
  30. const xOnImg = e.clientX - imgX;
  31. const st = xOnImg > imgWidth/2 ? step: -step;
  32. updateValue(st);
  33. };
  34. img.onmousewheel = e => {
  35. const st = e.deltaY*wheelStepRatio;
  36. updateValue(st);
  37. };
  38. const toDeg = rad => (360/(2*Math.PI))*rad;
  39. let start;
  40. const getAngleByCoords = (x, y) => {
  41. const {x: imgX, y: imgY, width, height} = img.getBoundingClientRect();
  42. const xOnImg = e.clientX - imgX - width/2;
  43. const yOnImg = e.clientY - imgY - height/2;
  44. return toDeg(Math.atan2(yOnImg, xOnImg));
  45. };
  46. img.onmousedown = e => {
  47. start = getAngleByCoords(e);
  48. e.preventDefault();
  49. };
  50. img.onmousemove = e => {
  51. if (e.buttons && start){
  52. const angleDiff = getAngleByCoords(e)- startAngle;
  53. const angle = getAngle();
  54. updateImg(angle + angleDiff);
  55. };
  56. };
  57. img.onmouseup = e => {
  58. let inValue = endAngle / ((maxAngle - minAngle) / (max - min))%100;
  59. value = inValue < 0 ? 75 + (25 + inValue) : inValue;
  60. updateImg(endAngle);
  61. };
  62. this.getValue = function () {
  63. return Math.floor(value);
  64. }
  65. }
  66. let controlRed = new Control(control1, max = 255);
  67. let controlGreen = new Control(control2, max = 255);
  68. let controlBlue = new Control(control3, max = 255);
  69. controlRed.setValue;
  70. controlGreen.setValue;
  71. controlBlue.setValue;
  72. let body = document.querySelector('body');
  73. control1.style = `width:362px;height:362px;border-radius: 50%;border: solid 10px #fe3f44;background:#fe3f44`;
  74. control2.style = `width:362px;height:362px;border-radius: 50%;border: solid 10px #80ea69;background:#80ea69`;
  75. control3.style = `width:362px;height:362px;border-radius: 50%;border: solid 10px #3e94d1; background:#3e94d1`;
  76. body.onmousemove = () => { //я хз
  77. control4.style = `background:rgba(${controlRed.getValue()},${controlGreen.getValue()},${controlBlue.getValue()},1);height: 362px;width: 362px;border-radius: 50%; border:solid 10px pink;`;
  78. };