Skip to content

Instantly share code, notes, and snippets.

View JoostKiens's full-sized avatar

Joost Kiens JoostKiens

View GitHub Profile
@JoostKiens
JoostKiens / jquery.toggleHtml.js
Created July 10, 2014 07:48
Advanced jQuery plugin to switch between one piece of html and another
/**
* Copyright (C) 2014 Joost Kiens
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
@JoostKiens
JoostKiens / index.html
Last active November 7, 2016 14:29
Checkboxes with css, no js
<div>
<input type="checkbox" id="floep">
<label for="floep">Floep</label>
</div>
<div>
<input type="checkbox" id="flap" checked="checked">
<label for="flap">Flap</label>
</div>
@JoostKiens
JoostKiens / iBeaconCalculateDistance.js
Last active October 26, 2022 17:40
iBeacon calculate distance in meters
// Based on http://stackoverflow.com/a/20434019
function calculateAccuracy(txPower, rssi) {
if (rssi === 0) {
return -1; // if we cannot determine accuracy, return -1.
}
var ratio = rssi * 1 / txPower;
if (ratio < 1.0) {
return Math.pow(ratio, 10);
@JoostKiens
JoostKiens / convertImageToGrayscale.js
Created February 19, 2015 12:50
Convert an image to grayscale using canvas, takes around 25 ms for an image of 500px x 400px
define(function () {
'use strict';
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
return function ($srcImg) {
var deferred = $.Deferred();
var srcImg = $srcImg[0];
@JoostKiens
JoostKiens / MakeReactComponent
Last active August 23, 2017 08:15
Make a React component.
#!/bin/bash -e
ORIGINAL_WORKING_DIR=$(pwd)
WORKSPACE=$(cd $(dirname $0); pwd)
# A POSIX variable
OPTIND=1 # Reset in case getopts has been used previously in the shell.
# Initialize our own variables:
NAME=""
@JoostKiens
JoostKiens / numberTransition.js
Created August 20, 2017 10:07
Transition numbers over time with easing
/*
Options
---
{
from: 1, // <Number> start number, default: 0
to: 100, // <Number> end number, required
duration: 1000, // <Number> total duration on transition, required
step: console.log, // <Function> function to execute on each update, receives current number as argument, required
ease: x => x, // <Function> custom easing function, default: linear
tick: fn => window.setTimeout(fn, 20) // <Function> ticker, default: requestAnimationFrame
@JoostKiens
JoostKiens / convertSVGCircleToPathDescription.js
Created August 23, 2017 08:14
Convert SVG circle arguments to path description
function createPathDFromCircle(cx, cy, r) {
return `d="
M ${cx} ${cy}
m -${r}, 0
a ${r},${r} 0 1,0 ${(r * 2)},0
a ${r},${r} 0 1,0 ${-(r * 2)},0
"`
}
function createPathDFromCircleStartOnTop(cx, cy, r) {
@JoostKiens
JoostKiens / customSelectArrow.css
Last active August 23, 2017 12:47
Remove default browser select arrow, replace with background image
select {
-moz-appearance: none;
-webkit-appearance: none;
appearance: none;
background-image: url('data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTUiIGhlaWdodD0iMTAiIHZpZXdCb3g9IjAgMCAxNSAxMCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4NCiAgPHBhdGggZD0iTTEyLjUgMi41bC00LjkgNS01LjEtNSIgc3Ryb2tlLXdpZHRoPSI0IiBzdHJva2U9IiMyMTFEMUUiIGZpbGw9Im5vbmUiIHN0cm9rZS1saW5lY2FwPSJyb3VuZCIgc3Ryb2tlLWxpbmVqb2luPSJyb3VuZCIvPg0KPC9zdmc+');
background-position: calc(100% - 10px) center;
background-repeat: no-repeat;
padding-left: 10px;
padding-right: 20px; /* prevent text from going under background image */
text-overflow: ellipsis;
@JoostKiens
JoostKiens / preventPullDownRefresh.js
Last active October 6, 2019 00:20
Prevent pull down refresh on Chrome browser on iOS
const target = window
let lastY = 0
target.addEventListener('touchmove', handleTouchMove)
function handleTouchMove(e) {
const { pageY } = e.changedTouches[0]
const scrollY = target.pageYOffset || target.scrollTop || 0
if (pageY > lastY && scrollY === 0) {
e.preventDefault()
@JoostKiens
JoostKiens / preloadVideo.js
Last active October 9, 2018 08:15
Preload entire video using fetch, returns promise
// Preloads entire video, returns promise
// NOTE: if a video doesn't exist the fetch will resolve,
// but with a Blob type of `plain/text`
function preloadVideo(src) {
return new Promise((resolve, reject) => {
fetch(src)
.then(response => response.blob())
.then(blob =>
/^video\/\w*/.test(blob.type)
? resolve(URL.createObjectURL(blob))