Skip to content

Instantly share code, notes, and snippets.

@thysultan
thysultan / camelCase.js
Last active August 21, 2016 15:43
camelCase and undoCamelCase
function camelCase (string, separator) {
return string.split(separator).map((word, index) => word.substr(0,1)[index ? 'toUpperCase' : 'toLowerCase']() + word.substr(1) ).join('');
}
console.log(camelCase('metal gear solid', ' '));
// metalGearSolid
function undoCamelCase (string, separator) {
return string.split('').map((letter) => letter.toLowerCase() !== letter ? separator + letter.toLowerCase() : letter).join('');
}
@thysultan
thysultan / urlstore.js
Last active August 26, 2016 18:51
urlstore.js
param: function (obj, prefix) {
var arr = [];
for (var key in obj) {
var __prefix = prefix ? prefix + '[' + key + ']' : key,
value = obj[key];
arr.push(typeof value == 'object' ?
param(value, __prefix) :
encodeURIComponent(__prefix) + '=' + encodeURIComponent(value));
}
@thysultan
thysultan / frameLoop.js
Created August 27, 2016 15:18 — forked from thebuilder/frameLoop.js
Call a function with a specific FPS. Exposes methods to pause, start and destroy the looper. Uses ES6.
/**
* @param fn {Function} Callback function to trigger on frame
* @param fps {int} Target FPS
* @returns {{pause: pause, start: start, destroy: destroy}}
*/
function frameLoop(fn, fps=60) {
let then = 0;
let interval = 1000 / fps;
let isRunning = true;
let currentFrameId = null;
@thysultan
thysultan / flatten.js
Last active September 22, 2016 06:29
flatten array
/**
* flattens a nested array
* 1. takes an array and optional destination array
* 2. iterates through the array
* 3. if an item in the array is an array
* run through flatten with the second argument
* as the destination array
* this will add every item within the passed array
* into the destination array
function frameScheduler (fps) {
var highPriWork = [];
var lowPriWork = [];
var highPriWorkLen = 0;
var lowPriWorkLen = 0;
var frameBudget = 1000 / fps;
var lastTime = 0;
var asyncCallback;
var Promise = window.Promise;
var length = 1024*100; // enough to store 100kb of instrutions at any time
var action = new Uint8Array(length);
var memory = new Array(length);
// action carry instructions with pointers to memory for where to find the nodes to act on
// memory carries nodes that exist in memory
// for example

A Journey Into Building a Fast Text Editor With JS & Canvas!

Text editors have a way of looking simple but managing and painting text is no small task, this talk will give us a look into what bits(literaly) and pieces go into making a fast text editor with Javascript & Canvas. We'll look into what format the text you type is stored in, how it's mutated and highlighted, and how all of this is translated onto a canvas in less than the time it takes you to blink. As a by product we'll get to see how a regular expression engine is implementated and why games use draw distance to save precious hardware resources.

@thysultan
thysultan / interface.ts
Last active August 27, 2017 01:41
virtual node interface
interface Virtual {
merge(Virtual)
}
interface VirtualStyle extends Virtual {
merge(VirtualStyle)
}
interface VirtualProperties extends Virtual {
style: VirtualStyle
function Stack () {
this.next = this
this.prev = this
this.length = 0
}
Stack.prototype = {
/**
* @param {Object} node
* @param {Object} before
@thysultan
thysultan / worker.html
Last active December 14, 2017 16:25
DIO running in a web worker.
<script id="worker" type="worker">
importScripts('https://unpkg.com/dio.js')
const shared = {memory: {}, address: 0}
const allocateMemory = (object) => {
shared.memory[shared.address] = object
return shared.address++
}