Skip to content

Instantly share code, notes, and snippets.

View vincentorback's full-sized avatar
🌻

Vincent Orback vincentorback

🌻
View GitHub Profile
@vincentorback
vincentorback / debounce-es2015.js
Last active September 28, 2023 16:24
Smarter debouncing
export function debounce (fn, wait = 1) {
let timeout
return function (...args) {
clearTimeout(timeout)
timeout = setTimeout(() => fn.call(this, ...args), wait)
}
}
@vincentorback
vincentorback / imageBrightness.js
Last active March 8, 2024 18:15
Get Image Brightness
function getImageBrightness(imageSrc, callback) {
var img = document.createElement('img'),
colorSum = 0,
i = 0,
len,
canvas,
ctx,
imageData,
data,
brightness,
@vincentorback
vincentorback / just-write
Last active May 8, 2017 12:30
Simple writing in the browser
Paste this into your browser:
data:text/html,<html><head><title>Just write!</title><style>*{margin: 0 padding: 0}body{width: 90%; max-width: 650px; margin: 5% auto;}@media (min-width:800px){body{font-size: 2em;}}</style><script>var body=document.body if(localStorage.justWrite) body.innerHTML=localStorage.justWrite} function downloadInnerHtml(filename,mimeType){var elHtml=body.innerHTML var link=document.createElement('a') mimeType=mimeType||'text/plain' filename=filename||'document.txt' link.setAttribute('download',filename) link.setAttribute('href','data:'+mimeType+'charset=utf-8,'+encodeURIComponent(elHtml)) link.click()} window.onkeydown=function(event){if((event.key===83)&&(event.metaKey||event.ctrlKey)){downloadInnerHtml() e.preventDefault() return false} localStorage.justWrite=body.innerHTML}</script></head><body contenteditable></body></html>
<!--
TODO:
- CMD + S to save file. ✓
- Auto focus on input
- Choose file extension .md, .txt, .doc
@vincentorback
vincentorback / readingTime.js
Last active February 14, 2019 14:05
Reading time / Words per minute
/**
* Get reading time
* @param {String} text The text string to analyze
* @return {Int} Reading time in milliseconds
*/
function getReadingTime(text) {
var totalWords,
wordsPerMillisecond,
totalReadingTime,
wordsPerMinute = 250,
@vincentorback
vincentorback / getWeekNumber.js
Last active August 9, 2023 08:34
getWeekNumber.js
function getWeekNumber(d) {
d = new Date(+d) // Copy date so don't modify original.
d.setHours(0, 0, 0, 0) // Reset hours.
d.setDate(d.getDate() + 4 - (d.getDay() || 7)) // Set to nearest Thursday: current date + 4 - current day number and make Sunday's day number 7
var yearStart = new Date(d.getFullYear(), 0, 1) // Get first day of year
var weekNo = Math.ceil((((d - yearStart) / 86400000) + 1) / 7) // Calculate full weeks to nearest Thursday
return weekNo // Return week number
}
var date = new Date()
@vincentorback
vincentorback / stringToElements.js
Last active April 13, 2020 18:39
Creates DOM-elements from a string
function stringToElements (string) {
const div = document.createElement('div')
div.innerHTML = string
if (div.childNodes.length > 1) {
return div.childNodes
}
return div.firstChild
}
@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;
@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 / 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 / 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;
}