script.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. function Control(img, {
  2. value = 0, step = 1, max = 255, min = 0, maxAngle = 360, minAngle = 0},
  3. inputId) {
  4. this.input = document.createElement('input');
  5. this.input.setAttribute('type', 'number');
  6. document.body.append(this.input);
  7. this.input.id = inputId;
  8. const ratio = (maxAngle - minAngle) / (max - min);
  9. const changeStep = () => (value - min) * ratio + minAngle;
  10. this.setValue = (newValue) => {
  11. if (newValue > max) newValue = max;
  12. if (newValue < min) newValue = min;
  13. value = newValue;
  14. if (typeof this.onChange === 'function') this.onChange(value);
  15. img.style.transform = `rotate(${changeStep()}deg)`;
  16. };
  17. const {
  18. left
  19. } = img.getBoundingClientRect();
  20. this.changeValue = delta => this.setValue(value + delta);
  21. img.onmousewheel = (e) => {
  22. e.preventDefault();
  23. this.changeValue(e.deltaY * step / 10)
  24. };
  25. img.onclick = (e) => {
  26. this.changeValue(e.clientX - left > img.width / 2 ? step : -step)
  27. };
  28. this.setValue(value);
  29. this.input.oninput = () => this.setValue(+value)
  30. this.onChange = function (value) {
  31. input.value = value
  32. }
  33. }
  34. const redControl = new Control(document.getElementById('red'),
  35. {value: 0, min: 0, max: 255, minAngle: 0, maxAngle: 360},'inputRed');
  36. const greenControl = new Control(document.getElementById('green'),
  37. {value: 0, min: 0, max: 255, minAngle: 0, maxAngle: 360}, 'inputGreen');
  38. const blueControl = new Control(document.getElementById('blue'),
  39. {value: 0, min: 0, max: 255, minAngle: 0, maxAngle: 360}, 'inputBlue');
  40. redControl.onChange = function (value) {
  41. inputRed.value = value
  42. }
  43. greenControl.onChange = function (value) {
  44. inputGreen.value = value
  45. }
  46. blueControl.onChange = function (value) {
  47. inputBlue.value = value
  48. }