logo

1-pixel-wealth

Source code for <https://mkorostoff.github.io/1-pixel-wealth/>: Wealth shown to scale

main.js (3855B)


  1. var bezos = document.getElementById('bezos');
  2. var bezos_counter = document.getElementById('bezos-counter');
  3. var bezosCounterStart = document.getElementById('bezos-counter-start');
  4. var four_hundred = document.getElementById('four-hundred');
  5. var four_hundred_counter = document.getElementById('four-hundred-counter');
  6. var four_hundred_counter_start = document.getElementById('four-hundred-counter-start');
  7. var sixtyPercent = document.getElementById('sixty-percent');
  8. var sixtyPercentIndicator = document.getElementById('sixty-percent-indicator');
  9. var sixtyPercentScrollPercentage = 0.0;
  10. var babies = document.getElementById('babies-wrapper');
  11. var baby_counter = document.getElementById('baby-counter');
  12. var thousand = new Intl.NumberFormat('en-US')
  13. var money = new Intl.NumberFormat('en-US', {
  14. style: 'currency',
  15. currency: 'USD',
  16. minimumFractionDigits: 0,
  17. maximumFractionDigits: 0,
  18. });
  19. var additional_instructions_shown = false;
  20. function detect_confused_user(e, timer) {
  21. if (!additional_instructions_shown) {
  22. additional_instructions_shown = true;
  23. setTimeout(function(){
  24. if (window.scrollX < 1) {
  25. document.getElementById('instructions').classList.add("show");
  26. }
  27. }, timer);
  28. }
  29. }
  30. function detect_slightly_confused_user(e, timer) {
  31. detect_confused_user(e, 2000);
  32. }
  33. function detect_very_confused_user(e, timer) {
  34. detect_confused_user(e, 4500);
  35. }
  36. if (window.innerWidth > 450) {
  37. document.addEventListener("mousemove", detect_very_confused_user, {once: true});
  38. document.addEventListener("mousewheel", detect_slightly_confused_user, {once: true});
  39. document.addEventListener("DOMMouseScroll", detect_slightly_confused_user, {once: true});
  40. }
  41. window.addEventListener('scroll', function(){
  42. update_wealth_counter();
  43. });
  44. function generate_sixty_percent() {
  45. for (var i = 0; i < 100; i++) {
  46. var node = document.createElement("div");
  47. node.classList = "people";
  48. if (i === 0) {
  49. node.classList += " first";
  50. }
  51. document.getElementById("sixty-percent").appendChild(node);
  52. }
  53. }
  54. generate_sixty_percent();
  55. sixtyPercent.addEventListener('scroll', function(){
  56. let newScroll = ((sixtyPercent.scrollTop / sixtyPercent.scrollHeight) * 60).toFixed(1);
  57. if (sixtyPercentScrollPercentage !== newScroll) {
  58. sixtyPercentScrollPercentage = newScroll;
  59. sixtyPercentIndicator.innerHTML = newScroll + '%';
  60. }
  61. })
  62. babies.addEventListener('scroll', function(){
  63. let is_mobile = window.innerWidth <= 450;
  64. let bg_size = (is_mobile) ? 68 : 160;
  65. baby_counter.innerHTML = thousand.format(Math.floor(babies.scrollTop / bg_size * 5));
  66. })
  67. function update_wealth_counter() {
  68. if (bezos_viewable()) {
  69. if (bezos_counter_viewable()) {
  70. let wealth = (window.scrollX - bezos.offsetLeft + 175) * 500000;
  71. bezos_counter.innerHTML = (wealth < 185000000000) ? money.format(wealth) : "$185,000,000,000";
  72. }
  73. else {
  74. bezos_counter.innerHTML = '';
  75. }
  76. }
  77. else if (four_hundred_viewable()) {
  78. if (four_hundred_counter_viewable()) {
  79. let wealth = (window.scrollX - four_hundred.offsetLeft + 175) * 500000;
  80. four_hundred_counter.innerHTML = (wealth < 3200000000000) ? money.format(wealth) : "$3,200,000,000,000";
  81. }
  82. else {
  83. four_hundred_counter.innerHTML = '';
  84. }
  85. }
  86. function bezos_viewable() {
  87. return window.scrollX < bezos.offsetLeft + bezos.offsetWidth + 100;
  88. }
  89. function bezos_counter_viewable() {
  90. return bezosCounterStart.offsetLeft - window.scrollX < (window.innerWidth);
  91. }
  92. function four_hundred_viewable() {
  93. return window.scrollX < four_hundred.offsetLeft + four_hundred.offsetWidth + 100;
  94. }
  95. function four_hundred_counter_viewable() {
  96. return four_hundred_counter_start.offsetLeft - window.scrollX < (window.innerWidth);
  97. }
  98. }
  99. function toggleZoom() {
  100. document.getElementById('line-chart').classList.toggle('zoom');
  101. }