Skip to content

Instantly share code, notes, and snippets.

View alexreardon's full-sized avatar

Alex Reardon alexreardon

View GitHub Profile
export {};
const allCollectedFps: number[][] = [];
function sum(values: number[]) {
return values.reduce((acc, current) => acc + current, 0);
}
function average(values: number[]): number {
if (!values.length) {
function average(values: number[]): number {
const sum = values.reduce((acc, current) => acc + current, 0);
return sum / values.length;
}
export function standardDeviation(values: number[]): number {
/** https://www.mathsisfun.com/data/standard-deviation-formulas.html
* 1. Work out the Mean (the simple average of the numbers)
* 2. Then for each number: subtract the Mean and square the result
* 3. Then work out the mean of those squared differences.
@alexreardon
alexreardon / wait-for-frame.ts
Created March 21, 2023 00:19
a function that lets you wait for animation frames to be called before executing a function
function waitForFrames({
frames,
done,
}: {
frames: number;
done: () => void;
}): () => void {
let frameId: number | null = null;
let remainingFrames = frames;
@alexreardon
alexreardon / fix-post-drop-pointer-bug.ts
Last active April 12, 2024 16:03
Fix Chrome + Safari bug where the element under where the user started dragging (on the viewport) is entered into by the browser
import { bindAll } from 'bind-event-listener';
import type { DragLocation } from '../internal-types';
type CleanupFn = () => void;
/** Set a `style` property on a `HTMLElement`
*
* @returns a `cleanup` function to restore the `style` property to it's original state
*/
@alexreardon
alexreardon / how-this-works.md
Created August 7, 2022 00:29
How `this` works in javascript

this binding

  • this is the runtime context of a function.
  • this is determined by the call site
  • the same function can be executed with different this runtime contexts. You can think of this as another arguement to the function
  • Comparison: scopes are generally defined at compile time (exception: eval)
const person = {
 name: 'Alex',
@alexreardon
alexreardon / alternative-flex-naming.md
Last active May 23, 2024 05:53
Alternative flex naming

flex container

  • flex-directionmain-axis (row [horizontal] → or column [vertical] ↓)
  • justify-contentmain-axis-align
  • align-itemscross-axis-align
  • align-contentcross-axis-align-wrapped

flex child

  • align-selfcross-axis-align-override
@alexreardon
alexreardon / entering-and-leaving-the-window.ts
Last active April 7, 2026 22:45
Detecting when entering or leaving a window
import { bindAll } from 'bind-event-listener';
// *Usually* to detect if you are entering / leaving a window you can
// use the `event.relatedTarget` property:
// "dragenter":
// - `event.relatedTarget` should point to the element that you are coming from
// - Scenario: A -> B
// - `event.target`: `B` (entering B)
// - `event.relatedTarget`: `A`: (leaving A - where you are coming from)
@alexreardon
alexreardon / drag-and-drop-notes.md
Last active December 16, 2025 12:14
An explanation of the timing of drag and drop events

Drag and drop

This is a collection of knowledge I have built up regarding browser powered drag and drop functionality

Events

dragstart

  • timing: once as drag is starting
  • event.target: draggable Element
@alexreardon
alexreardon / typescript.md
Last active July 13, 2022 00:56
TypeScript notes

Learning typescript

This work is not exclusively my own. There are sections copy pasted from elsewhere

Resources

Docs 📖

@alexreardon
alexreardon / machine.js
Last active May 31, 2021 03:57
Generated by XState Viz: https://xstate.js.org/viz
const machine = Machine({
id: 'save-button',
initial: 'hiding',
states: {
hiding: {
on: {
SHOW: 'waiting',
},
},
waiting: {