cursor.html 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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: 10em;
  12. text-align: center;
  13. background: url('cat.png');
  14. text-shadow: 1px 1px 1px black;
  15. color: white;
  16. }
  17. div.rep1 {
  18. position: fixed;
  19. top: 0;
  20. left: 0; right: 0;
  21. overflow: hidden;
  22. box-shadow: 0 0 20px black;
  23. background-color: white;
  24. }
  25. </style>
  26. <script>(function(){
  27. // The layers
  28. var rep1, main;
  29. var lines = [];
  30. var nlines = 3 + Math.floor(Math.random() * 4);
  31. var current = 0; // current line
  32. var currentWidth = 0; // the width of current line
  33. var currentHeight = 0;
  34. var heightSum = 0;
  35. var currentX = 0; // x of the cursor
  36. var dx = 20;
  37. var cursor;
  38. var interval;
  39. function Cursor() {
  40. var cursor1 = document.createElement("img");
  41. var cursor2 = document.createElement("img");
  42. // Public
  43. this.cursor1 = cursor1;
  44. this.cursor2 = cursor2;
  45. // Set position
  46. cursor1.style.position = "absolute";
  47. cursor2.style.position = "fixed";
  48. // Set z-indices
  49. cursor1.style.zIndex = "-1";
  50. cursor2.style.zIndex = "-1";
  51. // Generate a gradient
  52. this.style = function (width, height, color) {
  53. var c = document.createElement("canvas");
  54. c.width = width; c.height = height;
  55. var ctx = c.getContext("2d");
  56. ctx.globalAlpha = 0.5
  57. // Generate the gradient
  58. var gradient = ctx.createLinearGradient(0, 0, width, 0);
  59. gradient.addColorStop(0, "transparent");
  60. gradient.addColorStop(0.5, color);
  61. gradient.addColorStop(1, "transparent");
  62. ctx.fillStyle = gradient;
  63. ctx.fillRect(0, 0, width, height);
  64. // Set the images
  65. cursor1.src = cursor2.src = c.toDataURL('image/png');
  66. cursor1.width = cursor2.width = width ;
  67. cursor1.height = cursor2.height = height;
  68. }
  69. // Set positions
  70. this.set = function (x, y) {
  71. cursor1.style.top = heightSum + "px";
  72. cursor1.style.left = currentX + "px";
  73. cursor2.style.top = heightSum + "px";
  74. cursor2.style.left = currentX + "px";
  75. }
  76. // Display
  77. this.show2 = function (vis) {
  78. if (vis) {
  79. cursor2.style.display = "block";
  80. } else {
  81. cursor2.style.display = "none";
  82. }
  83. }
  84. }
  85. window.onload = function() {
  86. window.resizeTo(650, 650);
  87. cursor = new Cursor();
  88. rep1 = document.createElement("div");
  89. rep1.className = "rep1";
  90. var helper = document.createElement("div");
  91. helper.className = "helper";
  92. rep1.appendChild(cursor.cursor2);
  93. rep1.appendChild(helper);
  94. main = document.createElement("div");
  95. main.className = "main";
  96. document.body.appendChild(cursor.cursor1);
  97. document.body.appendChild(main);
  98. document.body.appendChild(rep1);
  99. //rep1.style.display = "none";
  100. // z-indices
  101. //main.style.zIndex = "1";
  102. //rep1.style.zIndex = "1";
  103. //main.style.background = "green";
  104. //
  105. lines = [];
  106. var els = [main, helper];
  107. // fix div colors
  108. for (var i = 0; i < nlines; i++)
  109. for (var j = 0; j < els.length; j ++) {
  110. var div = document.createElement("div");
  111. div.className = "big";
  112. if (j === 0)
  113. lines.push(div);
  114. els[j].appendChild(div);
  115. div.innerHTML = (i ? "&nbsp;" : ":") + (i + 1) + (i !== nlines - 1 ? "&nbsp;" : ":");
  116. }
  117. // Initialize variabels
  118. heightSum = 0;
  119. currentX = 0; currentWidth = -1; current = -1;
  120. currentHeight = 0;
  121. window.setTimeout(init, 1);
  122. }
  123. function init() {
  124. // Fix height
  125. rep1.style.height = Math.floor(window.innerHeight / 2) + "px";
  126. // start animation
  127. interval = window.setInterval(step, 20);
  128. }
  129. function step () {
  130. currentX += dx;
  131. // We need to go to another staff line
  132. if (currentX > currentWidth) {
  133. heightSum += currentHeight;
  134. currentX = 0;
  135. current += 1;
  136. if (current == lines.length) {
  137. //window.clearInterval(interval);
  138. //return false;
  139. current = 0;
  140. heightSum = 0;
  141. }
  142. if (current === lines.length - 1) {
  143. console.log("Last line: repeat");
  144. showRep1(0);
  145. }
  146. var thenHideSep1 = (current === 0);
  147. window.setTimeout(function(){scrollToLine(current, thenHideSep1);}, 0);
  148. currentWidth = lines[current].offsetWidth - 20;
  149. currentHeight = lines[current].offsetHeight;
  150. // Re-generate cursor
  151. cursor.style(20, currentHeight, "black");
  152. }
  153. cursor.set(heightSum, currentX);
  154. }
  155. function scrollToLine(line, thenHideSep1) {
  156. var height = lines[line].offsetHeight;
  157. var windowHeight = window.innerHeight;
  158. var top = Math.max(0, (windowHeight - height) >> 1);
  159. //window.scrollTo(0, heightSum + top);
  160. if (thenHideSep1) {
  161. // Put the cursor on Sep1
  162. cursor.show2(true);
  163. $('html, body').animate({
  164. scrollTop: heightSum - top
  165. }, 500, function() {
  166. showRep1();
  167. });
  168. } else {
  169. $('html, body').animate({
  170. scrollTop: heightSum - top
  171. }, 500);
  172. }
  173. }
  174. // Show or hide repetition view
  175. function showRep1(line) {
  176. if (typeof line !== "undefined") {
  177. rep1.scrollTop = 0;
  178. //rep1.style.display = "block";
  179. $(rep1).fadeIn("slow");
  180. } else {
  181. // rep1.style.display = "none";
  182. $(rep1).fadeOut("fast");
  183. cursor.show2(false);
  184. }
  185. }
  186. }());
  187. </script>
  188. </head>
  189. </html>