Skip to content

Instantly share code, notes, and snippets.

View surma's full-sized avatar
🍑

Surma surma

🍑
View GitHub Profile
package main
import (
"flag"
"log"
"io/ioutil"
"net/http"
"time"
@surma
surma / index.html
Last active October 31, 2016 13:33
Stupid MIDI synthisizer
<script>
// 5 octaves + 9 semitones under A440Hz is Midi Key Zero
const MidiKeyZero = 440/(Math.pow(2, 5) * Math.pow(2, 9/12)) ;
const ctx = new AudioContext();
const oscis = new Map();
const volumeCtrl = ctx.createGain();
volumeCtrl.gain.value = 0;
volumeCtrl.connect(ctx.destination);
@surma
surma / staleWhileRevalidate.js
Last active April 8, 2024 22:38
ServiceWorker that implements “Stale-while-revalidate”
// Implements stale-while-revalidate
self.addEventListener('fetch', event => {
const cached = caches.match(event.request);
const fetched = fetch(event.request);
const fetchedCopy = fetched.then(resp => resp.clone());
// Call respondWith() with whatever we get first.
// If the fetch fails (e.g disconnected), wait for the cache.
// If there’s nothing in cache, wait for the fetch.
// If neither yields a response, return a 404.
<!doctype html>
<style>
div {
height: 40px;
background: red;
}
body {
height: 100vh;
}
section {
@surma
surma / convert.sh
Last active May 12, 2017 21:30
Convert video to gif
#!/bin/bash
FUZZ="0.0% 0.5% 1.0% 1.5% 2.0% 2.5% 3.0%"
COLORS="128 256"
CONV_OPTS="-resize 450 -layers optimize-plus -layers optimize-transparency"
mkdir output
for f in $FUZZ; do
convert +repage -fuzz $f -delay 4 -loop 0 $@ $CONV_OPTS ./output/im-$f.gif
for c in $COLORS; do
<!doctype html>
<style>
html, body {
margin: 0;
background-color: #CCC;
box-sizing: border-box;
}
.item {
margin: 20px;
padding: 20px;
@surma
surma / data.txt
Created March 29, 2016 10:37
Populate Cache from client side
Hello World
{⍵ (({⊃∨/1 ⍵∧3 4=+/,1 0 ¯1∘.⊖1 0 ¯1⌽¨⊂⍵}⍣10000)⍵)} 90{⍵ ⍵⍴(⍳⍵*2)∊⍺?⍵*2}15
@surma
surma / intersectionobserver-polyfill.js
Last active June 17, 2017 17:48
IntersectionObserver
(function() {
// TODO: rootMargin are completely ignored for now
// Constructor
window.IntersectionObserver = function(callback, options) {
options = options || {};
if(!(callback instanceof Function)) {
throw('callback needs to be a function');
}
diff --git a/gulpfile.js b/gulpfile.js
index e027df5..8b8bd35 100644
--- a/gulpfile.js
+++ b/gulpfile.js
@@ -112,7 +112,10 @@ function build() {
entries: ENTRY_FILE,
debug: true
})
- .transform(babelify.configure({presets: ["stage-0","es2015"]}))
+ .transform(babelify.configure({