Skip to content

Instantly share code, notes, and snippets.

View av01d's full-sized avatar

Arjan Haverkamp av01d

View GitHub Profile
@av01d
av01d / Point2PointDistance.js
Created February 21, 2024 08:30
Javascript Point2PointDistance: Caculate distance (in m) between 2 lat/lng points
const Point2PointDistance = (lat1, lon1, lat2, lon2) => {
const toRad = num => num * Math.PI / 180; // Converts numeric degrees to radians
const R = 6371000, // earth radius, in m
dLat = toRad(lat2 - lat1),
dLon = toRad(lon2 - lon1);
lat1 = toRad(lat1);
lat2 = toRad(lat2);
const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
@av01d
av01d / keypress.js
Created March 29, 2024 09:51
Javascript keypress handler
const commands = {
'ctrl+z' => History.undo,
'ctrl+y' => History.redo,
'escape': () => { alert('esc'); }
};
$(window).on('keydown', event => {
let label = '';
if (event.ctrlKey) { label += 'ctrl+'; }
if (event.altKey) { label += 'alt+'; }
@av01d
av01d / tiny-jquery.js
Last active May 27, 2024 13:43
Javascript: tiny jQuery
// Select single element
const $ = (e, parent = document) => parent.querySelector(e);
// Select multiple elements
const $$ = (e, parent = document) => Array.from(parent.querySelectorAll(e));
// Create element
const cE = (tagName, props) => {
const el = document.createElement(tagName);
for (let prop of ['id','name','value','className','type','href','method','action']) {
@av01d
av01d / loadFonts.js
Last active March 12, 2025 13:03
Javascript: Load fonts concurrently, via `new FontFace`
window.addEventListener('DOMContentLoaded', async () => {
const fonts = ['Action Man', 'Heaven', 'Weltron Urban']
// First method (if you don't need to know when all fonts are finished loading)
fonts.forEach(font => {
new FontFace(font, `url(fonts/${encodeURIComponent(font)}.woff)`).load().then(font => { document.fonts.add(font) })
})
// Second method (if you do need to know when all fonts are finished loading)
const promises = []