Skip to content

Instantly share code, notes, and snippets.

View carlhannes's full-sized avatar

Hannes W carlhannes

View GitHub Profile
debounce: function(func, wait, immediate) {
// Returns a function, that, as long as it continues to be invoked, will not
// be triggered. The function will be called after it stops being called for
// N milliseconds. If `immediate` is passed, trigger the function on the
// leading edge, instead of the trailing.
// taken from https://github.com/jashkenas/underscore/blob/master/underscore.js
var timeout, args, context, timestamp, result;
var later = function() {
var last = new Date().getTime() - timestamp;
@carlhannes
carlhannes / simpe-parallax.js
Last active October 21, 2017 21:59
Very simple ES6 JavaScript background-position parallax function using requestAnimationFrame
export default function attachParallax(elm) {
document.addEventListener('mousemove', (e) => {
window.requestAnimationFrame(() => {
const left = (e.clientX / window.innerWidth) * 5;
const top = (e.clientY / (window.innerHeight)) * 5;
elm.style.backgroundPosition = `${47.5 + left}% ${47.5 + top}%`;
});
}, false);
}
@carlhannes
carlhannes / Creating picassojs extensions - Links.md
Last active May 25, 2019 18:39
Creating picassojs extensions
let versions = [];
(await (await fetch('https://unpkg.com/browse/picasso.js/')).text()).replace(/<option[^>]*value="([^"]*)"[^>]*>/gm, (match, version) => versions.push(version) );
Region Type Count
Stockholm Petrol 1288
Stockholm Diesel 1962
Stockholm Hybrids & Flexifuels 688
Malmo Petrol 495
Malmo Diesel 458
Malmo Hybrids & Flexifuels 116
Gothenburg Petrol 635
Gothenburg Diesel 439
Gothenburg Hybrids & Flexifuels 250
function JSON2Matrix(jsonArray) {
let matrixHeader = [];
let matrixData = [];
for (let idx = 0; idx < jsonArray.length; idx++) {
const cur = jsonArray[idx];
const keys = Object.keys(cur);
let matrixLine = [];
for (let idk = 0; idk < keys.length; idk++) {
Add-WindowsCapability -Online -Name OpenSSH.Server*
Start-Service sshd
Set-Service -Name sshd -StartupType 'Automatic'
@carlhannes
carlhannes / json-path-resolver.js
Last active February 23, 2022 12:53
JSON Path Resolver in JavaScript
/**
* Resolves the value at the given JSON path
*
* @param {String} path Path with / for children
* @param {Object} obj Object to use when searching
* @return {Object} Result at the end of the path
*
* @example
* let path = "/path/to/paradise";
* let obj = {
@carlhannes
carlhannes / picassorenderer.jsx
Last active May 11, 2022 07:04
React picasso.js rendering area
import React, { useEffect } from 'react';
import picasso from 'picasso.js';
const debounce = (func, wait, immediate = false) => {
let timeout;
return function debouncedFn(...args) {
return new Promise((resolve) => {
clearTimeout(timeout);
timeout = setTimeout(() => {
@carlhannes
carlhannes / batch-promise.js
Last active July 12, 2022 06:18
Batch promises in javascript
/**
* Same as Promise.all(items.map(item => task(item))), but it waits for
* the first {batchSize} promises to finish before starting the next batch.
*
* Usage:
* const results = await batchPromise(STUFF_TO_DO, 5, async (ITEM_TO_DO) => {
* console.log('Fetching', ITEM_TO_DO.url);
* return await fetch(ITEM_TO_DO.url);
* });
*