Skip to content

Instantly share code, notes, and snippets.

View vincentorback's full-sized avatar
🌻

Vincent Orback vincentorback

🌻
View GitHub Profile
@vincentorback
vincentorback / distanceBetween.js
Last active January 10, 2016 14:06
Get distance between to geolocations in kilometers
var distance = distanceBetween({
lat: 59.329323,
lng: 18.068581
}, {
lat: 57.708870,
lng: 11.974560
});
function getRadius(x) {
return x * Math.PI / 180;
@vincentorback
vincentorback / nodesToArray.js
Created March 20, 2016 12:00
Converting a NodeList to an Array
/**
* Turns a list of nodes into an array
* @param { NodeList } nodeList
* @return { Array }
*/
export function nodesToArray(nodeList) {
return [...nodeList];
return Array.from(nodeList);
return [].slice.call(nodeList);
return Array.prototype.slice.call(nodeList);
@vincentorback
vincentorback / functions.php
Last active May 20, 2016 17:29
Some fun WordPress snippets
<?php
/**
* Check if on posts-page
*/
function is_page_for_posts () {
return ( is_home() || (is_archive() && ! is_post_type_archive() ) );
}
@vincentorback
vincentorback / helpers.js
Last active May 27, 2016 11:39
Lots of small javascript helpers that I tend to reuse in projects
/**
* isUndefined
* Check if object is undefined
* @param {Anything} value
* @return {Boolean} Returns `true` if `value` is `undefined`, else `false`.
*/
export function isUndefined(value) {
return value === undefined;
}
@vincentorback
vincentorback / getClosest.js
Last active June 8, 2016 11:57
Get closest parent element matching selector
/**
* Get closest DOM element up the tree that contains a class, ID, or data attribute
* @param {Node} elem The base element
* @param {String} selector The class, id, data attribute, or tag to look for
* @return {Node} Element or Null if no match
*/
export function getClosest (elem, selector) {
let firstChar = selector.charAt(0);
// Get closest match
var orientationEls = doc.querySelectorAll('.js-orientation');
window.addEventListener('deviceorientation', function(eventData) {
var yTilt = Math.round((-eventData.beta + 90) * (40/180) - 40);
var xTilt = Math.round(-eventData.gamma * (20/180) - 20);
if (xTilt > 0) {
xTilt = -xTilt;
} else if (xTilt < -40) {
xTilt = -(xTilt + 80);
@vincentorback
vincentorback / applyStyle.js
Last active September 6, 2016 11:24
Apply style prefixed with all prefixes
const PREFIXES = ['', 'Moz', 'webkit', 'Webkit', 'O', 'ms'];
function applyStyle(prop, value, element) {
const first = prop[0];
const trail = prop.slice(1);
PREFIXES.forEach(prefix => {
let name = prefix ? (prefix + first.toUpperCase()) : first.toLowerCase();
name += trail;
/* If js doesnt load or just before it does: hide element for 2 seconds. */
.no-js .u-showIfNoJs {
animation: showAfterLong 2000ms;
}
/* Hide when js loads and remove animation */
.js .u-showIfNoJs {
animation: none;
display: none;
}
@vincentorback
vincentorback / randomBetween.js
Last active January 8, 2017 11:30
randomBetween.js
/**
* Get a random int between two values
* @param {Int} min
* @param {Int} max
* @return {Int}
*/
export function randomBetween(min = 0, max = 0) {
return Math.floor(Math.random() * (max - min + 1) + min)
}
@vincentorback
vincentorback / getMousePosition.js
Last active January 8, 2017 11:33
getMousePosition.js
export function getMousePosition (e) {
let posX = 0
let posY = 0
if (!e) {
e = window.event
}
if (e.pageX || e.pageY) {
posX = e.pageX