| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311 |
- <html>
- <style>
- body, html, canvas {
- padding: 0;
- margin: 0;
- background: black;
- height: 100%;
- width: 100%;
- cursor: none;
- }
- </style>
- <body>
- <canvas></canvas>
- <script>
- function drawEllipseByCenter(ctx, cx, cy, w, h) {
- drawEllipse(ctx, cx - w/2.0, cy - h/2.0, w, h);
- }
- function drawEllipse(ctx, x, y, w, h, color) {
- var kappa = .5522848,
- ox = (w / 2) * kappa, // control point offset horizontal
- oy = (h / 2) * kappa, // control point offset vertical
- xe = x + w, // x-end
- ye = y + h, // y-end
- xm = x + w / 2, // x-middle
- ym = y + h / 2; // y-middle
- ctx.beginPath();
- ctx.moveTo(x, ym);
- ctx.bezierCurveTo(x, ym - oy, xm - ox, y, xm, y);
- ctx.bezierCurveTo(xm + ox, y, xe, ym - oy, xe, ym);
- ctx.bezierCurveTo(xe, ym + oy, xm + ox, ye, xm, ye);
- ctx.bezierCurveTo(xm - ox, ye, x, ym + oy, x, ym);
- //ctx.closePath(); // not used correctly, see comments (use to close off open path)
- ctx.fillStyle = color;
- ctx.fill();
- }
- var canvas = document.getElementsByTagName('canvas')[0];
- var ctx = canvas.getContext('2d');
- var mode = ''; //'clip_edit';
- var mouse = {
- pressed: false,
- pressedX: 0,
- pressedY: 0,
- diffX: 0,
- diffY: 0
- };
- var colors = [
- 'blue',
- '#ff0033',
- '#ff0066',
- '#49fb35',
- 'yellow',
- '#1e90ff',
- '#ff00ff',
- '#66ff00',
- '#ffff66',
- '#b4ff00',
- '#ffff33',
- 'white',
- 'black'
- ];
- function randomInt(min, max) {
- min = Math.ceil(min);
- max = Math.floor(max);
- return Math.floor(Math.random() * (max - min + 1)) + min;
- }
- function randomFloat(min, max) {
- return (Math.random() * (max - min + 1)) + min;
- }
- function randomColor() {
- return colors[Math.floor(Math.random() * colors.length)];
- }
- var backgroundFillStyle = 'blue';
- var features = [{
- x: 0,
- y: 0,
- r: 0,
- fillStyle: 'purple',
- speed: 4,
- type: 'circle'
- }, {
- x: 0,
- y: 0,
- w: 4000,
- h: 100,
- fillStyle: 'blue',
- speed: 1,
- horizontal: true,
- type: 'line'
- }, {
- x: 0,
- y: 100,
- w: 4000,
- h: 20,
- fillStyle: 'red',
- speed: 2,
- horizontal: false,
- type: 'line'
- }];
- var clipRegion = {
- x: window.innerWidth/2-200,
- y: window.innerHeight/2-200,
- w: 400,
- h: 400
- }
- function setupClipping(ctx, x, y, w, h) {
- var kappa = .5522848,
- ox = (w / 2) * kappa, // control point offset horizontal
- oy = (h / 2) * kappa, // control point offset vertical
- xe = x + w, // x-end
- ye = y + h, // y-end
- xm = x + w / 2, // x-middle
- ym = y + h / 2; // y-middle
- ctx.beginPath();
- ctx.moveTo(x, ym);
- ctx.bezierCurveTo(x, ym - oy, xm - ox, y, xm, y);
- ctx.bezierCurveTo(xm + ox, y, xe, ym - oy, xe, ym);
- ctx.bezierCurveTo(xe, ym + oy, xm + ox, ye, xm, ye);
- ctx.bezierCurveTo(xm - ox, ye, x, ym + oy, x, ym);
- ctx.clip();
- }
- function resetFeature(f) {
- if (f.type === 'line') {
- f.fillStyle = randomColor();
- f.speed = randomFloat(0.1, 1);
- f.h = randomInt(10, 50);
- f.y = -f.h;
- } else if (f.type === 'circle') {
- if (f.speed < 0) {
- f.r = 0;
- f.fillStyle = randomColor();
- f.speed = randomFloat(0.1, 1);
- } else {
- f.r -= f.speed;
- f.speed = -randomFloat(0.1, 1);
- }
- f.y = clipRegion.y + clipRegion.h/2;
- f.x = clipRegion.x + clipRegion.w/2;
- }
- }
- // initial reset
- features.forEach(function (f) {
- resetFeature(f);
- });
- // load last clip area
- if (localStorage.clipRegion) {
- try {
- clipRegion = JSON.parse(localStorage.clipRegion);
- } catch (e) {}
- }
- function animateContent() {
- // change background maybe
- // if (randomInt(0, 10000) < 10) backgroundFillStyle = randomColor();
- backgroundFillStyle = 'black';
- features.forEach(function (f) {
- if (f.type === 'line') {
- if (f.y > canvas.height) {
- resetFeature(f);
- } else {
- f.y += f.speed;
- }
- } else if (f.type === 'circle') {
- if (f.r < 0 || f.r > clipRegion.w * 2) {
- resetFeature(f);
- } else {
- f.r += f.speed;
- }
- }
- });
- }
- function drawContent() {
- // main color
- ctx.fillStyle = backgroundFillStyle;
- ctx.fillRect(0, 0, canvas.width, canvas.height);
- // feature
- features.forEach(function (f) {
- ctx.fillStyle = f.fillStyle;
- if (f.type === 'line') {
- if (f.horizontal) {
- ctx.fillRect(f.x, f.y, f.w, f.h);
- } else {
- ctx.fillRect(f.y, f.x, f.h, f.w);
- }
- } else if (f.type === 'circle') {
- drawEllipseByCenter(ctx, f.x, f.y, f.r, f.r);
- }
- });
- }
- function redraw() {
- ctx.clearRect(0, 0, canvas.width, canvas.height);
- ctx.save();
- if (mode === 'clip_edit') {
- drawEllipse(ctx, 10, 10, 20, 20, 'red');
- drawEllipse(ctx, clipRegion.x, clipRegion.y, clipRegion.w, clipRegion.h, '#ff0000aa');
- } else {
- setupClipping(ctx, clipRegion.x, clipRegion.y, clipRegion.w, clipRegion.h);
- animateContent();
- drawContent();
- }
- ctx.restore();
- requestAnimationFrame(redraw);
- }
- function toggleFullScreen() {
- if (window.fullScreen) {
- if (document.exitFullscreen) {
- document.exitFullscreen();
- } else if (document.mozCancelFullScreen) { /* Firefox */
- document.mozCancelFullScreen();
- } else if (document.webkitExitFullscreen) { /* Chrome, Safari and Opera */
- document.webkitExitFullscreen();
- } else if (document.msExitFullscreen) { /* IE/Edge */
- document.msExitFullscreen();
- }
- } else {
- if (document.documentElement.requestFullscreen) {
- document.documentElement.requestFullscreen();
- } else if (document.documentElement.mozRequestFullScreen) { /* Firefox */
- document.documentElement.mozRequestFullScreen();
- } else if (document.documentElement.webkitRequestFullscreen) { /* Chrome, Safari and Opera */
- document.documentElement.webkitRequestFullscreen();
- } else if (document.documentElement.msRequestFullscreen) { /* IE/Edge */
- document.documentElement.msRequestFullscreen();
- }
- }
- }
- function onResize() {
- ctx.canvas.width = window.innerWidth;
- ctx.canvas.height = window.innerHeight;
- }
- function onMouseDown(e) {
- mouse.pressed = true;
- mouse.pressedX = e.x;
- mouse.pressedY = e.y;
- }
- function onMouseUp(e) {
- mouse.pressed = false;
- mouse.pressedX = 0;
- mouse.pressedY = 0;
- }
- function onMouseMove(e) {
- if (!mouse.pressed || mode !== 'clip_edit') return;
- if (e.ctrlKey) {
- clipRegion.w += e.movementX;
- clipRegion.h += e.movementY;
- } else {
- clipRegion.x += e.movementX;
- clipRegion.y += e.movementY;
- }
- localStorage.clipRegion = JSON.stringify(clipRegion);
- }
- function onKeypress(e) {
- if (e.key === 'Escape') {
- mode = mode === 'clip_edit' ? '' : 'clip_edit';
- } else if (e.key === 'f') {
- toggleFullScreen();
- }
- }
- window.addEventListener('keypress', onKeypress);
- window.addEventListener('resize', onResize);
- canvas.addEventListener('mousedown', onMouseDown);
- canvas.addEventListener('mouseup', onMouseUp);
- canvas.addEventListener('mousemove', onMouseMove);
- onResize();
- requestAnimationFrame(redraw);
- </script>
- </body>
- </html>
|