LED.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. let lastPushId = ""
  2. let arduionoURL
  3. let openSlide = "animationsContainer"
  4. let data
  5. let numberOfLEDS
  6. let var_1 = 0
  7. let speed = 100
  8. fetch("./res/settings.json")
  9. .then(response => response.json())
  10. .then(json => {
  11. console.log(json)
  12. //data = JSON.parse(json)
  13. data = json
  14. arduionoURL = data.settings.ip
  15. numberOfLEDS = data.settings.num_lights
  16. console.log(data)
  17. })
  18. updateCanvas()
  19. function buttonPushAnimation(id) {
  20. if (id !== lastPushId) {
  21. document.getElementById(id).style.backgroundColor = "#2c2c36"
  22. if (lastPushId !== "") {
  23. document.getElementById(lastPushId).style.backgroundColor = "#252531"
  24. }
  25. lastPushId = id
  26. speed = data.animations[id].speed
  27. var_1 = data.animations[id].var_1
  28. document.getElementById("speed").value = speed
  29. document.getElementById("var_1").value = var_1
  30. httpGet(arduionoURL + "/" + id)
  31. }
  32. }
  33. function topBarClick(id) {
  34. if (id !== openSlide) {
  35. document.getElementById(id).classList.toggle("hide")
  36. document.getElementById(openSlide).classList.toggle("hide")
  37. openSlide = id
  38. updateCanvas()
  39. }
  40. }
  41. function setSpeed(value) {
  42. if (value !== speed) {
  43. speed = value
  44. document.getElementById("speed").value = value
  45. httpGet(arduionoURL + "/y1:" + value + ":;")
  46. }
  47. }
  48. function setVar_1(value) {
  49. if (value !== var_1) {
  50. var_1 = value
  51. document.getElementById("var_1").value = value
  52. try {
  53. httpGetAsync(arduionoURL + "/y2:" + value + ":;")
  54. } catch (e) {
  55. }
  56. }
  57. }
  58. function setSettings() {
  59. const nSpeed = document.getElementById("speed").value;
  60. const nVar_1 = document.getElementById("var_1").value;
  61. setSpeed(nSpeed)
  62. setVar_1(nVar_1)
  63. }
  64. function updateCanvas() {
  65. const canvas = document.getElementById("customCanvas")
  66. const context = canvas.getContext("2d")
  67. const numDivs = Math.floor(canvas.getBoundingClientRect().width / numberOfLEDS)
  68. for (let i = 0; i < numDivs; i++) {
  69. console.log(i)
  70. context.fillStyle = "#FFFFFF"
  71. context.fillRect(i*numberOfLEDS, 0, (i+1)*numberOfLEDS, canvas.getBoundingClientRect().height)
  72. }
  73. }
  74. function httpGet(theUrl, callback) {
  75. fetch(theUrl, {
  76. method: "POST"
  77. })
  78. .then(response => {
  79. if (!response.ok) {
  80. console.dir("Something went wrong with request: ", response)
  81. }
  82. })
  83. .catch(error => {
  84. console.log(error)
  85. })
  86. }