Skip to content

Instantly share code, notes, and snippets.

💭
"What I produce is 99.1% pure" - WW

Thomas Tortorini mr21

💭
"What I produce is 99.1% pure" - WW
Block or report user

Report or block mr21

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View audio-compressor.js
var vid = document.querySelector( "video" );
var audioCtx = new AudioContext();
var source = audioCtx.createMediaElementSource( vid );
var compressor = audioCtx.createDynamicsCompressor();
var now = audioCtx.currentTime;
compressor.threshold.setValueAtTime( -50, now );
compressor.knee.setValueAtTime( 40, now );
compressor.ratio.setValueAtTime( 32, now );
compressor.attack.setValueAtTime( 0, now );
View DragNDrop.js
"use strict";
class DragNDrop {
constructor( el, opt = {} ) {
this._clickedElem =
this._draggedElem =
this._dragoverBCR =
this._dragoverElem = null;
this._handleSelector = opt.handleSelector || "*";
el.addEventListener( "mousedown", this._onmousedown.bind( this ) );
View color.js
const Color = {
split( col ) {
switch ( col[ 0 ] ) {
case "r": return ( col[ 3 ] === "a"
? Color._rgbaExtract
: Color._rgbExtract )( col );
case "#": return ( col.length < 6
? Color._hex3Extract
: Color._hex6Extract )( col );
}
View todomvc-data.js
// create the new TodoMVC instance:
// ................................................................
const todomvc2 = new TodoMVC();
document.body.append( todomvc2.rootElement );
// move the first todo from the first todomvc to the second one
// ................................................................
const firstTodoId = Object.keys( todomvc.data )[ 0 ],
firstTodo = todomvc.data[ firstTodoId ];
View ElementMap.js
"use strict";
class ElementMap {
constructor( data ) {
this._map = new Map();
this._keyToElement = new Map();
this.size = data ? data.length : 0;
if ( data ) {
data.forEach( d => this._map.set( d[ 0 ], d[ 1 ] ) );
}
@mr21
mr21 / build-prod.sh
Last active Sep 5, 2018
This shell script prints all the CSS/JS in the same index.html file
View build-prod.sh
#!/bin/sh
# usage: ./build-prod.sh < index.html > index-prod.html
cssURLs=()
jsURLs=()
cssReg="<link.*['\"](.*\.css)['\"].*>"
jsReg="<script.*['\"](.*\.js)['\"].*></script>"
while read line
@mr21
mr21 / checkNaN.js
Created Aug 7, 2018
Math.min/max/round/... check for NaN
View checkNaN.js
( function() {
const _Math = {
min: Math.min,
max: Math.max,
abs: Math.abs,
ceil: Math.ceil,
floor: Math.floor,
round: Math.round,
};
@mr21
mr21 / MapTypeCheck.js
Created Jul 29, 2018
Avoid (by alerting) stringified number used as key into Map
View MapTypeCheck.js
( function() {
const
protoHas = Map.prototype.has,
protoGet = Map.prototype.get,
protoSet = Map.prototype.set,
protoDelete = Map.prototype.delete;
Map.prototype.has = function( id ) {
if ( typeof id === "string" && !isNaN( +id ) ) {
console.warn( `Map.has( "${ id }" )` );
@mr21
mr21 / random.js
Created Jul 5, 2018
A better random if needed
View random.js
function Random() {
return crypto.getRandomValues( Random.arr )[ 0 ] / Random.limit;
}
Random.limit = 2 ** 32;
Random.arr = new Uint32Array( 1 );
@mr21
mr21 / calc_schwarzschild_radius.js
Created May 14, 2018
Function to calcul the Schwarzschild radius with a mass
View calc_schwarzschild_radius.js
const lightSpeed = 299792458; // meter (per second)
const solarMass = 1.98855 * Math.pow( 10, 30 ); // kilograms
const gravity = 6.67408 * Math.pow( 10, -11 );
function calcSchwarzschildRadius( nbSolarMass ) {
return ( 2 * gravity * nbSolarMass * solarMass ) / ( lightSpeed * lightSpeed );
}
You can’t perform that action at this time.