Skip to content

Instantly share code, notes, and snippets.

@rhysburnie
rhysburnie / node-or-browser.js
Last active September 21, 2023 16:13
Detect node or browser
// determine if in-browser or using node.js
// thruthy
var _nodejs = (
typeof process !== 'undefined' && process.versions && process.versions.node);
if (_nodejs) {
_nodejs = {
version: process.versions.node
};
}
@rhysburnie
rhysburnie / useHasSlot.js
Last active January 25, 2023 13:43
vue3 determine if slot is being used (has content)
import { useSlots } from 'vue';
export default function useHasSlot() {
const slots = useSlots();
return function hasSlot(name) {
return slots[name] && !isEmptySlot(slots[name]());
};
}
function isEmptySlot(items) {
@rhysburnie
rhysburnie / ActiveElementObserver.js
Last active April 26, 2022 07:16
Simple document.activeElement observation
export default function ActiveElementObserver (handler = function () {}) {
let prev;
let raf;
this.observe = _ => {
cancelAnimationFrame(raf);
raf = undefined;
if (document.activeElement !== prev) {
handler({ activeElement: document.activeElement, prev });
prev = document.activeElement;
@rhysburnie
rhysburnie / jquery-ajax-dataFilter.js
Last active January 17, 2022 22:02
jQuery ajax dataFilter to remove scripts from html string to prevent script execution if loading string into a temp dom element
function dfStripScripts = function(data, type)
{
// incase the response is full html with scripts remove them
type = type || 'text';
if(type=='html'||type=='text'){
/*return data.replace(/<script.*>.*?<\/script>/gi, '');*/
return data.replace(/<script.*?>([\w\W\d\D\s\S\0\n\f\r\t\v\b\B]*?)<\/script>/gi, '');
}
return data;
};
@rhysburnie
rhysburnie / Looper.js
Last active December 1, 2017 10:23
Small frame looper for quick setup
function Looper(update, targetFPS = 30, autoplay = true) {
let updating = false;
this.autoplay = autoplay;
this.targetFPS = targetFPS;
const advanceFrame = () => {
updating = true;
const next = () => {
const delay = this.autoplay ? 1000 / (this.targetFPS || 30) : 0;
setTimeout(() => updating = false, delay);
@rhysburnie
rhysburnie / dat.GUI.convertToDisplay.js
Created October 29, 2017 03:41
dat.GUI convert a field to a display instead of input
dat.GUI.prototype.convertToDisplay = function(owner, property) {
var field = this.__controllers.find(function(item){return item.property === property});
var parent = field.domElement.parentElement;
var value = owner[property];
delete owner[property];
Object.defineProperty(owner, property, {
get: function() {
return value;
},
set: function(newValue) {
function transformsSupport() {
if (!window.getComputedStyle) {
return false;
}
var el = document.createElement('p'),
has3d,
transformPropName,
transforms = {
webkitTransform: '-webkit-transform',
@rhysburnie
rhysburnie / index.js
Last active September 25, 2017 05:34
iso8601-duration conversions - requirebin sketch
// Welcome! require() some modules from npm (like you were using browserify)
// and then hit Run Code to run your code on the right side.
// Modules get downloaded from browserify-cdn and bundled in your browser.
var iso8601Duration = require('iso8601-duration');
// https://github.com/wking/milliseconds-to-iso-8601-duration/blob/master/milliseconds-to-iso-8601-duration.js
var millisecondsToISO8601Duration = function(milliseconds) {
if (milliseconds == 0) {
return 'P0D';
@rhysburnie
rhysburnie / console-utilities.js
Last active September 19, 2017 01:06
Suppress certain console.errors
/* eslint-disable no-console */
const originalConsoleError = console.error;
export function restoreConsoleError() {
console.error = originalConsoleError;
}
export function createSuppressedConsoleError(ignoreThese = []) {
if (console.error === originalConsoleError) {
console.error = (...args) => {
@rhysburnie
rhysburnie / prototyping-tools.js
Last active September 18, 2017 02:02
Personal setup Stats, dat.GUI and colors reference for dirty clientside prototyping
/**
* @author Rhys Burnie
* Personal setup for Stats and dat.GUI with a materialColors object for quick nice color options.
* (clientside - dirty) - for things like p5 sketches, codepens etc.
*
* promiseTools().then(function(tools){
* var colors = tools.colors;
* var stats = tools.stats;
* var gui = tools.gui;
*