index.html 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <html>
  2. <style>
  3. body, html, canvas {
  4. padding: 0;
  5. margin: 0;
  6. background: black;
  7. height: 100%;
  8. width: 100%;
  9. cursor: none;
  10. }
  11. </style>
  12. <body>
  13. <canvas></canvas>
  14. <script>
  15. function drawEllipseByCenter(ctx, cx, cy, w, h) {
  16. drawEllipse(ctx, cx - w/2.0, cy - h/2.0, w, h);
  17. }
  18. function drawEllipse(ctx, x, y, w, h, color) {
  19. var kappa = .5522848,
  20. ox = (w / 2) * kappa, // control point offset horizontal
  21. oy = (h / 2) * kappa, // control point offset vertical
  22. xe = x + w, // x-end
  23. ye = y + h, // y-end
  24. xm = x + w / 2, // x-middle
  25. ym = y + h / 2; // y-middle
  26. ctx.beginPath();
  27. ctx.moveTo(x, ym);
  28. ctx.bezierCurveTo(x, ym - oy, xm - ox, y, xm, y);
  29. ctx.bezierCurveTo(xm + ox, y, xe, ym - oy, xe, ym);
  30. ctx.bezierCurveTo(xe, ym + oy, xm + ox, ye, xm, ye);
  31. ctx.bezierCurveTo(xm - ox, ye, x, ym + oy, x, ym);
  32. //ctx.closePath(); // not used correctly, see comments (use to close off open path)
  33. ctx.fillStyle = color;
  34. ctx.fill();
  35. }
  36. var canvas = document.getElementsByTagName('canvas')[0];
  37. var ctx = canvas.getContext('2d');
  38. var mode = ''; //'clip_edit';
  39. var mouse = {
  40. pressed: false,
  41. pressedX: 0,
  42. pressedY: 0,
  43. diffX: 0,
  44. diffY: 0
  45. };
  46. var colors = [
  47. 'blue',
  48. '#ff0033',
  49. '#ff0066',
  50. '#49fb35',
  51. 'yellow',
  52. '#1e90ff',
  53. '#ff00ff',
  54. '#66ff00',
  55. '#ffff66',
  56. '#b4ff00',
  57. '#ffff33',
  58. 'white',
  59. 'black'
  60. ];
  61. function randomInt(min, max) {
  62. min = Math.ceil(min);
  63. max = Math.floor(max);
  64. return Math.floor(Math.random() * (max - min + 1)) + min;
  65. }
  66. function randomFloat(min, max) {
  67. return (Math.random() * (max - min + 1)) + min;
  68. }
  69. function randomColor() {
  70. return colors[Math.floor(Math.random() * colors.length)];
  71. }
  72. var backgroundFillStyle = 'blue';
  73. var features = [{
  74. x: 0,
  75. y: 0,
  76. r: 0,
  77. fillStyle: 'purple',
  78. speed: 4,
  79. type: 'circle'
  80. }, {
  81. x: 0,
  82. y: 0,
  83. w: 4000,
  84. h: 100,
  85. fillStyle: 'blue',
  86. speed: 1,
  87. horizontal: true,
  88. type: 'line'
  89. }, {
  90. x: 0,
  91. y: 100,
  92. w: 4000,
  93. h: 20,
  94. fillStyle: 'red',
  95. speed: 2,
  96. horizontal: false,
  97. type: 'line'
  98. }];
  99. var clipRegion = {
  100. x: window.innerWidth/2-200,
  101. y: window.innerHeight/2-200,
  102. w: 400,
  103. h: 400
  104. }
  105. function setupClipping(ctx, x, y, w, h) {
  106. var kappa = .5522848,
  107. ox = (w / 2) * kappa, // control point offset horizontal
  108. oy = (h / 2) * kappa, // control point offset vertical
  109. xe = x + w, // x-end
  110. ye = y + h, // y-end
  111. xm = x + w / 2, // x-middle
  112. ym = y + h / 2; // y-middle
  113. ctx.beginPath();
  114. ctx.moveTo(x, ym);
  115. ctx.bezierCurveTo(x, ym - oy, xm - ox, y, xm, y);
  116. ctx.bezierCurveTo(xm + ox, y, xe, ym - oy, xe, ym);
  117. ctx.bezierCurveTo(xe, ym + oy, xm + ox, ye, xm, ye);
  118. ctx.bezierCurveTo(xm - ox, ye, x, ym + oy, x, ym);
  119. ctx.clip();
  120. }
  121. function resetFeature(f) {
  122. if (f.type === 'line') {
  123. f.fillStyle = randomColor();
  124. f.speed = randomFloat(0.1, 1);
  125. f.h = randomInt(10, 50);
  126. f.y = -f.h;
  127. } else if (f.type === 'circle') {
  128. if (f.speed < 0) {
  129. f.r = 0;
  130. f.fillStyle = randomColor();
  131. f.speed = randomFloat(0.1, 1);
  132. } else {
  133. f.r -= f.speed;
  134. f.speed = -randomFloat(0.1, 1);
  135. }
  136. f.y = clipRegion.y + clipRegion.h/2;
  137. f.x = clipRegion.x + clipRegion.w/2;
  138. }
  139. }
  140. // initial reset
  141. features.forEach(function (f) {
  142. resetFeature(f);
  143. });
  144. // load last clip area
  145. if (localStorage.clipRegion) {
  146. try {
  147. clipRegion = JSON.parse(localStorage.clipRegion);
  148. } catch (e) {}
  149. }
  150. function animateContent() {
  151. // change background maybe
  152. // if (randomInt(0, 10000) < 10) backgroundFillStyle = randomColor();
  153. backgroundFillStyle = 'black';
  154. features.forEach(function (f) {
  155. if (f.type === 'line') {
  156. if (f.y > canvas.height) {
  157. resetFeature(f);
  158. } else {
  159. f.y += f.speed;
  160. }
  161. } else if (f.type === 'circle') {
  162. if (f.r < 0 || f.r > clipRegion.w * 2) {
  163. resetFeature(f);
  164. } else {
  165. f.r += f.speed;
  166. }
  167. }
  168. });
  169. }
  170. function drawContent() {
  171. // main color
  172. ctx.fillStyle = backgroundFillStyle;
  173. ctx.fillRect(0, 0, canvas.width, canvas.height);
  174. // feature
  175. features.forEach(function (f) {
  176. ctx.fillStyle = f.fillStyle;
  177. if (f.type === 'line') {
  178. if (f.horizontal) {
  179. ctx.fillRect(f.x, f.y, f.w, f.h);
  180. } else {
  181. ctx.fillRect(f.y, f.x, f.h, f.w);
  182. }
  183. } else if (f.type === 'circle') {
  184. drawEllipseByCenter(ctx, f.x, f.y, f.r, f.r);
  185. }
  186. });
  187. }
  188. function redraw() {
  189. ctx.clearRect(0, 0, canvas.width, canvas.height);
  190. ctx.save();
  191. if (mode === 'clip_edit') {
  192. drawEllipse(ctx, 10, 10, 20, 20, 'red');
  193. drawEllipse(ctx, clipRegion.x, clipRegion.y, clipRegion.w, clipRegion.h, '#ff0000aa');
  194. } else {
  195. setupClipping(ctx, clipRegion.x, clipRegion.y, clipRegion.w, clipRegion.h);
  196. animateContent();
  197. drawContent();
  198. }
  199. ctx.restore();
  200. requestAnimationFrame(redraw);
  201. }
  202. function toggleFullScreen() {
  203. if (window.fullScreen) {
  204. if (document.exitFullscreen) {
  205. document.exitFullscreen();
  206. } else if (document.mozCancelFullScreen) { /* Firefox */
  207. document.mozCancelFullScreen();
  208. } else if (document.webkitExitFullscreen) { /* Chrome, Safari and Opera */
  209. document.webkitExitFullscreen();
  210. } else if (document.msExitFullscreen) { /* IE/Edge */
  211. document.msExitFullscreen();
  212. }
  213. } else {
  214. if (document.documentElement.requestFullscreen) {
  215. document.documentElement.requestFullscreen();
  216. } else if (document.documentElement.mozRequestFullScreen) { /* Firefox */
  217. document.documentElement.mozRequestFullScreen();
  218. } else if (document.documentElement.webkitRequestFullscreen) { /* Chrome, Safari and Opera */
  219. document.documentElement.webkitRequestFullscreen();
  220. } else if (document.documentElement.msRequestFullscreen) { /* IE/Edge */
  221. document.documentElement.msRequestFullscreen();
  222. }
  223. }
  224. }
  225. function onResize() {
  226. ctx.canvas.width = window.innerWidth;
  227. ctx.canvas.height = window.innerHeight;
  228. }
  229. function onMouseDown(e) {
  230. mouse.pressed = true;
  231. mouse.pressedX = e.x;
  232. mouse.pressedY = e.y;
  233. }
  234. function onMouseUp(e) {
  235. mouse.pressed = false;
  236. mouse.pressedX = 0;
  237. mouse.pressedY = 0;
  238. }
  239. function onMouseMove(e) {
  240. if (!mouse.pressed || mode !== 'clip_edit') return;
  241. if (e.ctrlKey) {
  242. clipRegion.w += e.movementX;
  243. clipRegion.h += e.movementY;
  244. } else {
  245. clipRegion.x += e.movementX;
  246. clipRegion.y += e.movementY;
  247. }
  248. localStorage.clipRegion = JSON.stringify(clipRegion);
  249. }
  250. function onKeypress(e) {
  251. if (e.key === 'Escape') {
  252. mode = mode === 'clip_edit' ? '' : 'clip_edit';
  253. } else if (e.key === 'f') {
  254. toggleFullScreen();
  255. }
  256. }
  257. window.addEventListener('keypress', onKeypress);
  258. window.addEventListener('resize', onResize);
  259. canvas.addEventListener('mousedown', onMouseDown);
  260. canvas.addEventListener('mouseup', onMouseUp);
  261. canvas.addEventListener('mousemove', onMouseMove);
  262. onResize();
  263. requestAnimationFrame(redraw);
  264. </script>
  265. </body>
  266. </html>