index.html 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <html><head>
  2. <meta http-equiv="content-type" content="text/html; charset=UTF-8"><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. </head><body>
  13. <canvas height="627" width="20"></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. '#ff5400',
  58. '#ffff33',
  59. 'white',
  60. 'black'
  61. ];
  62. function randomInt(min, max) {
  63. min = Math.ceil(min);
  64. max = Math.floor(max);
  65. return Math.floor(Math.random() * (max - min + 1)) + min;
  66. }
  67. function randomFloat(min, max) {
  68. return (Math.random() * (max - min + 1)) + min;
  69. }
  70. function randomColor(blacklist) {
  71. if (!blacklist) blacklist = [];
  72. var color;
  73. do {
  74. color = colors[Math.floor(Math.random() * colors.length)]
  75. }
  76. while (blacklist.indexOf(color) > -1);
  77. return color;
  78. }
  79. var backgroundFillStyle = 'blue';
  80. var features = [{
  81. x: 0,
  82. y: 0,
  83. r: 0,
  84. fillStyle: 'purple',
  85. speed: 4,
  86. type: 'circle'
  87. }, {
  88. x: 0,
  89. y: 0,
  90. w: 4000,
  91. h: 100,
  92. fillStyle: 'blue',
  93. speed: 1,
  94. horizontal: true,
  95. type: 'line'
  96. }, {
  97. x: 0,
  98. y: 100,
  99. w: 4000,
  100. h: 20,
  101. fillStyle: 'red',
  102. speed: 2,
  103. horizontal: false,
  104. type: 'line'
  105. }];
  106. var clipRegion = {
  107. x: window.innerWidth/2-200,
  108. y: window.innerHeight/2-200,
  109. w: 400,
  110. h: 400
  111. }
  112. function setupClipping(ctx, x, y, w, h) {
  113. var kappa = .5522848,
  114. ox = (w / 2) * kappa, // control point offset horizontal
  115. oy = (h / 2) * kappa, // control point offset vertical
  116. xe = x + w, // x-end
  117. ye = y + h, // y-end
  118. xm = x + w / 2, // x-middle
  119. ym = y + h / 2; // y-middle
  120. ctx.beginPath();
  121. ctx.moveTo(x, ym);
  122. ctx.bezierCurveTo(x, ym - oy, xm - ox, y, xm, y);
  123. ctx.bezierCurveTo(xm + ox, y, xe, ym - oy, xe, ym);
  124. ctx.bezierCurveTo(xe, ym + oy, xm + ox, ye, xm, ye);
  125. ctx.bezierCurveTo(xm - ox, ye, x, ym + oy, x, ym);
  126. ctx.clip();
  127. }
  128. function resetFeature(f) {
  129. if (f.type === 'line') {
  130. f.fillStyle = randomColor();
  131. f.speed = randomFloat(0.5, 2);
  132. f.h = randomInt(20, 80);
  133. f.y = -f.h;
  134. } else if (f.type === 'circle') {
  135. if (f.speed < 0) {
  136. f.r = 0;
  137. f.fillStyle = randomColor(['black']);
  138. f.speed = randomFloat(0.8, 1.5);
  139. } else {
  140. f.r -= f.speed;
  141. f.speed = -randomFloat(0.8, 1.5);
  142. }
  143. f.y = clipRegion.y + clipRegion.h/2;
  144. f.x = clipRegion.x + clipRegion.w/2;
  145. }
  146. }
  147. // initial reset
  148. features.forEach(function (f) {
  149. resetFeature(f);
  150. });
  151. // load last clip area
  152. if (localStorage.clipRegion) {
  153. try {
  154. clipRegion = JSON.parse(localStorage.clipRegion);
  155. } catch (e) {}
  156. }
  157. function animateContent() {
  158. // change background maybe
  159. // if (randomInt(0, 1000) < 5) backgroundFillStyle = randomColor();
  160. backgroundFillStyle = 'black';
  161. features.forEach(function (f) {
  162. if (f.type === 'line') {
  163. if (f.y > canvas.width) {
  164. resetFeature(f);
  165. } else {
  166. f.y += f.speed;
  167. }
  168. } else if (f.type === 'circle') {
  169. if (f.r < 0 || f.r > clipRegion.w * 2) {
  170. resetFeature(f);
  171. } else {
  172. f.r += f.speed;
  173. }
  174. }
  175. });
  176. }
  177. function drawContent() {
  178. // main color
  179. ctx.fillStyle = backgroundFillStyle;
  180. ctx.fillRect(0, 0, canvas.width, canvas.height);
  181. // feature
  182. features.forEach(function (f) {
  183. ctx.fillStyle = f.fillStyle;
  184. if (f.type === 'line') {
  185. if (f.horizontal) {
  186. ctx.fillRect(f.x, f.y, f.w, f.h);
  187. } else {
  188. ctx.fillRect(f.y, f.x, f.h, f.w);
  189. }
  190. } else if (f.type === 'circle') {
  191. drawEllipseByCenter(ctx, f.x, f.y, f.r, f.r);
  192. }
  193. });
  194. }
  195. function redraw() {
  196. ctx.clearRect(0, 0, canvas.width, canvas.height);
  197. ctx.save();
  198. if (mode !== 'clip_edit') {
  199. setupClipping(ctx, clipRegion.x, clipRegion.y, clipRegion.w, clipRegion.h);
  200. }
  201. animateContent();
  202. drawContent();
  203. if (mode === 'clip_edit') {
  204. drawEllipse(ctx, 10, 10, 20, 20, 'red');
  205. drawEllipse(ctx, clipRegion.x, clipRegion.y, clipRegion.w, clipRegion.h, '#ff0000aa');
  206. }
  207. ctx.restore();
  208. requestAnimationFrame(redraw);
  209. }
  210. function toggleFullScreen() {
  211. if (window.fullScreen) {
  212. if (document.exitFullscreen) {
  213. document.exitFullscreen();
  214. } else if (document.mozCancelFullScreen) { /* Firefox */
  215. document.mozCancelFullScreen();
  216. } else if (document.webkitExitFullscreen) { /* Chrome, Safari and Opera */
  217. document.webkitExitFullscreen();
  218. } else if (document.msExitFullscreen) { /* IE/Edge */
  219. document.msExitFullscreen();
  220. }
  221. } else {
  222. if (document.documentElement.requestFullscreen) {
  223. document.documentElement.requestFullscreen();
  224. } else if (document.documentElement.mozRequestFullScreen) { /* Firefox */
  225. document.documentElement.mozRequestFullScreen();
  226. } else if (document.documentElement.webkitRequestFullscreen) { /* Chrome, Safari and Opera */
  227. document.documentElement.webkitRequestFullscreen();
  228. } else if (document.documentElement.msRequestFullscreen) { /* IE/Edge */
  229. document.documentElement.msRequestFullscreen();
  230. }
  231. }
  232. }
  233. function onResize() {
  234. ctx.canvas.width = window.innerWidth;
  235. ctx.canvas.height = window.innerHeight;
  236. }
  237. function onMouseDown(e) {
  238. mouse.pressed = true;
  239. mouse.pressedX = e.x;
  240. mouse.pressedY = e.y;
  241. }
  242. function onMouseUp(e) {
  243. mouse.pressed = false;
  244. mouse.pressedX = 0;
  245. mouse.pressedY = 0;
  246. }
  247. function onMouseMove(e) {
  248. if (!mouse.pressed || mode !== 'clip_edit') return;
  249. if (e.ctrlKey) {
  250. clipRegion.w += e.movementX;
  251. clipRegion.h += e.movementY;
  252. } else {
  253. clipRegion.x += e.movementX;
  254. clipRegion.y += e.movementY;
  255. }
  256. localStorage.clipRegion = JSON.stringify(clipRegion);
  257. }
  258. function onKeypress(e) {
  259. if (e.key === 'e') {
  260. mode = mode === 'clip_edit' ? '' : 'clip_edit';
  261. } else if (e.key === 'f') {
  262. toggleFullScreen();
  263. }
  264. }
  265. window.addEventListener('keypress', onKeypress);
  266. window.addEventListener('resize', onResize);
  267. canvas.addEventListener('mousedown', onMouseDown);
  268. canvas.addEventListener('mouseup', onMouseUp);
  269. canvas.addEventListener('mousemove', onMouseMove);
  270. onResize();
  271. requestAnimationFrame(redraw);
  272. </script>
  273. </body></html>