cursor.html 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Playback &gt; Cursor Experiment</title>
  5. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
  6. <style>
  7. html, body {
  8. padding: 0; margin: 0;
  9. }
  10. div.big {
  11. font-size: 40em;
  12. text-align: center;
  13. }
  14. </style>
  15. <script>(function(){
  16. var nlines = 5;
  17. var current = 0;
  18. var currentWidth = 0;
  19. var currentX = 0;
  20. var lines = [];
  21. var dx = 20;
  22. var cursor = null;
  23. var currentHeight = 0;
  24. var interval = null;
  25. var heightSum = 0;
  26. function createGradient(width, height, color) {
  27. var c = document.createElement("canvas");
  28. c.width = width; c.height = height;
  29. var ctx = c.getContext("2d");
  30. ctx.globalAlpha = 0.5
  31. var gradient = ctx.createLinearGradient(0, 0, width, 0);
  32. gradient.addColorStop(0, "transparent");
  33. gradient.addColorStop(0.5, color);
  34. gradient.addColorStop(1, "transparent");
  35. ctx.fillStyle = gradient;
  36. ctx.fillRect(0, 0, width, height);
  37. /*
  38. // Add top/bottom lines
  39. ctx.beginPath();
  40. ctx.moveTo(0,1);
  41. ctx.lineTo(width, 1);
  42. ctx.stroke();
  43. */
  44. var img = new Image();
  45. img.src = c.toDataURL('image/png');
  46. img.width = width; img.height = height;
  47. document.body.appendChild(img);
  48. return img;
  49. }
  50. window.onload = function() {
  51. lines = [];
  52. // fix div colors
  53. for (var i = 0; i < nlines; i++) {
  54. var div = document.createElement("div");
  55. div.className = "big";
  56. lines.push(div); document.body.appendChild(div);
  57. div.style.background = i % 2 ? "black" : "white";
  58. div.style.color = i % 2 ? "white" : "black";
  59. div.textContent = i + 1;
  60. }
  61. // Initialize variabels
  62. heightSum = 0;
  63. currentX = 0; currentWidth = -1; current = -1;
  64. currentHeight = 0;
  65. // start animation
  66. interval = window.setInterval(step, 100);
  67. }
  68. function step() {
  69. //console.log("step");
  70. currentX += dx;
  71. if (currentX > currentWidth) {
  72. current += 1;
  73. heightSum += currentHeight;
  74. currentX = 0;
  75. if (current == lines.length) {
  76. window.clearInterval(interval);
  77. return false;
  78. }
  79. window.setTimeout(function(){scrollToLine(current);}, 0);
  80. currentWidth = lines[current].offsetWidth - 20;
  81. currentHeight = lines[current].offsetHeight;
  82. // Hide cursor
  83. if (cursor)
  84. cursor.parentElement.removeChild(cursor);
  85. // Re-generate cursor
  86. cursor = createGradient(20, currentHeight, current % 2 ? "white" : "black");
  87. cursor.style.position = "absolute";
  88. // Display cursor
  89. document.body.appendChild(cursor);
  90. }
  91. cursor.style.top = heightSum + "px";
  92. cursor.style.left = currentX + "px";
  93. }
  94. function scrollToLine(line) {
  95. var height = lines[line].offsetHeight;
  96. var windowHeight = window.innerHeight;
  97. var top = Math.max(0, (windowHeight - height) >> 1);
  98. //window.scrollTo(0, heightSum + top);
  99. $('html, body').animate({
  100. scrollTop: heightSum - top
  101. }, 500);
  102. }
  103. }());</script>
  104. </head>
  105. </html>