index.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. function Control(el, {value=50,
  2. min=0,
  3. max=100,
  4. minAngle=-90,
  5. maxAngle=90,
  6. wheelSpeed=0.1,
  7. step=1}={}){
  8. const img = document.createElement('img')
  9. img.src = '13x.png'
  10. el.append(img)
  11. const ratio = (maxAngle - minAngle) / (max - min) //цена единицы value в градусах
  12. const getAngle = () => (value - min) * ratio + minAngle //подсчитали отступ в единицах, пересчитали в градусы и прибавили к стартовому углу
  13. this.setValue = (newValue) => {
  14. if (newValue < min){
  15. newValue = min
  16. }
  17. if (newValue > max){
  18. newValue = max
  19. }
  20. value = newValue
  21. if (typeof this.onchange === 'function'){
  22. this.onchange(value)
  23. }
  24. img.style.transform = `rotate(${getAngle()}deg)`
  25. }
  26. this.getValue = () => value
  27. img.onmousewheel = (e) => {
  28. const {deltaY} = e
  29. //console.log(deltaY)
  30. e.preventDefault()
  31. this.setValue(value + deltaY*wheelSpeed)
  32. }
  33. img.onclick = (e) => {
  34. const {layerX} = e
  35. console.log(e, layerX)
  36. if (layerX > img.width/2)
  37. this.setValue(value +step)
  38. else
  39. this.setValue(value -step)
  40. }
  41. this.setValue(value)
  42. }
  43. let $song = document.querySelector('.mySong')
  44. const volumeControl = new Control(container1, {max: 1, wheelSpeed: 0.001, value: 75, minAngle: -180})
  45. volumeControl.onchange = (value => {
  46. $song.volume = value
  47. })
  48. setTimeout(() => volumeControl.setValue(80), 2000)
  49. function setRGB(value, n){
  50. colorBox.style.backgroundColor = `rgb(${red.getValue()}, ${green.getValue()}, ${blue.getValue()})`
  51. }
  52. const red = new Control(container2, {min: 0, max: 255})
  53. const green = new Control(container2, {min: 0, max: 255})
  54. const blue = new Control(container2, {min: 0, max: 255})
  55. const colors = [red, green, blue];
  56. for(let i = 0; i < colors.length; i++) {
  57. colors[i].onchange = value => setRGB(value, i)
  58. }