Skip to content

Instantly share code, notes, and snippets.

View callumlocke's full-sized avatar

Callum Locke callumlocke

View GitHub Profile
@callumlocke
callumlocke / scale-canvas.ts
Last active April 12, 2024 03:25
How to fix a canvas so it will look good on retina/high-DPI screens.
/*
UPDATED for 2023 - Now much simpler. The old tricks are no longer needed.
The following code makes an 800×600 canvas that is always as sharp as possible for the device.
You still draw on it as if it's the logical size (800×600 in this case), but everything just
looks sharper on high-DPI screens. Regular non-sharp screens are not affected.
*/
const width = 800
@callumlocke
callumlocke / .zshrc
Last active March 24, 2024 08:12
ZSH function to auto-switch to correct Node version
####
# ZSH function to auto-switch to correct Node version
# https://gist.github.com/callumlocke/30990e247e52ab6ac1aa98e5f0e5bbf5
#
# - Searches up your directory tree for the closest .nvmrc, just like `nvm use` does.
#
# - If you are already on the right Node version, IT DOES NOTHING, AND PRINTS NOTHING.
#
# - Works correctly if your .nvmrc file contains something relaxed/generic,
# like "4" or "v12.0" or "stable".
@callumlocke
callumlocke / rgb-colour-to-number.js
Created July 21, 2015 13:56
RGB colour to single decimal number
// convert three r,g,b integers (each 0-255) to a single decimal integer (something between 0 and ~16m)
function colourToNumber(r, g, b) {
return (r << 16) + (g << 8) + (b);
}
// convert it back again (to a string)
function numberToColour(number) {
const r = (number & 0xff0000) >> 16;
const g = (number & 0x00ff00) >> 8;
@callumlocke
callumlocke / fast-bash-server-command.md
Last active April 4, 2022 15:37
Bash function to run a simple static file server from the current directory, and open it in your browser.

To set up:

  1. Clone this repo. (Or just copy serve.go into a folder on your computer).
  2. cd into it, and run go build serve.go

You can now run the binary like this: ./serve --root="/path/to/directory"

But to make it easier, put this in your .bashrc:

@callumlocke
callumlocke / material-design-shadows.css
Created March 25, 2021 14:10 — forked from serg0x/material-design-shadows.css
Google material design elevation system shadows as css. Based on https://material.io/design/environment/elevation.html#default-elevations Exported with Sketchapp from the Google material design theme editor plugin "Baseline" theme.
/* Shadow 0dp */
box-shadow: none;
/* Shadow 1dp */
box-shadow: 0 1px 1px 0 rgba(0,0,0,0.14), 0 2px 1px -1px rgba(0,0,0,0.12), 0 1px 3px 0 rgba(0,0,0,0.20);
/* Shadow 2dp */
box-shadow: 0 2px 2px 0 rgba(0,0,0,0.14), 0 3px 1px -2px rgba(0,0,0,0.12), 0 1px 5px 0 rgba(0,0,0,0.20);
/* Shadow 3dp */
@callumlocke
callumlocke / README.md
Last active February 23, 2021 13:02
How to run TypeScript CLI tasks in a Next.js app

Assuming you've got a Next.js app that's already using TypeScript to render pages/components or handle API routes, but you also want to be able to run ad hoc build tasks written in TypeScript (perhaps pulling in type files used by the app)...

  1. Add these deps: npm install -D @babel/core @babel/node
  2. Make sure your Next app has an explicit .babelrc file:
// Default Next.js .babelrc (it's OK if you also have extra stuff in it)
{
  "presets": ["next/babel"],
 "plugins": []
@callumlocke
callumlocke / machine.js
Last active March 9, 2020 18:23
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
@callumlocke
callumlocke / index.html
Created November 9, 2019 21:02
Electron Fiddle Gist
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
<!-- All of the Node.js APIs are available in this renderer process. -->
We are using Node.js <script>document.write(process.versions.node)</script>,
@callumlocke
callumlocke / README.md
Created October 26, 2015 14:38
Importing individual lodash-es functions
import chunk from 'lodash-es/array/chunk';
import zipObject from 'lodash-es/array/zipObject';

console.log(zipObject(chunk(['a', 'b', 'c', 'd'], 2)));
$ rollup -f=iife demo.js > output.js