Skip to content

Instantly share code, notes, and snippets.

View JoostKiens's full-sized avatar

Joost Kiens JoostKiens

View GitHub Profile
@JoostKiens
JoostKiens / localStorageUtils.js
Last active February 11, 2024 22:34
Simple AMD module with basic localStorage utilities
define(function () {
'use strict';
function removeItem(itemKey) {
localStorage.removeItem(itemKey);
}
function getItem(itemKey) {
return JSON.parse(localStorage.getItem(itemKey));
@JoostKiens
JoostKiens / test2.json
Last active October 10, 2023 17:45
Flypath of Black-tailed godwit, last 2 years, from https://www.globalflywaynetwork.org/
[
{
"from": {
"pos": [
153.43916,
-29.50063
],
"time": "2021-10-03 07:26:23"
},
"to": {
@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 / 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 / useScroll.jsx
Created June 13, 2019 16:14
React useScroll hook (window scrolling) returns scrollX & scrollY, with context provider
import React from 'react'
const ScrollContext = React.createContext(null)
export function useScroll() {
const scroll = React.useContext(ScrollContext)
if (!scroll)
throw new Error('Please make sure ScrollContextProvider is available')
return scroll
@JoostKiens
JoostKiens / useCookies.jsx
Created June 13, 2019 16:12
React useCookies hook with context provider
import React from 'react'
import Cookies from 'js-cookie'
const CookiesContext = React.createContext(null)
export function useCookies() {
const cookies = React.useContext(CookiesContext)
if (!cookies)
throw new Error('Please make sure CookiesContextProvider is available')
return cookies
@JoostKiens
JoostKiens / functions.php
Last active April 22, 2019 09:58
Improve the WordPress caption shortcode with HTML5 figure & figcaption, microdata & wai-aria attributes
<?php
/**
* Improves the WordPress caption shortcode with HTML5 figure & figcaption, microdata & wai-aria attributes
*
* Author: @joostkiens
* Licensed under the MIT license
*
* @param string $val Empty
* @param array $attr Shortcode attributes
* @param string $content Shortcode content
@JoostKiens
JoostKiens / createUVWGrid.js
Created January 17, 2019 08:47
Create a 3D grid in uvw coordinates for THREE.js (pure function, no loops)
const createUVWGrid = ([cx, cy, cz]) => {
return times(cx).map(x => times(cy).map((y) => times(cz).map(z =>
new THREE.Vector3(
cx <= 1 ? 0.5 : x / (cx - 1),
cy <= 1 ? 0.5 : y / (cy - 1),
cz <= 1 ? 0.5 : z / (cz - 1)
)
))).flat(2)
}
@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
function autoplayVideoSupported(muted = true, playsinline = true) {
const v = document.createElement('video')
v.muted = muted
playsinline && v.setAttribute('playsinline', 'playsinline')
v.setAttribute('width', 0)
v.setAttribute('height', 0)
v.style.opacity = 0
document.querySelector('body').appendChild(v)
return new Promise((resolve, reject) => {
v.play()