Skip to content

Instantly share code, notes, and snippets.

View wuhhh's full-sized avatar

Huw Roberts wuhhh

View GitHub Profile
@wuhhh
wuhhh / map.js
Created February 15, 2018 16:35 — forked from AugustMiller/map.js
Extension of the native Number class to map a number in one range to a number in another.
Number.prototype.map = function ( in_min , in_max , out_min , out_max ) {
return ( this - in_min ) * ( out_max - out_min ) / ( in_max - in_min ) + out_min;
}
@wuhhh
wuhhh / wp_rewrite_content_type.php
Created February 19, 2019 15:21
Rewrite a WordPress URL and set its content-type. Use case: Displaying dynamic PDF files which follow a custom URL pattern.
<?php
/**
* Hook into WP init and add custom rewrite rule(s)
*/
add_action('init', 'wuhhh_add_rewrites'), 1, 0);
function wuhhh_add_rewrites() {
// Add a rewrite rule according to your requirements
add_rewrite_rule(
@wuhhh
wuhhh / generics.ts
Created February 21, 2019 11:17
Reminder on TypeScript's Generics syntax
/**
* The generic function loggingIdentity takes a type parameter T, and
* an argument arg which is an array of Ts, and returns an array of Ts.
*/
function loggingIdentity<T>(arg: T[]): T[] {
console.log(arg.length); // Array has a .length, so no more error
return arg;
}
function parseVideo(url) {
// - Supported YouTube URL formats:
// - http://www.youtube.com/watch?v=My2FRPA3Gf8
// - http://youtu.be/My2FRPA3Gf8
// - https://youtube.googleapis.com/v/My2FRPA3Gf8
// - Supported Vimeo URL formats:
// - http://vimeo.com/25451551
// - http://player.vimeo.com/video/25451551
// - Also supports relative URLs:
// - //player.vimeo.com/video/25451551
/**
* From: https://stackoverflow.com/a/23529943
*
* Add or update a query string parameter. If no URI is given, we use the current
* window.location.href value for the URI.
*
* Based on the DOM URL parser described here:
* http://james.padolsey.com/javascript/parsing-urls-with-the-dom/
*
* @param (string) uri Optional: The URI to add or update a parameter in
@wuhhh
wuhhh / outerHeight.js
Created August 21, 2019 08:10
Get element outer height
/**
* Returns the element height including margins
* @param element - element
* @returns {number}
*/
function outerHeight(element) {
const height = element.offsetHeight,
style = window.getComputedStyle(element)
return ['top', 'bottom']
@wuhhh
wuhhh / vue-parallax-plugins.js
Last active January 15, 2020 13:43
A simple parallax plugin for Vue. Add the v-parallax directive to elements you want to... parallaxify. Directive argument should be an object with single argument, 'target'. Target specifies the final offset amount (in px) for the element
/**
* A simple parallax plugin for Vue
* Add the v-parallax directive to elements you want to... parallaxify.
* Directive argument should be an object with single argument, 'target'
* Target specifies the final offset amount (in px) for the element
*
* For example:
*
* <my-element v-parallax="{ target: -500 }">
*/
// Hex to RGB
// e.g. vec3 rgbBlue = hexToRGB(0xD0F5F7);
vec3 hexToRGB(int h) {
float rValue = float(h / 256 / 256);
float gValue = float(h / 256 - int(rValue * 256.0));
float bValue = float(h - int(rValue * 256.0 * 256.0) - int(gValue * 256.0));
return vec3(rValue / 255.0, gValue / 255.0, bValue / 255.0);
}
float map(float value, float low1, float high1, float low2, float high2) {
return low2 + (value - low1) * (high2 - low2) / (high1 - low1);
}
@wuhhh
wuhhh / limit-orbit-pan.jsx
Created June 12, 2023 20:19
Limit OrbitControls pan
/**
* Limit OrbitControls pan
*/
const controls = useRef();
const minPan = new THREE.Vector3(-2, 1, 0);
const maxPan = new THREE.Vector3(2, -1, 0);
useEffect(() => {
const _v = new THREE.Vector3();