pxmoon.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. var lightspeed = 299792
  2. var _1km = 3474.8
  3. var unit = 'km'
  4. var delimeter = ','
  5. var decimalmark = '.'
  6. var unitname = 'km'
  7. var currentRAFID = 0
  8. var isSpeeding = 0
  9. var unitTable = {
  10. km: 1, mi: 0.621371, au: 6.68458712e-9, lightminutes: 0.0000000555941, earths: 0.0000785238, buses: 79.36, bluewhales: 33.3333, greatwalls: 0.00011297137305, pixels: 0.0002877863474156786
  11. }
  12. var msgTimer
  13. var msgNum = [$('#msg1'), $('#msg2'), $('#msg3'), $('#msg4'), $('#msg5')]
  14. var msgIndex = 0
  15. var startX = window.pageXOffset
  16. $(document).ready(function () {
  17. $('html, body').mousewheel(function (e, delta) {
  18. if (Math.abs(e.deltaX)) {
  19. return
  20. } else {
  21. this.scrollLeft -= (e.deltaY * 30);
  22. }
  23. e.preventDefault();
  24. });
  25. }); $(function () {
  26. if (typeof window.performance === 'undefined') {
  27. window.performance = {
  28. };
  29. }
  30. if (!window.performance.now) {
  31. var nowOffset = Date.now(); if (performance.timing && performance.timing.navigationStart) {
  32. nowOffset = performance.timing.navigationStart
  33. }
  34. window.performance.now = function now() {
  35. return Date.now() - nowOffset;
  36. }
  37. }
  38. $('ul.nav a.planetjump').bind('click', function (event) {
  39. cancelLightMsg()
  40. stopSpeeding(); var $anchor = $(this); $('html, body').stop().animate({
  41. scrollLeft: $($anchor.attr('href')).offset().left
  42. }, 5000, 'easeInOutExpo'); event.preventDefault();
  43. })
  44. var essayMarks = []; $('.essay').each(function () {
  45. essayMarks.push($(this).offset().left - 200)
  46. }); var planetMarks = [$('#suntxt').offset().left - 200, $('#merctxt').offset().left - 200, $('#venustxt').offset().left - 200, $('#earthtxt').offset().left - 200, $('#marstxt').offset().left - 200, $('#jupitertxt').offset().left - 200, $('#saturntxt').offset().left - 200, $('#neptunetxt').offset().left - 200, $('#uranustxt').offset().left - 200, $('#plutotxt').offset().left - 200,]; var destinations = $.makeArray(essayMarks).concat($.makeArray(planetMarks)); destinations.sort(function (a, b) {
  47. return a - b
  48. }); var destinationNext = destinations[0]; $('ul.nav a.nextjump').bind('click', function (event) {
  49. cancelLightMsg()
  50. stopSpeeding(); var currentDist = (window.pageXOffset); $.each(destinations, function (index, value) {
  51. if (currentDist >= value - 100) {
  52. destinationNext = destinations[index + 1];
  53. }
  54. else {
  55. return false
  56. }
  57. }); $('html, body').stop().animate({
  58. scrollLeft: destinationNext
  59. }, 4500, 'easeInOutQuad'); event.preventDefault();
  60. })
  61. $('ul.nav a.prevjump').bind('click', function (event) {
  62. cancelLightMsg()
  63. stopSpeeding(); var currentDist = (window.pageXOffset); $.each(destinations, function (index, value) {
  64. if (currentDist <= value + 100) {
  65. destinationNext = destinations[index - 1]; return false
  66. }
  67. }); $('html, body').stop().animate({
  68. scrollLeft: destinationNext
  69. }, 4500, 'easeInOutQuad'); event.preventDefault();
  70. })
  71. $('#distance-counter').on('click', function (e) {
  72. var $units = $('#unitselect')
  73. $units.css('display', $units.css('display') == 'none' ? 'block' : 'none')
  74. })
  75. $('#unitselect li').on('click', function (e) {
  76. unit = $(e.target).attr('id')
  77. unitname = $(e.target).text()
  78. updateDistance()
  79. $('#unitselect').css('display', 'none')
  80. return false
  81. })
  82. $('#lightspeeder a').on('click', function (e) {
  83. stopSpeeding()
  84. if (isSpeeding == 1) {
  85. cancelLightMsg()
  86. stopSpeeding()
  87. isSpeeding = 0
  88. return false
  89. }
  90. else {
  91. isSpeeding = 1
  92. fadeInLightMsg()
  93. changeUnitToLight()
  94. $('#lightspeeder a').css('opacity', 1.0)
  95. $('#lightspeedmsg').css('display', 'block')
  96. currentRAFID = startSpeedingAt()
  97. return false
  98. }
  99. })
  100. }); function startSpeedingAt() {
  101. stopSpeeding()
  102. var startX = window.pageXOffset
  103. var lastTime = window.performance.now()
  104. var onEnterFrame = function (now) {
  105. var timeDelta = now - lastTime
  106. var distance = (lightspeed * timeDelta) / (_1km * 1000)
  107. $('html, body').scrollLeft(startX + distance)
  108. currentRAFID = requestAnimationFrame(onEnterFrame)
  109. }
  110. return requestAnimationFrame(onEnterFrame)
  111. }
  112. function changeUnitToLight() {
  113. unit = 'lightminutes'
  114. unitname = $('#lightminutes').text()
  115. updateDistance()
  116. }; function fadeInLightMsg() {
  117. msgNum[msgIndex].fadeIn(500)
  118. msgTimer = window.setTimeout(fadeOutLightMsg, 4500)
  119. }; function fadeOutLightMsg() {
  120. msgNum[msgIndex].fadeOut(500)
  121. if (msgIndex < msgNum.length - 1) {
  122. msgIndex = msgIndex + 1
  123. fadeInLightMsg()
  124. } else {
  125. return
  126. }
  127. }; function cancelLightMsg() {
  128. msgNum[msgIndex].fadeOut(500)
  129. msgIndex = 0
  130. isSpeeding = 0
  131. window.clearTimeout(msgTimer)
  132. $('#lightspeedmsg').css('display', 'none')
  133. $('#lightspeeder a').css('opacity', 0.7)
  134. }; function updateDistance() {
  135. var px = (window.pageXOffset - $('#bigspace').position().left + $(window).width() / 2); var km = px * _1km; var distance = km * unitTable[unit]; $('#counter').text(Math.max(0, distance.toFixed(1)).toString().replace(".", decimalmark).replace(/\B(?=(\d{3})+(?!\d))/g, delimeter) + ' ' + $('#' + unit).text());
  136. }
  137. function stopSpeeding() {
  138. cancelAnimationFrame(currentRAFID);
  139. }
  140. $('#monitors').text(Math.floor($('#bigspace').width() / screen.availWidth / window.devicePixelRatio)); $(window).scroll(updateDistance);