Plan stuff. Log stuff.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
2.3 KiB

4 years ago
  1. <script>
  2. import { onMount } from "svelte";
  3. export let value;
  4. export let goal;
  5. let smoothValue = value;
  6. let bars = [];
  7. let timeout = null;
  8. function update(value, goal) {
  9. const bars = [];
  10. let gold = (goal / 1000);
  11. let red = gold + 3;
  12. for (let i = 0;; ++i) {
  13. let v = value - (i * 1000);
  14. if (v > 1000) {
  15. v = 1000;
  16. } else if (v < 0) {
  17. v = 0;
  18. }
  19. if (i >= red && v === 0) {
  20. break;
  21. }
  22. bars.push({
  23. green: i < gold,
  24. gold: i >= gold && i < red,
  25. red: i >= red,
  26. value: v,
  27. full: v === 1000,
  28. })
  29. }
  30. return bars;
  31. }
  32. onMount(() => {
  33. return () => clearTimeout(timeout);
  34. })
  35. $: bars = update(smoothValue, goal);
  36. $: if (smoothValue !== value && timeout === null) {
  37. timeout = setInterval(() => {
  38. if (smoothValue < value) {
  39. smoothValue += 100;
  40. if (smoothValue > value) {
  41. smoothValue = value;
  42. }
  43. } else if (smoothValue > value) {
  44. smoothValue -= 100;
  45. if (smoothValue < value) {
  46. smoothValue = value;
  47. }
  48. } else {
  49. clearTimeout(timeout);
  50. timeout = null;
  51. }
  52. }, 1000/30);
  53. }
  54. </script>
  55. {#each bars as bar}
  56. <div class="bar" class:red={bar.red} class:green={bar.green} class:gold={bar.gold} class:full={bar.full}>
  57. <div class="content" style={`height: ${bar.value / 10}%; top: ${(1000 - bar.value) / 10}%`}>
  58. </div>
  59. </div>
  60. {/each}
  61. <style>
  62. div.bar {
  63. display: inline-block;
  64. background: #111;
  65. width: 1.5ch;
  66. height: 1em;
  67. margin-right: 0.5ch;
  68. }
  69. div.bar > div.content {
  70. width: 100%;
  71. position: relative;
  72. }
  73. div.bar.green {
  74. border: 0.1px solid #4da069;
  75. }
  76. div.bar.green > div.content {
  77. background-color: #4da069;
  78. }
  79. div.bar.green.full > div.content {
  80. background-color: #45fc82;
  81. box-shadow: 0px 0px 2px #45fc82;
  82. }
  83. div.bar.gold {
  84. border: 0.1px solid #9b7a1f;
  85. }
  86. div.bar.gold > div.content {
  87. background-color: #9b7a1f;
  88. }
  89. div.bar.gold.full > div.content {
  90. background-color: #ffc936;
  91. box-shadow: 0px 0px 2px #ffc936;
  92. }
  93. div.bar.red {
  94. border: 0.1px solid #880000;
  95. }
  96. div.bar.red > div.content {
  97. background-color: #880000;
  98. }
  99. div.bar.red.full > div.content {
  100. background-color: #bb0000;
  101. box-shadow: 0px 0px 2px #ff0000;
  102. }
  103. </style>