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.

117 lines
2.4 KiB

4 years ago
4 years ago
4 years ago
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 >= gold && 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. empty: v === 0,
  28. full: v === 1000,
  29. })
  30. }
  31. return bars;
  32. }
  33. onMount(() => {
  34. return () => clearTimeout(timeout);
  35. })
  36. $: bars = update(smoothValue, goal);
  37. $: if (smoothValue !== value && timeout === null) {
  38. timeout = setInterval(() => {
  39. if (smoothValue < value) {
  40. smoothValue += 100;
  41. if (smoothValue > value) {
  42. smoothValue = value;
  43. }
  44. } else if (smoothValue > value) {
  45. smoothValue -= 100;
  46. if (smoothValue < value) {
  47. smoothValue = value;
  48. }
  49. } else {
  50. clearTimeout(timeout);
  51. timeout = null;
  52. }
  53. }, 1000/30);
  54. }
  55. </script>
  56. {#each bars as bar}
  57. <div class="bar" class:red={bar.red} class:green={bar.green} class:gold={bar.gold} class:empty={bar.empty} class:full={bar.full}>
  58. <div class="content" style={`height: ${bar.value / 10}%; top: ${(1000 - bar.value) / 10}%`}>
  59. </div>
  60. </div>
  61. {/each}
  62. <style>
  63. div.bar {
  64. display: inline-block;
  65. background: #111;
  66. width: 1.5ch;
  67. height: 1em;
  68. margin-right: 0.5ch;
  69. }
  70. div.bar > div.content {
  71. width: 100%;
  72. position: relative;
  73. }
  74. div.bar.green {
  75. border: 0.1px solid #4da069;
  76. }
  77. div.bar.green > div.content {
  78. background-color: #4da069;
  79. }
  80. div.bar.green.full > div.content {
  81. background-color: #45fc82;
  82. box-shadow: 0px 0px 2px #45fc82;
  83. }
  84. div.bar.gold {
  85. border: 0.1px solid #9b7a1f;
  86. }
  87. div.bar.gold > div.content {
  88. background-color: #9b7a1f;
  89. }
  90. div.bar.gold.full > div.content {
  91. background-color: #ffc936;
  92. box-shadow: 0px 0px 2px #ffc936;
  93. }
  94. div.bar.red {
  95. border: 0.1px solid #880000;
  96. }
  97. div.bar.red > div.content {
  98. background-color: #880000;
  99. }
  100. div.bar.red.full > div.content {
  101. background-color: #bb0000;
  102. box-shadow: 0px 0px 2px #ff0000;
  103. }
  104. </style>