Skip to content

Instantly share code, notes, and snippets.

View chinghanho's full-sized avatar
💁‍♂️
What's up?

Ching-Han Ho chinghanho

💁‍♂️
What's up?
View GitHub Profile
@chinghanho
chinghanho / eventemitter.js
Created September 16, 2020 06:27 — forked from mudge/eventemitter.js
A very simple EventEmitter in pure JavaScript (suitable for both node.js and browsers).
/* Polyfill indexOf. */
var indexOf;
if (typeof Array.prototype.indexOf === 'function') {
indexOf = function (haystack, needle) {
return haystack.indexOf(needle);
};
} else {
indexOf = function (haystack, needle) {
var i = 0, length = haystack.length, idx = -1, found = false;
@chinghanho
chinghanho / HEY-YOU.md
Created July 13, 2020 17:36 — forked from cowboy/HEY-YOU.md
jQuery Tiny Pub/Sub: A really, really, REALLY tiny pub/sub implementation for jQuery.
@chinghanho
chinghanho / paginated.js
Created October 15, 2019 16:30 — forked from jstott/paginated.js
lodash paginated items
function getPaginatedItems(items, page, pageSize) {
var pg = page || 1,
pgSize = pageSize || 100,
offset = (pg - 1) * pgSize,
pagedItems = _.drop(items, offset).slice(0, pgSize);
return {
page: pg,
pageSize: pgSize,
total: items.length,
total_pages: Math.ceil(items.length / pgSize),
@chinghanho
chinghanho / encoding-video.md
Created June 17, 2019 15:23 — forked from Vestride/encoding-video.md
Encoding video for the web

Encoding Video

Installing

Install FFmpeg with homebrew. You'll need to install it with a couple flags for webm and the AAC audio codec.

brew install ffmpeg --with-libvpx --with-libvorbis --with-fdk-aac --with-opus
const canvasSketch = require('canvas-sketch'); // not yet released tool
const Random = require('./util/random');
const { lerp } = require('./util/math');
// We can force a random seed or a specific string/number
Random.setSeed(Random.getRandomSeed());
const settings = {
pixelsPerInch: 300,
// When exporting, use the seed as the suffix
uniform sampler2D tex; // matched from gst-plugin-gl uniform
const vec4 kappa = vec4(1.0,1.7,0.7,15.0);
// These can be made uniform if they are added to:
// gst/gl/gstglfiltershader.c:363
// gst_gl_shader_set_uniform_1f (filtershader->shader0, "screen_width", width);
// gst_gl_shader_set_uniform_1f (filtershader->shader0, "screen_height", height);
const float screen_width = 1920.0;
@chinghanho
chinghanho / fuzzy_string_match.js
Created May 13, 2018 15:34 — forked from dtjm/fuzzy_string_match.js
Fuzzy string matching function for JavaScript
// Fuzzy matching algorithm
var fuzzyMatch = function(needle, haystack) {
if(needle === "" || haystack === "") return true;
needle = needle.toLowerCase().replace(/ /g, "");
haystack = haystack.toLowerCase();
// All characters in needle must be present in haystack
var j = 0; // haystack position
for(var i = 0; i < needle.length; i++) {
@chinghanho
chinghanho / what-forces-layout.md
Created January 13, 2018 02:13 — forked from paulirish/what-forces-layout.md
What forces layout/reflow. The comprehensive list.

What forces layout / reflow

All of the below properties or methods, when requested/called in JavaScript, will trigger the browser to synchronously calculate the style and layout*. This is also called reflow or layout thrashing, and is common performance bottleneck.

Element

Box metrics
  • elem.offsetLeft, elem.offsetTop, elem.offsetWidth, elem.offsetHeight, elem.offsetParent
  • elem.clientLeft, elem.clientTop, elem.clientWidth, elem.clientHeight
  • elem.getClientRects(), elem.getBoundingClientRect()
@chinghanho
chinghanho / bytesToSize.js
Created November 2, 2017 06:50 — forked from lanqy/bytesToSize.js
JavaScript To Convert Bytes To MB, KB, Etc
// from http://scratch99.com/web-development/javascript/convert-bytes-to-mb-kb/
function bytesToSize(bytes) {
var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
if (bytes == 0) return 'n/a';
var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
if (i == 0) return bytes + ' ' + sizes[i];
return (bytes / Math.pow(1024, i)).toFixed(1) + ' ' + sizes[i];
};
@chinghanho
chinghanho / velocity.js
Created September 6, 2017 16:20 — forked from yuheiy/velocity.js
add `easeOutBounce` to velocity.js
'use strict';
import Velocity from 'velocity-animate';
let baseEasings = {};
baseEasings.Bounce = p => {
let pow2;
let bounce = 4;
while (p < ((pow2 = Math.pow(2, --bounce)) - 1) / 11) {}