scripts.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. function Control(el, { value = 0,
  2. min = 0,
  3. max = 100,
  4. minAngle = 0,
  5. maxAngle = 360,
  6. wheelSpeed = 0.1,
  7. step = 1 } = {}) {
  8. const img = document.createElement('img')
  9. img.src = '1@3x.png'
  10. img.style.width = '100%'
  11. img.style.height = 'auto'
  12. el.append(img)
  13. let forceChange = new Event('change')
  14. this.currentValue = document.createElement('input')
  15. this.currentValue.type = 'number'
  16. this.currentValue.value = value
  17. const ratio = (maxAngle - minAngle) / (max - min)
  18. const getAngle = () => (value - min) * ratio + minAngle
  19. this.getValue = () => {
  20. return value
  21. }
  22. this.setValue = newValue => {
  23. if (newValue > max) {
  24. newValue = max
  25. }
  26. if (newValue < min) {
  27. newValue = min
  28. }
  29. value = newValue
  30. this.currentValue.value = value
  31. this.currentValue.dispatchEvent(forceChange)
  32. img.style.transform = `rotate(${getAngle()}deg)`
  33. }
  34. img.onmousewheel = e => {
  35. const { deltaY } = e
  36. const newValue = value + deltaY * wheelSpeed
  37. this.setValue(newValue)
  38. e.preventDefault()
  39. }
  40. img.onclick = e => {
  41. const { layerX } = e
  42. const { width } = img
  43. layerX > width / 2? this.setValue(value + step) : this.setValue(value - step)
  44. }
  45. const toDeg = rad => ((rad * 180) / Math.PI + 360 + 90) % 360
  46. let prevMouseAngle = null
  47. img.onmousedown = e => {
  48. const y = e.layerY - img.height / 2
  49. const x = e.layerX - img.width / 2
  50. prevMouseAngle = toDeg(Math.atan2(y, x))
  51. e.preventDefault()
  52. }
  53. img.onmousemove = e => {
  54. if (prevMouseAngle === null) return
  55. const y = e.layerY - img.height / 2
  56. const x = e.layerX - img.width / 2
  57. let currentAngle = toDeg(Math.atan2(y, x))
  58. let moveAngleDiff = (currentAngle - prevMouseAngle)
  59. this.setValue(value + moveAngleDiff / ratio)
  60. prevMouseAngle = currentAngle
  61. }
  62. img.onmouseout = img.onmouseup = () => {
  63. prevMouseAngle = null
  64. }
  65. this.setValue(value)
  66. }
  67. let track = document.getElementById('track')
  68. let audioSpin = document.getElementById('audio-spin')
  69. const volumeControl = new Control(audioSpin, { value: 50, min: 0, max: 100, minAngle: 0, maxAngle: 360 })
  70. track.volume = volumeControl.getValue() / 100
  71. volumeControl.currentValue.onchange = () => track.volume = volumeControl.getValue() / 100
  72. // RGB
  73. const Red = new Control(red, {min: 0, max: 255})
  74. const Green = new Control(green, {value: 200, min: 0, max: 255})
  75. const Blue = new Control(blue, {value: 125, min: 0, max: 255})
  76. let setRGB = (element) => {
  77. element.style.backgroundColor = `rgb(${Red.getValue()||0}, ${Green.getValue()||0}, ${Blue.getValue()||0})`
  78. }
  79. setRGB(document.getElementById('color-window'))
  80. Red.currentValue.onchange = () => setRGB(document.getElementById('color-window'))
  81. Green.currentValue.onchange = () => setRGB(document.getElementById('color-window'))
  82. Blue.currentValue.onchange = () => setRGB(document.getElementById('color-window'))