Skip to content

Instantly share code, notes, and snippets.

View endel's full-sized avatar

Endel Dreyer endel

View GitHub Profile
@endel
endel / getMoonPhase.js
Created March 25, 2015 16:04
Get Moon Phase by Date, written in JavaScript
/*
* modified from http://www.voidware.com/moon_phase.htm
*/
function getMoonPhase(year, month, day)
{
var c = e = jd = b = 0;
if (month < 3) {
year--;
@endel
endel / conver.pe
Last active January 21, 2024 06:37
FontForge script to convert .ttf file to its webfont variations (.otf, .svg, .woff, .woff2)
#!/usr/local/bin/fontforge
Open($1)
Generate($1:r + ".otf")
Generate($1:r + ".svg")
Generate($1:r + ".woff")
Generate($1:r + ".woff2")
// This code was written by Tyler Akins and has been placed in the
// public domain. It would be nice if you left this header intact.
// Base64 code from Tyler Akins -- http://rumkin.com
var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
function encode64(input) {
var output = new StringMaker();
var chr1, chr2, chr3;
var enc1, enc2, enc3, enc4;
@endel
endel / aws-ec2-root-node-port-80.sh
Created December 4, 2019 18:58
Allow non-root node to use ports 80 (HTTP) and 443 (HTTPS) (AWS EC2)
# Allow non-root node to use ports 80 (HTTP) and 443 (HTTPS)
sudo setcap 'cap_net_bind_service=+ep' $(which node)
@endel
endel / number-pad-zero.js
Last active August 1, 2023 11:54
Simplest way for leading zero padding in JavaScript
Number.prototype.pad = function(size) {
var s = String(this);
while (s.length < (size || 2)) {s = "0" + s;}
return s;
}
(1).pad(3) // => "001"
(10).pad(3) // => "010"
(100).pad(3) // => "100"
@endel
endel / index.ts
Created July 17, 2023 17:29
Colyseus Schema - Listen for all changes and print them as they come
room.onStateChange.once(function() {
function registerCallbacksOnStructure (schemaInstance, path) {
const schema = schemaInstance['_definition'].schema;
for (let field in schema) {
const schemaType = typeof(schema[field]);
if (schemaType === "object") {
// on item added to collection
schemaInstance[field].onAdd(function (item, key) {
onItemAdd([...path, field], item, key);
@endel
endel / README.md
Last active June 13, 2022 15:29
Node.js Package Authors: Please support both CJS and ESM: https://dev.to/endel/nodejs-package-authors-please-support-both-cjs-and-esm-1oj3
@endel
endel / threejs-json-atlas-loader.js
Last active September 13, 2021 13:11 — forked from momo-the-monster/threejsatlasloader.js
three.js - JSON Atlas Loader
// three.js JSON Atlas Loader
// Requires: whatwg-fetch (https://github.com/github/fetch)
var materials = {}
fetch('images/spritesheet.json').then(function(response) {
return response.json()
}).then(function(json) {
var atlasTexture = THREE.ImageUtils.loadTexture('images/' + json.meta.image, undefined, function() {
@endel
endel / SchemaCallbacks.md
Last active August 22, 2021 13:26
New Schema Callback API Proposal

Hi everybody! Regarding the possible new schema callback API, so far this is what I could came up so far, feedback highly appreciated! Do let me know if you feel I need to clarify anything!

I understand this may be long and hard to grasp, I can provide a side-by-side comparison between the new API and previous one so you can clearly see the difference.

The new API would allow to create all the callbacks at once, after the connection with the room has been established (even on deep structures) - rather than binding callbacks as Schema instances that are created on-demand on the client-side.

Structures:

class Point extends Schema {
@endel
endel / generateUserAgent.ts
Created September 13, 2018 23:41
Fake User Agent Generator
export function* generateUserAgent() {
let webkitVersion = 10;
let chromeVersion = 1000;
const so = [
'Windows NT 6.1; WOW64',
'Windows NT 6.2; Win64; x64',
"Windows NT 5.1; Win64; x64",,
'Macintosh; Intel Mac OS X 10_12_6',
"X11; Linux x86_64",