diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 9e774cc..be7b194 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -2,13 +2,13 @@ name: CI on: [push, pull_request] jobs: - node-13-typescript: + node-20-typescript: runs-on: ubuntu-18.04 steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v2 with: - node-version: '13' + node-version: '20' - run: npm install - - run: ./node_modules/.bin/tsc + - run: npm run build - run: git diff --exit-code diff --git a/README.md b/README.md index f53a6fe..f0fa77f 100644 --- a/README.md +++ b/README.md @@ -17,13 +17,39 @@ $ python3 -m http.server 6969 $ iexplore.exe http://localhost:6969/ ``` -## Development Workflow +## Building -1. `$ npm install` -2. `$ ./node_modules/.bin/tsc -w` -3. `` +The whole build is organized so you can just serve the repo via an HTTP server and it just works. This is done to simplify deployment to [GitHub pages](https://pages.github.com/). We just tell GitHub to service this repo as is. The build artifacts are also commited to the repo. So if you want to simply get the website working you don't even have to build anything. Just serve the repo. -Make sure that you commit the generated `js/*` files along with your changes. This is important for the project to retain that "Just deploy the repo" attitude. +The build is done via the [./build.js](./build.js) script. It is recommended to read it to get an idea on how it works. It is also recommended to check the `"scripts"` section of [./package.json](./package.json) to get an idea on how it is called from `npm run`. + +Before doing any building make sure you installed all the necessary dependencies: + +```console +$ npm install +``` + +To build all the artifacts + +```console +$ npm run build +``` + +## Watching + +The [./build.js](./build.js) script enables you to [Watch](https://www.typescriptlang.org/docs/handbook/configuring-watch.html#handbook-content) the source code: + +```console +$ npm run watch +``` + +## Serving and Watching + +```console +$ npm run service +``` + +This starts both `python3 -m http.server 6969` and [Watching](#Watching) at the same time, providing a convenient development environment. # Filter Development diff --git a/build.js b/build.js new file mode 100644 index 0000000..716ff5c --- /dev/null +++ b/build.js @@ -0,0 +1,99 @@ +import { spawn } from 'child_process'; + +function cmd(program, args) { + console.log('CMD:', program, args); + const p = spawn(program, args.flat()); // NOTE: flattening the args array enables you to group related arguments for better self-documentation of the running command + p.stdout.on('data', (data) => process.stdout.write(data)); + p.stderr.on('data', (data) => process.stderr.write(data)); + p.on('close', (code) => { + if (code !== 0) { + console.error(program, args, 'exited with', code); + } + }); + return p; +} + +const commonTscFlags = [ + '--strict', + '--removeComments', + '--skipLibCheck', +]; + +const mainTs = [ + 'ts/eval.ts', + 'ts/filters.ts', + 'ts/grecha.ts', + 'ts/index.ts' +]; + +function tscMain(...extraParams) { + cmd('tsc', [ + ...commonTscFlags, + ['--outDir', 'js'], + ...extraParams, + mainTs, + ]); +} + +function tscServiceWorker(...extraParams) { + cmd('tsc', [ + ...commonTscFlags, + ['--lib', 'webworker'], + ['--outFile', 'serviceworker.js'], + ...extraParams, + 'serviceworker.ts' + ]); +} + +function build(part, ...args) { + switch (part) { + case undefined: + tscServiceWorker(); + tscMain(); + break; + case 'main': + tscMain(); + break; + case 'serviceworker': + tscServiceWorker(); + break; + default: + throw new Error(`Unknown build part ${part}. Available parts: main, serviceworker.`); + } +} + +function watch(part, ...args) { + switch (part) { + case undefined: + tscMain('-w', '--preserveWatchOutput'); + tscServiceWorker('-w', '--preserveWatchOutput'); + break; + case 'main': + tscMain('-w', '--preserveWatchOutput'); + break; + case 'serviceworker': + tscServiceWorker('-w', '--preserveWatchOutput'); + break; + default: + throw new Error(`Unknown watch part ${part}. Available parts: main, serviceworker.`); + } +} + +const [nodePath, scriptPath, command, ...args] = process.argv; +switch (command) { +case undefined: +case 'build': + build(...args); + break; +case 'watch': + watch(...args); + break; +case 'serve': + // TODO: maybe replace Python with something from Node itself? + // Python is a pretty unreasonable dependency. + cmd('python3', [['-m', 'http.server'], '6969']); + watch(); + break; +default: + throw new Error(`Unknown command ${command}. Available commands: build, watch.`); +} diff --git a/index.html b/index.html index a35234e..3fab8c7 100644 --- a/index.html +++ b/index.html @@ -1,3 +1,4 @@ + emoteJAM — Generate animated emotes from static images diff --git a/js/eval.js b/js/eval.js index 9a9883d..44c0700 100644 --- a/js/eval.js +++ b/js/eval.js @@ -28,7 +28,7 @@ var BINARY_OPS = { } }; var UNARY_OPS = { - '-': function (arg) { return -arg; }, + '-': function (arg) { return -arg; } }; var Lexer = (function () { function Lexer(src) { @@ -73,8 +73,8 @@ function parse_primary(lexer) { "kind": "unary_op", "payload": { "op": token, - "operand": operand, - }, + "operand": operand + } }; } else if (token === '(') { @@ -98,7 +98,7 @@ function parse_primary(lexer) { "kind": "funcall", "payload": { "name": token, - "args": args, + "args": args } }; } @@ -119,7 +119,7 @@ function parse_primary(lexer) { "kind": "funcall", "payload": { "name": token, - "args": args, + "args": args } }; } @@ -155,7 +155,7 @@ function parse_expr(lexer, prec) { "payload": { "op": op_token, "lhs": lhs, - "rhs": rhs, + "rhs": rhs } }; } diff --git a/js/filters.js b/js/filters.js index a60a6e0..d634ca5 100644 --- a/js/filters.js +++ b/js/filters.js @@ -10,7 +10,7 @@ var filters = { "init": 0.85, "min": 0.01, "max": 2.00, - "step": 0.01, + "step": 0.01 }, "ground": { "label": "Ground", @@ -18,7 +18,7 @@ var filters = { "init": 0.5, "min": -1.0, "max": 1.0, - "step": 0.01, + "step": 0.01 }, "scale": { "label": "Scale", @@ -26,7 +26,7 @@ var filters = { "init": 0.40, "min": 0.0, "max": 1.0, - "step": 0.01, + "step": 0.01 }, "jump_height": { "label": "Jump Height", @@ -34,7 +34,7 @@ var filters = { "init": 4.0, "min": 1.0, "max": 10.0, - "step": 0.01, + "step": 0.01 }, "hops": { "label": "Hops Count", @@ -42,7 +42,7 @@ var filters = { "init": 2.0, "min": 1.0, "max": 5.0, - "step": 1.0, + "step": 1.0 } }, "vertex": "#version 100\nprecision mediump float;\n\nattribute vec2 meshPosition;\nuniform float time;\nuniform vec2 emoteSize;\n\nuniform float interval;\nuniform float ground;\nuniform float scale;\nuniform float jump_height;\nuniform float hops;\n\nvarying vec2 uv;\n\nfloat sliding_from_left_to_right(float time_interval) {\n return (mod(time, time_interval) - time_interval * 0.5) / (time_interval * 0.5);\n}\n\nfloat flipping_directions(float time_interval) {\n return 1.0 - 2.0 * mod(floor(time / time_interval), 2.0);\n}\n\nvoid main() {\n float x_time_interval = interval;\n float y_time_interval = x_time_interval / (2.0 * hops);\n vec2 offset = vec2(\n sliding_from_left_to_right(x_time_interval) * flipping_directions(x_time_interval) * (1.0 - scale),\n ((sliding_from_left_to_right(y_time_interval) * flipping_directions(y_time_interval) + 1.0) / jump_height) - ground);\n\n gl_Position = vec4(\n meshPosition * scale + offset,\n 0.0,\n 1.0);\n\n uv = (meshPosition + vec2(1.0, 1.0)) / 2.0;\n\n uv.x = (flipping_directions(x_time_interval) + 1.0) / 2.0 - uv.x * flipping_directions(x_time_interval);\n}\n", @@ -69,24 +69,24 @@ var filters = { "init": 5.0, "min": 1.0, "max": 10.0, - "step": 0.1, + "step": 0.1 }, "scale": { "type": "float", "init": 0.30, "min": 0.0, "max": 1.0, - "step": 0.01, + "step": 0.01 } }, "vertex": "#version 100\nprecision mediump float;\n\nattribute vec2 meshPosition;\n\nuniform vec2 resolution;\nuniform float time;\n\nuniform float period;\nuniform float scale;\n\nvarying vec2 uv;\n\nvoid main() {\n vec2 offset = vec2(0.0, (2.0 * abs(sin(time * period)) - 1.0) * (1.0 - scale));\n gl_Position = vec4(meshPosition * scale + offset, 0.0, 1.0);\n uv = (meshPosition + 1.0) / 2.0;\n}\n", - "fragment": "\n#version 100\n\nprecision mediump float;\n\nuniform vec2 resolution;\nuniform float time;\n\nuniform sampler2D emote;\n\nvarying vec2 uv;\n\nvoid main() {\n gl_FragColor = texture2D(emote, vec2(uv.x, 1.0 - uv.y));\n gl_FragColor.w = floor(gl_FragColor.w + 0.5);\n}\n", + "fragment": "\n#version 100\n\nprecision mediump float;\n\nuniform vec2 resolution;\nuniform float time;\n\nuniform sampler2D emote;\n\nvarying vec2 uv;\n\nvoid main() {\n gl_FragColor = texture2D(emote, vec2(uv.x, 1.0 - uv.y));\n gl_FragColor.w = floor(gl_FragColor.w + 0.5);\n}\n" }, "Circle": { "transparent": 0x00FF00 + "", "duration": "Math.PI / 4.0", "vertex": "#version 100\nprecision mediump float;\n\nattribute vec2 meshPosition;\n\nuniform vec2 resolution;\nuniform float time;\n\nvarying vec2 uv;\n\nvec2 rotate(vec2 v, float a) {\n\tfloat s = sin(a);\n\tfloat c = cos(a);\n\tmat2 m = mat2(c, -s, s, c);\n\treturn m * v;\n}\n\nvoid main() {\n float scale = 0.30;\n float period_interval = 8.0;\n float pi = 3.141592653589793238;\n vec2 outer_circle = vec2(cos(period_interval * time), sin(period_interval * time)) * (1.0 - scale);\n vec2 inner_circle = rotate(meshPosition * scale, (-period_interval * time) + pi / 2.0);\n gl_Position = vec4(\n inner_circle + outer_circle,\n 0.0,\n 1.0);\n uv = (meshPosition + 1.0) / 2.0;\n}\n", - "fragment": "\n#version 100\n\nprecision mediump float;\n\nuniform vec2 resolution;\nuniform float time;\n\nuniform sampler2D emote;\n\nvarying vec2 uv;\n\nvoid main() {\n float speed = 1.0;\n gl_FragColor = texture2D(emote, vec2(uv.x, 1.0 - uv.y));\n gl_FragColor.w = floor(gl_FragColor.w + 0.5);\n}\n", + "fragment": "\n#version 100\n\nprecision mediump float;\n\nuniform vec2 resolution;\nuniform float time;\n\nuniform sampler2D emote;\n\nvarying vec2 uv;\n\nvoid main() {\n float speed = 1.0;\n gl_FragColor = texture2D(emote, vec2(uv.x, 1.0 - uv.y));\n gl_FragColor.w = floor(gl_FragColor.w + 0.5);\n}\n" }, "Slide": { "transparent": 0x00FF00 + "", @@ -110,25 +110,25 @@ var filters = { "transparent": 0x00FF00 + "", "duration": "1 / 4", "vertex": "#version 100\nprecision mediump float;\n\nattribute vec2 meshPosition;\n\nuniform vec2 resolution;\nuniform float time;\n\nvarying vec2 uv;\n\nvoid main() {\n gl_Position = vec4(meshPosition, 0.0, 1.0);\n uv = (meshPosition + 1.0) / 2.0;\n}\n", - "fragment": "\n#version 100\n\nprecision mediump float;\n\nuniform vec2 resolution;\nuniform float time;\n\nuniform sampler2D emote;\n\nvarying vec2 uv;\n\nfloat slide(float speed, float value) {\n return mod(value - speed * time, 1.0);\n}\n\nvoid main() {\n float speed = 4.0;\n gl_FragColor = texture2D(emote, vec2(slide(speed, uv.x), 1.0 - uv.y));\n gl_FragColor.w = floor(gl_FragColor.w + 0.5);\n}\n", + "fragment": "\n#version 100\n\nprecision mediump float;\n\nuniform vec2 resolution;\nuniform float time;\n\nuniform sampler2D emote;\n\nvarying vec2 uv;\n\nfloat slide(float speed, float value) {\n return mod(value - speed * time, 1.0);\n}\n\nvoid main() {\n float speed = 4.0;\n gl_FragColor = texture2D(emote, vec2(slide(speed, uv.x), 1.0 - uv.y));\n gl_FragColor.w = floor(gl_FragColor.w + 0.5);\n}\n" }, "Elevator": { "transparent": 0x00FF00 + "", "duration": "1 / 4", "vertex": "#version 100\nprecision mediump float;\n\nattribute vec2 meshPosition;\n\nuniform vec2 resolution;\nuniform float time;\n\nvarying vec2 uv;\n\nvoid main() {\n gl_Position = vec4(meshPosition, 0.0, 1.0);\n uv = (meshPosition + 1.0) / 2.0;\n}\n", - "fragment": "\n#version 100\n\nprecision mediump float;\n\nuniform vec2 resolution;\nuniform float time;\n\nuniform sampler2D emote;\n\nvarying vec2 uv;\n\nfloat slide(float speed, float value) {\n return mod(value - speed * time, 1.0);\n}\n\nvoid main() {\n float speed = 4.0;\n gl_FragColor = texture2D(\n emote,\n vec2(uv.x, slide(speed, 1.0 - uv.y)));\n gl_FragColor.w = floor(gl_FragColor.w + 0.5);\n}\n", + "fragment": "\n#version 100\n\nprecision mediump float;\n\nuniform vec2 resolution;\nuniform float time;\n\nuniform sampler2D emote;\n\nvarying vec2 uv;\n\nfloat slide(float speed, float value) {\n return mod(value - speed * time, 1.0);\n}\n\nvoid main() {\n float speed = 4.0;\n gl_FragColor = texture2D(\n emote,\n vec2(uv.x, slide(speed, 1.0 - uv.y)));\n gl_FragColor.w = floor(gl_FragColor.w + 0.5);\n}\n" }, "Rain": { "transparent": 0x00FF00 + "", "duration": "1", "vertex": "#version 100\nprecision mediump float;\n\nattribute vec2 meshPosition;\n\nuniform vec2 resolution;\nuniform float time;\n\nvarying vec2 uv;\n\nvoid main() {\n gl_Position = vec4(meshPosition, 0.0, 1.0);\n uv = (meshPosition + 1.0) / 2.0;\n}\n", - "fragment": "\n#version 100\n\nprecision mediump float;\n\nuniform vec2 resolution;\nuniform float time;\n\nuniform sampler2D emote;\n\nvarying vec2 uv;\n\nfloat slide(float speed, float value) {\n return mod(value - speed * time, 1.0);\n}\n\nvoid main() {\n float speed = 1.0;\n gl_FragColor = texture2D(\n emote,\n vec2(mod(4.0 * slide(speed, uv.x), 1.0),\n mod(4.0 * slide(speed, 1.0 - uv.y), 1.0)));\n gl_FragColor.w = floor(gl_FragColor.w + 0.5);\n}\n", + "fragment": "\n#version 100\n\nprecision mediump float;\n\nuniform vec2 resolution;\nuniform float time;\n\nuniform sampler2D emote;\n\nvarying vec2 uv;\n\nfloat slide(float speed, float value) {\n return mod(value - speed * time, 1.0);\n}\n\nvoid main() {\n float speed = 1.0;\n gl_FragColor = texture2D(\n emote,\n vec2(mod(4.0 * slide(speed, uv.x), 1.0),\n mod(4.0 * slide(speed, 1.0 - uv.y), 1.0)));\n gl_FragColor.w = floor(gl_FragColor.w + 0.5);\n}\n" }, "Pride": { "transparent": null, "duration": "2.0", "vertex": "#version 100\nprecision mediump float;\n\nattribute vec2 meshPosition;\n\nuniform vec2 resolution;\nuniform float time;\n\nvarying vec2 uv;\n\nvoid main() {\n gl_Position = vec4(meshPosition, 0.0, 1.0);\n uv = (meshPosition + 1.0) / 2.0;\n}\n", - "fragment": "\n#version 100\n\nprecision mediump float;\n\nuniform vec2 resolution;\nuniform float time;\n\nuniform sampler2D emote;\n\nvarying vec2 uv;\n\nvec3 hsl2rgb(vec3 c) {\n vec3 rgb = clamp(abs(mod(c.x*6.0+vec3(0.0,4.0,2.0),6.0)-3.0)-1.0, 0.0, 1.0);\n return c.z + c.y * (rgb-0.5)*(1.0-abs(2.0*c.z-1.0));\n}\n\nvoid main() {\n float speed = 1.0;\n\n vec4 pixel = texture2D(emote, vec2(uv.x, 1.0 - uv.y));\n pixel.w = floor(pixel.w + 0.5);\n pixel = vec4(mix(vec3(1.0), pixel.xyz, pixel.w), 1.0);\n vec4 rainbow = vec4(hsl2rgb(vec3((time - uv.x - uv.y) * 0.5, 1.0, 0.80)), 1.0);\n gl_FragColor = pixel * rainbow;\n}\n", + "fragment": "\n#version 100\n\nprecision mediump float;\n\nuniform vec2 resolution;\nuniform float time;\n\nuniform sampler2D emote;\n\nvarying vec2 uv;\n\nvec3 hsl2rgb(vec3 c) {\n vec3 rgb = clamp(abs(mod(c.x*6.0+vec3(0.0,4.0,2.0),6.0)-3.0)-1.0, 0.0, 1.0);\n return c.z + c.y * (rgb-0.5)*(1.0-abs(2.0*c.z-1.0));\n}\n\nvoid main() {\n float speed = 1.0;\n\n vec4 pixel = texture2D(emote, vec2(uv.x, 1.0 - uv.y));\n pixel.w = floor(pixel.w + 0.5);\n pixel = vec4(mix(vec3(1.0), pixel.xyz, pixel.w), 1.0);\n vec4 rainbow = vec4(hsl2rgb(vec3((time - uv.x - uv.y) * 0.5, 1.0, 0.80)), 1.0);\n gl_FragColor = pixel * rainbow;\n}\n" }, "Hard": { "transparent": 0x00FF00 + "", @@ -139,31 +139,31 @@ var filters = { "init": 1.4, "min": 0.0, "max": 6.9, - "step": 0.1, + "step": 0.1 }, "intensity": { "type": "float", "init": 32.0, "min": 1.0, "max": 42.0, - "step": 1.0, + "step": 1.0 }, "amplitude": { "type": "float", "init": 1.0 / 8.0, "min": 0.0, "max": 1.0 / 2.0, - "step": 0.001, - }, + "step": 0.001 + } }, "vertex": "#version 100\nprecision mediump float;\n\nattribute vec2 meshPosition;\n\nuniform vec2 resolution;\nuniform float time;\n\nuniform float zoom;\nuniform float intensity;\nuniform float amplitude;\n\nvarying vec2 uv;\n\nvoid main() {\n vec2 shaking = vec2(cos(intensity * time), sin(intensity * time)) * amplitude;\n gl_Position = vec4(meshPosition * zoom + shaking, 0.0, 1.0);\n uv = (meshPosition + 1.0) / 2.0;\n}\n", - "fragment": "\n#version 100\n\nprecision mediump float;\n\nuniform vec2 resolution;\nuniform float time;\n\nuniform sampler2D emote;\n\nvarying vec2 uv;\n\nvoid main() {\n gl_FragColor = texture2D(emote, vec2(uv.x, 1.0 - uv.y));\n gl_FragColor.w = floor(gl_FragColor.w + 0.5);\n}\n", + "fragment": "\n#version 100\n\nprecision mediump float;\n\nuniform vec2 resolution;\nuniform float time;\n\nuniform sampler2D emote;\n\nvarying vec2 uv;\n\nvoid main() {\n gl_FragColor = texture2D(emote, vec2(uv.x, 1.0 - uv.y));\n gl_FragColor.w = floor(gl_FragColor.w + 0.5);\n}\n" }, "Peek": { "transparent": 0x00FF00 + "", "duration": "2.0 * Math.PI", "vertex": "#version 100\nprecision mediump float;\n\nattribute vec2 meshPosition;\n\nuniform vec2 resolution;\nuniform float time;\n\nvarying vec2 uv;\n\nvoid main() {\n float time_clipped= mod(time * 2.0, (4.0 * 3.14));\n\n float s1 = float(time_clipped < (2.0 * 3.14));\n float s2 = 1.0 - s1;\n\n float hold1 = float(time_clipped > (0.5 * 3.14) && time_clipped < (2.0 * 3.14));\n float hold2 = 1.0 - float(time_clipped > (2.5 * 3.14) && time_clipped < (4.0 * 3.14));\n\n float cycle_1 = 1.0 - ((s1 * sin(time_clipped) * (1.0 - hold1)) + hold1);\n float cycle_2 = s2 * hold2 * (sin(time_clipped) - 1.0); \n\n gl_Position = vec4(meshPosition.x + 1.0 + cycle_1 + cycle_2 , meshPosition.y, 0.0, 1.0);\n uv = (meshPosition + 1.0) / 2.0;\n}\n", - "fragment": "\n#version 100\n\nprecision mediump float;\n\nuniform vec2 resolution;\nuniform float time;\n\nuniform sampler2D emote;\n\nvarying vec2 uv;\n\nvoid main() {\n gl_FragColor = texture2D(emote, vec2(uv.x, 1.0 - uv.y));\n gl_FragColor.w = floor(gl_FragColor.w + 0.5);\n}\n", + "fragment": "\n#version 100\n\nprecision mediump float;\n\nuniform vec2 resolution;\nuniform float time;\n\nuniform sampler2D emote;\n\nvarying vec2 uv;\n\nvoid main() {\n gl_FragColor = texture2D(emote, vec2(uv.x, 1.0 - uv.y));\n gl_FragColor.w = floor(gl_FragColor.w + 0.5);\n}\n" }, "Matrix": { "transparent": null, @@ -186,25 +186,25 @@ var filters = { "init": 6.0, "min": 1.0, "max": 16.0, - "step": 1.0, + "step": 1.0 }, "delay": { "type": "float", "init": 0.2, "min": 0.0, "max": 1.0, - "step": 0.1, + "step": 0.1 }, "pixelization": { "type": "float", "init": 1.0, "min": 1.0, "max": 3.0, - "step": 1.0, - }, + "step": 1.0 + } }, "vertex": "#version 100\nprecision mediump float;\n\nattribute vec2 meshPosition;\n\nuniform vec2 resolution;\nuniform float time;\n\nvarying vec2 uv;\n\nvoid main() {\n gl_Position = vec4(meshPosition, 0.0, 1.0);\n uv = (meshPosition + 1.0) / 2.0;\n}\n", - "fragment": "\n#version 100\n\nprecision mediump float;\n\nuniform vec2 resolution;\nuniform float time;\nuniform float duration;\nuniform float delay;\nuniform float pixelization;\n\nuniform sampler2D emote;\n\nvarying vec2 uv;\n\n// https://www.aussiedwarf.com/2017/05/09/Random10Bit.html\nfloat rand(vec2 co){\n vec3 product = vec3( sin( dot(co, vec2(0.129898,0.78233))),\n sin( dot(co, vec2(0.689898,0.23233))),\n sin( dot(co, vec2(0.434198,0.51833))) );\n vec3 weighting = vec3(4.37585453723, 2.465973, 3.18438);\n return fract(dot(weighting, product));\n}\n\nvoid main() {\n float pixelated_resolution = 112.0 / pixelization;\n vec2 pixelated_uv = floor(uv * pixelated_resolution);\n float noise = (rand(pixelated_uv) + 1.0) / 2.0;\n float slope = (0.2 + noise * 0.8) * (1.0 - (0.0 + uv.x * 0.5));\n float time_interval = 1.1 + delay * 2.0;\n float progress = 0.2 + delay + slope - mod(time_interval * time / duration, time_interval);\n float mask = progress > 0.1 ? 1.0 : 0.0;\n vec4 pixel = texture2D(emote, vec2(uv.x * (progress > 0.5 ? 1.0 : progress * 2.0), 1.0 - uv.y));\n pixel.w = floor(pixel.w + 0.5);\n gl_FragColor = pixel * vec4(vec3(1.0), mask);\n}\n", + "fragment": "\n#version 100\n\nprecision mediump float;\n\nuniform vec2 resolution;\nuniform float time;\nuniform float duration;\nuniform float delay;\nuniform float pixelization;\n\nuniform sampler2D emote;\n\nvarying vec2 uv;\n\n// https://www.aussiedwarf.com/2017/05/09/Random10Bit.html\nfloat rand(vec2 co){\n vec3 product = vec3( sin( dot(co, vec2(0.129898,0.78233))),\n sin( dot(co, vec2(0.689898,0.23233))),\n sin( dot(co, vec2(0.434198,0.51833))) );\n vec3 weighting = vec3(4.37585453723, 2.465973, 3.18438);\n return fract(dot(weighting, product));\n}\n\nvoid main() {\n float pixelated_resolution = 112.0 / pixelization;\n vec2 pixelated_uv = floor(uv * pixelated_resolution);\n float noise = (rand(pixelated_uv) + 1.0) / 2.0;\n float slope = (0.2 + noise * 0.8) * (1.0 - (0.0 + uv.x * 0.5));\n float time_interval = 1.1 + delay * 2.0;\n float progress = 0.2 + delay + slope - mod(time_interval * time / duration, time_interval);\n float mask = progress > 0.1 ? 1.0 : 0.0;\n vec4 pixel = texture2D(emote, vec2(uv.x * (progress > 0.5 ? 1.0 : progress * 2.0), 1.0 - uv.y));\n pixel.w = floor(pixel.w + 0.5);\n gl_FragColor = pixel * vec4(vec3(1.0), mask);\n}\n" }, "Ripple": { "transparent": 0x00FF00 + "", @@ -216,7 +216,7 @@ var filters = { "init": 12.0, "min": 0.01, "max": 24.0, - "step": 0.01, + "step": 0.01 }, "b": { "label": "Time Freq", @@ -224,7 +224,7 @@ var filters = { "init": 4.0, "min": 0.01, "max": 8.0, - "step": 0.01, + "step": 0.01 }, "c": { "label": "Amplitude", @@ -232,10 +232,10 @@ var filters = { "init": 0.03, "min": 0.01, "max": 0.06, - "step": 0.01, + "step": 0.01 } }, "vertex": "#version 100\nprecision mediump float;\n\nattribute vec2 meshPosition;\n\nuniform vec2 resolution;\nuniform float time;\n\nvarying vec2 uv;\n\nvoid main() {\n gl_Position = vec4(meshPosition, 0.0, 1.0);\n uv = (meshPosition + 1.0) / 2.0;\n}\n", - "fragment": "#version 100\n\nprecision mediump float;\n\nuniform vec2 resolution;\nuniform float time;\n\nuniform sampler2D emote;\n\nuniform float a;\nuniform float b;\nuniform float c;\n\nvarying vec2 uv;\n\nvoid main() {\n vec2 pos = vec2(uv.x, 1.0 - uv.y);\n vec2 center = vec2(0.5);\n vec2 dir = pos - center;\n float x = length(dir);\n float y = sin(x + time);\n vec4 pixel = texture2D(emote, pos + cos(x*a - time*b)*c*(dir/x));\n gl_FragColor = pixel;\n gl_FragColor.w = floor(gl_FragColor.w + 0.5);\n}\n", + "fragment": "#version 100\n\nprecision mediump float;\n\nuniform vec2 resolution;\nuniform float time;\n\nuniform sampler2D emote;\n\nuniform float a;\nuniform float b;\nuniform float c;\n\nvarying vec2 uv;\n\nvoid main() {\n vec2 pos = vec2(uv.x, 1.0 - uv.y);\n vec2 center = vec2(0.5);\n vec2 dir = pos - center;\n float x = length(dir);\n float y = sin(x + time);\n vec4 pixel = texture2D(emote, pos + cos(x*a - time*b)*c*(dir/x));\n gl_FragColor = pixel;\n gl_FragColor.w = floor(gl_FragColor.w + 0.5);\n}\n" } }; diff --git a/js/index.js b/js/index.js index de6ae23..b9ea9e4 100644 --- a/js/index.js +++ b/js/index.js @@ -71,7 +71,7 @@ function loadFilterProgram(gl, filter, vertexAttribs) { var uniforms = { "resolution": gl.getUniformLocation(id, 'resolution'), "time": gl.getUniformLocation(id, 'time'), - "emoteSize": gl.getUniformLocation(id, 'emoteSize'), + "emoteSize": gl.getUniformLocation(id, 'emoteSize') }; var paramsPanel = div().att$("class", "widget-element"); var paramsInputs = {}; @@ -129,7 +129,7 @@ function loadFilterProgram(gl, filter, vertexAttribs) { "uniforms": uniforms, "duration": compile_expr(filter.duration), "transparent": filter.transparent, - "paramsPanel": paramsPanel, + "paramsPanel": paramsPanel }; } function ImageSelector() { @@ -286,7 +286,7 @@ function FilterSelector() { quality: 10, width: CANVAS_WIDTH, height: CANVAS_HEIGHT, - transparent: program.transparent, + transparent: program.transparent }); var context = { "vars": { @@ -346,7 +346,7 @@ function FilterSelector() { } gif.addFrame(new ImageData(pixels, CANVAS_WIDTH, CANVAS_HEIGHT), { delay: dt * 1000, - dispose: 2, + dispose: 2 }); renderProgress.style.width = (t / duration) * 50 + "%"; t += dt; @@ -382,6 +382,16 @@ function FilterSelector() { return root; } window.onload = function () { + if ("serviceWorker" in navigator) { + navigator.serviceWorker.register('/serviceworker.js').then(function (registration) { + console.log("Registered a Service Worker ", registration); + }, function (error) { + console.error("Could not register a Service Worker ", error); + }); + } + else { + console.error("Service Workers are not supported in this browser."); + } feature_params = new URLSearchParams(document.location.search).has("feature-params"); var filterSelectorEntry = document.getElementById('filter-selector-entry'); if (filterSelectorEntry === null) { diff --git a/package-lock.json b/package-lock.json index 3c6686b..6de1ed3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,8 +1,54 @@ { "name": "emotejam", "version": "1.0.0", - "lockfileVersion": 1, + "lockfileVersion": 2, "requires": true, + "packages": { + "": { + "name": "emotejam", + "version": "1.0.0", + "license": "MIT", + "devDependencies": { + "@types/gif.js": "^0.2.1", + "@types/node": "^20.9.3", + "typescript": "^4.3.2" + } + }, + "node_modules/@types/gif.js": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/@types/gif.js/-/gif.js-0.2.1.tgz", + "integrity": "sha512-oZAPX8pgueiAngu3HfynjdtsDNt4EiD1fs5An//LtBKvOdnc4Wq8/S7GkAKpP80+29WyVwGEGVEUXPyFhbQ2+g==", + "dev": true + }, + "node_modules/@types/node": { + "version": "20.9.3", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.9.3.tgz", + "integrity": "sha512-nk5wXLAXGBKfrhLB0cyHGbSqopS+nz0BUgZkUQqSHSSgdee0kssp1IAqlQOu333bW+gMNs2QREx7iynm19Abxw==", + "dev": true, + "dependencies": { + "undici-types": "~5.26.4" + } + }, + "node_modules/typescript": { + "version": "4.3.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.3.2.tgz", + "integrity": "sha512-zZ4hShnmnoVnAHpVHWpTcxdv7dWP60S2FsydQLV8V5PbS3FifjWFFRiHSWpDJahly88PRyV5teTSLoq4eG7mKw==", + "dev": true, + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=4.2.0" + } + }, + "node_modules/undici-types": { + "version": "5.26.5", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz", + "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==", + "dev": true + } + }, "dependencies": { "@types/gif.js": { "version": "0.2.1", @@ -10,11 +56,26 @@ "integrity": "sha512-oZAPX8pgueiAngu3HfynjdtsDNt4EiD1fs5An//LtBKvOdnc4Wq8/S7GkAKpP80+29WyVwGEGVEUXPyFhbQ2+g==", "dev": true }, + "@types/node": { + "version": "20.9.3", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.9.3.tgz", + "integrity": "sha512-nk5wXLAXGBKfrhLB0cyHGbSqopS+nz0BUgZkUQqSHSSgdee0kssp1IAqlQOu333bW+gMNs2QREx7iynm19Abxw==", + "dev": true, + "requires": { + "undici-types": "~5.26.4" + } + }, "typescript": { "version": "4.3.2", "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.3.2.tgz", "integrity": "sha512-zZ4hShnmnoVnAHpVHWpTcxdv7dWP60S2FsydQLV8V5PbS3FifjWFFRiHSWpDJahly88PRyV5teTSLoq4eG7mKw==", "dev": true + }, + "undici-types": { + "version": "5.26.5", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-5.26.5.tgz", + "integrity": "sha512-JlCMO+ehdEIKqlFxk6IfVoAUVmgz7cU7zD/h9XZ0qzeosSHmUJVOzSQvvYSYWXkFXC+IfLKSIffhv0sVZup6pA==", + "dev": true } } } diff --git a/package.json b/package.json index e50c61a..a56e15a 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,11 @@ "version": "1.0.0", "description": "Simple website that generates animated BTTV emotes from static images", "main": "index.js", + "type": "module", "scripts": { + "build": "node build.js build", + "watch": "node build.js watch", + "serve": "node build.js serve", "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { @@ -27,6 +31,7 @@ "homepage": "https://github.com/tsoding/emoteJAM#readme", "devDependencies": { "@types/gif.js": "^0.2.1", + "@types/node": "^20.9.3", "typescript": "^4.3.2" } } diff --git a/serviceworker.js b/serviceworker.js new file mode 100644 index 0000000..653cca4 --- /dev/null +++ b/serviceworker.js @@ -0,0 +1,128 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __generator = (this && this.__generator) || function (thisArg, body) { + var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; + return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; + function verb(n) { return function (v) { return step([n, v]); }; } + function step(op) { + if (f) throw new TypeError("Generator is already executing."); + while (_) try { + if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; + if (y = 0, t) op = [op[0] & 2, t.value]; + switch (op[0]) { + case 0: case 1: t = op; break; + case 4: _.label++; return { value: op[1], done: false }; + case 5: _.label++; y = op[1]; op = [0]; continue; + case 7: op = _.ops.pop(); _.trys.pop(); continue; + default: + if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } + if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } + if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } + if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } + if (t[2]) _.ops.pop(); + _.trys.pop(); continue; + } + op = body.call(thisArg, _); + } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } + if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; + } +}; +var cacheName = "emoteJAM-v1"; +var assets = [ + '/index.html', + '/css/bright.css', + '/css/main.css', + '/css/reset.css', + '/gif.js', + '/gif.worker.js', + '/img/tsodinClown.png', + '/js/eval.js', + '/js/filters.js', + '/js/grecha.js', + '/js/index.js', +]; +self.addEventListener("install", function (e) { + console.log("[Service Worker] Install"); + var event = e; + event.waitUntil((function () { return __awaiter(void 0, void 0, void 0, function () { + var cache; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + console.log("[Service Worker] Caching all the assets"); + return [4, caches.open(cacheName)]; + case 1: + cache = _a.sent(); + cache.addAll(assets); + return [2]; + } + }); + }); })()); +}); +self.addEventListener("activate", function (e) { + console.log("[Service Worker] Activate"); + var event = e; + event.waitUntil((function () { return __awaiter(void 0, void 0, void 0, function () { + var keys, _a, _b, _i, key; + return __generator(this, function (_c) { + switch (_c.label) { + case 0: + console.log("[Service Worker] Cleaning up all caches"); + return [4, caches.keys()]; + case 1: + keys = _c.sent(); + _a = []; + for (_b in keys) + _a.push(_b); + _i = 0; + _c.label = 2; + case 2: + if (!(_i < _a.length)) return [3, 5]; + key = _a[_i]; + if (!(key !== cacheName)) return [3, 4]; + return [4, caches["delete"](key)]; + case 3: + _c.sent(); + _c.label = 4; + case 4: + _i++; + return [3, 2]; + case 5: return [2]; + } + }); + }); })()); +}); +self.addEventListener("fetch", function (e) { + var event = e; + event.respondWith((function () { return __awaiter(void 0, void 0, void 0, function () { + var cache, response; + return __generator(this, function (_a) { + switch (_a.label) { + case 0: + console.log("[Service Worker] Fetch " + event.request.url); + return [4, caches.open(cacheName)]; + case 1: + cache = _a.sent(); + return [4, cache.match(event.request)]; + case 2: + response = _a.sent(); + if (!(response === undefined)) return [3, 4]; + console.log("[Service Worker] Response for " + event.request.url + " is not available in cache. Making an actual request..."); + return [4, fetch(event.request)]; + case 3: + response = _a.sent(); + cache.put(event.request, response.clone()); + _a.label = 4; + case 4: return [2, response]; + } + }); + }); })()); +}); diff --git a/serviceworker.ts b/serviceworker.ts new file mode 100644 index 0000000..78164d3 --- /dev/null +++ b/serviceworker.ts @@ -0,0 +1,53 @@ +const cacheName = "emoteJAM-v1"; +const assets = [ + '/index.html', + '/css/bright.css', + '/css/main.css', + '/css/reset.css', + '/gif.js', + '/gif.worker.js', + '/img/tsodinClown.png', + '/js/eval.js', + '/js/filters.js', + '/js/grecha.js', + '/js/index.js', +]; + +self.addEventListener("install", e => { + console.log("[Service Worker] Install"); + const event = e as ExtendableEvent; + event.waitUntil((async () => { + console.log("[Service Worker] Caching all the assets"); + const cache = await caches.open(cacheName); + cache.addAll(assets); + })()); +}); + +self.addEventListener("activate", e => { + console.log("[Service Worker] Activate"); + const event = e as ExtendableEvent; + event.waitUntil((async() => { + console.log("[Service Worker] Cleaning up all caches"); + const keys = await caches.keys(); + for (let key in keys) { + if (key !== cacheName) { + await caches.delete(key); + } + } + })()); +}); + +self.addEventListener("fetch", (e) => { + const event = e as FetchEvent; + event.respondWith((async () => { + console.log(`[Service Worker] Fetch ${event.request.url}`); + const cache = await caches.open(cacheName); + let response = await cache.match(event.request); + if (response === undefined) { + console.log(`[Service Worker] Response for ${event.request.url} is not available in cache. Making an actual request...`); + response = await fetch(event.request); + cache.put(event.request, response.clone()); + } + return response; + })()); +}); diff --git a/ts/index.ts b/ts/index.ts index 4c8abf2..00db943 100644 --- a/ts/index.ts +++ b/ts/index.ts @@ -512,6 +512,19 @@ function FilterSelector() { } window.onload = () => { + if ("serviceWorker" in navigator) { + navigator.serviceWorker.register('/serviceworker.js').then( + (registration) => { + console.log("Registered a Service Worker ", registration); + }, + (error) => { + console.error("Could not register a Service Worker ", error); + }, + ); + } else { + console.error("Service Workers are not supported in this browser."); + } + feature_params = new URLSearchParams(document.location.search).has("feature-params"); const filterSelectorEntry = document.getElementById('filter-selector-entry'); diff --git a/tsconfig.json b/tsconfig.json deleted file mode 100644 index 86022aa..0000000 --- a/tsconfig.json +++ /dev/null @@ -1,72 +0,0 @@ -{ - "compilerOptions": { - /* Visit https://aka.ms/tsconfig.json to read more about this file */ - - /* Basic Options */ - // "incremental": true, /* Enable incremental compilation */ - "target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', 'ES2021', or 'ESNEXT'. */ - "module": "none", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */ - "lib": ["es2019", "dom"], /* Specify library files to be included in the compilation. */ - // "allowJs": true, /* Allow javascript files to be compiled. */ - // "checkJs": true, /* Report errors in .js files. */ - // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', 'react', 'react-jsx' or 'react-jsxdev'. */ - // "declaration": true, /* Generates corresponding '.d.ts' file. */ - // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ - // "sourceMap": true, /* Generates corresponding '.map' file. */ - // "outFile": "./", /* Concatenate and emit output to single file. */ - "outDir": "./js/", /* Redirect output structure to the directory. */ - "rootDir": "./ts/", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ - // "composite": true, /* Enable project compilation */ - // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */ - "removeComments": true, /* Do not emit comments to output. */ - // "noEmit": true, /* Do not emit outputs. */ - // "importHelpers": true, /* Import emit helpers from 'tslib'. */ - // "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */ - // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */ - - /* Strict Type-Checking Options */ - "strict": true, /* Enable all strict type-checking options. */ - // "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */ - // "strictNullChecks": true, /* Enable strict null checks. */ - // "strictFunctionTypes": true, /* Enable strict checking of function types. */ - // "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */ - // "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */ - // "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */ - // "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */ - - /* Additional Checks */ - // "noUnusedLocals": true, /* Report errors on unused locals. */ - // "noUnusedParameters": true, /* Report errors on unused parameters. */ - // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */ - // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */ - // "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */ - // "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an 'override' modifier. */ - // "noPropertyAccessFromIndexSignature": true, /* Require undeclared properties from index signatures to use element accesses. */ - - /* Module Resolution Options */ - // "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ - // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */ - // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ - // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ - // "typeRoots": [], /* List of folders to include type definitions from. */ - // "types": [], /* Type declaration files to be included in compilation. */ - // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */ - "esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */ - // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */ - // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ - - /* Source Map Options */ - // "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */ - // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ - // "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */ - // "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */ - - /* Experimental Options */ - // "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */ - // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */ - - /* Advanced Options */ - "skipLibCheck": true, /* Skip type checking of declaration files. */ - "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */ - } -}