Skip to content

Instantly share code, notes, and snippets.

View callumlocke's full-sized avatar

Callum Locke callumlocke

View GitHub Profile
@callumlocke
callumlocke / format-date.js
Created September 11, 2015 12:39
Format a date object in this style: "July 21, 2014"
var monthNames = [
'January', 'February', 'March', 'April', 'May', 'June', 'July',
'August', 'September', 'October', 'November', 'December'
];
function formatDate(date) {
return (
monthNames[date.getUTCMonth()] + ' ' +
date.getUTCDate() + ', ' +
date.getUTCFullYear()
<link rel="import" href="../ace-element/ace-element.html">
<link rel="import" href="../topeka-elements/category-images.html">
<link rel="import" href="../core-icon/core-icon.html">
<link rel="import" href="../core-icons/core-icons.html">
<link rel="import" href="../core-icons/av-icons.html">
<link rel="import" href="../paper-fab/paper-fab.html">
<link rel="import" href="../topeka-elements/category-icons.html">
<polymer-element name="my-element">
@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 / 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 / README.md
Last active August 29, 2015 14:25 — forked from mbostock/.block
@callumlocke
callumlocke / is-simple-click.js
Last active August 29, 2015 14:23
Find out whether a click is really a click.
"use strict";
module.exports = function isSimpleClick(event) {
var isLeft = true;
var which = event.which;
if (!which && event.button != null) {
which = event.button & 1 ? 1 : event.button & 2 ? 3 : event.button & 4 ? 2 : 0;
}
@callumlocke
callumlocke / gist:2c4675abe6c364f0bcb7
Last active August 29, 2015 14:18
Bundle up an npm module to paste inline into Node.js code
# make sure you have these installed:
npm install --global browserify uglify-js
# example command for 'chalk':
echo "var chalk = (function(){var _ref;" `echo "_ref = require('chalk');" | browserify --bare - | uglifyjs` "return _ref;}).call(this);" > bundled.js
@callumlocke
callumlocke / README.md
Last active August 29, 2015 14:17 — forked from mbostock/.block
@callumlocke
callumlocke / time-require-calls.js
Created February 19, 2014 09:51
cd into a Node.js project, run node (CLI) and paste this in to time require calls
var modules = require('fs').readdirSync('node_modules').filter(function (name) {return name !== '.bin'});
modules.forEach(function (m) {
console.time(m);
require(m);
console.timeEnd(m);
});
@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: