Skip to content

Instantly share code, notes, and snippets.

@Daniel-Hug
Daniel-Hug / collision-detection.js
Last active March 3, 2024 04:02
JS functions: check if 2 rectangles intersect, are touching, or if one contains the other
// Check if rectangle a contains rectangle b
// Each object (a and b) should have 2 properties to represent the
// top-left corner (x1, y1) and 2 for the bottom-right corner (x2, y2).
function contains(a, b) {
return !(
b.x1 < a.x1 ||
b.y1 < a.y1 ||
b.x2 > a.x2 ||
b.y2 > a.y2
);
@Daniel-Hug
Daniel-Hug / arr-stat.js
Last active December 11, 2023 14:46
JavaScript statistical functions for arrays: max, min, range, midrange, sum, mean / average, median, modes, variance, standard deviation, mean absolute deviation, z scores
var arr = {
max: function(array) {
return Math.max.apply(null, array);
},
min: function(array) {
return Math.min.apply(null, array);
},
range: function(array) {
@Daniel-Hug
Daniel-Hug / dabblet.css
Created January 14, 2012 02:22
A "deeper" indented text effect with the :before and :after pseudo-elements.
/**
* A "deeper" indented text effect with the :before and :after pseudo-elements.
*/
html, body {
height: 100%;
}
body {
margin: 0;
@Daniel-Hug
Daniel-Hug / get-text-nodes.js
Last active April 7, 2022 20:09
Get the text nodes which are descendants of the passed parent
// Get the text nodes which are descendants of the passed parent
function getTextNodes(parent){
var all = [];
for (parent = parent.firstChild; parent; parent = parent.nextSibling) {
if (['SCRIPT','STYLE'].indexOf(parent.tagName) >= 0) continue;
if (parent.nodeType === Node.TEXT_NODE) all.push(parent);
else all = all.concat(getTextNodes(parent));
}
return all;
}
@Daniel-Hug
Daniel-Hug / change-tag.js
Last active November 12, 2021 02:42
JS function: replace tag name but keep element contents. Source: http://stackoverflow.com/a/15086834/552067
// Replace tag name but keep element contents
function changeTag(el, newTagName, keepAttributes) {
var newEl = document.createElement(newTagName);
// Copy the children
while (el.firstChild) {
newEl.appendChild(el.firstChild); // *Moves* the child
}
// Copy the attributes
@Daniel-Hug
Daniel-Hug / probabilities.js
Last active October 5, 2020 18:28
JS probability functions
// probability that the event with the passed probability will NOT occur
function complement(p) {
return 1 - p;
}
// probability that a and b will happen when neither outcome
// is affected by the other (accepts 1 or more arguments)
function intersectionOfIndependentEvents(a, b) {
var ret = a;
for (var i = 1; i < arguments.length; i++) {
@Daniel-Hug
Daniel-Hug / delegate-event.js
Last active September 14, 2020 05:05
Vanilla JS equivalent of jQuery's .live(): use event delegation to handle events whose target matches a selector, closest(): get nearest parent element matching selector
// get nearest parent element matching selector
var closest = (function() {
var el = HTMLElement.prototype;
var matches = el.matches || el.webkitMatchesSelector || el.mozMatchesSelector || el.msMatchesSelector;
return function closest(el, selector) {
return matches.call(el, selector) ? el : closest(el.parentElement, selector);
};
})();
@Daniel-Hug
Daniel-Hug / dabblet.css
Created May 4, 2012 01:19
Pure CSS aspect ratio with no spacer images or js! :)
/*
* Pure CSS aspect ratio with no spacer images or js! :)
*/
body {
width: 36%;
margin: 8px auto;
}
div.stretchy-wrapper {
@Daniel-Hug
Daniel-Hug / dabblet.css
Created March 28, 2012 06:49
Pure CSS stopwatch
/**
* Pure CSS stopwatch
*/
* { box-sizing: border-box; -moz-box-sizing: border-box }
html { background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAQAAAAECAYAAACp8Z5+AAAAOklEQVQIWx3K2QkAMAgFwadEsBjF/ksTD0Kyn8uQme05BzOD3QW5+2YmVBWvP6oKIoLuBkXEF48zMy6Jfxr7Roi85AAAAABJRU5ErkJggg==) }
body {
text-align: center;
@Daniel-Hug
Daniel-Hug / unwrap.js
Last active June 16, 2019 07:22
JS: unwrap element (remove parent without removing child nodes). Demo: http://jsbin.com/hipahu/edit?html,js,output
// remove parent without removing childen
function unwrap(wrapper) {
// place childNodes in document fragment
var docFrag = document.createDocumentFragment();
while (wrapper.firstChild) {
var child = wrapper.removeChild(wrapper.firstChild);
docFrag.appendChild(child);
}
// replace wrapper with document fragment