Skip to content

Instantly share code, notes, and snippets.

View BretCameron's full-sized avatar
🏠
Working from home

Bret Cameron BretCameron

🏠
Working from home
  • YuLife
  • London
View GitHub Profile
@BretCameron
BretCameron / container.js
Created October 15, 2019 20:46
An example container used for functional programming, to help separate impure functions from pure logic.
const isFunction = fn => fn && Object.prototype.toString.call(fn) === '[object Function]';
const isAsync = fn => fn && Object.prototype.toString.call(fn) === '[object AsyncFunction]';
const isPromise = p => p && Object.prototype.toString.call(p) === '[object Promise]';
const tap = f => x => {
f(x);
return x;
};
@BretCameron
BretCameron / fp.js
Created October 14, 2019 19:08
An example of functional programming in JavaScript, featuring compose, pipe, tap and trace functions
/**
* Performs right-to-left composition, combining multiple functions into a single function.
* @function compose
* @param {...Function} args
* @returns {Function}
*/
const compose = (...fns) => x => fns.reduceRight((output, fn) => fn(output), x, fns);
/**
* Performs left-to-right composition, combining multiple functions into a single function. Sometimes called `sequence`. Right-to-left composition is typically known as `compose`.
@BretCameron
BretCameron / LinkedList.js
Created September 30, 2019 09:16
The full LinkedList implementation from this tutorial: https://bit.ly/2mihZac
class LinkedListNode {
constructor(value, next) {
this.value = value;
this.next = next || null;
}
}
class LinkedList {
constructor(value) {
this.size = 0;
@BretCameron
BretCameron / canvas.js
Created September 27, 2019 14:55
The complete JavaScript file to create "Icy Lines"
const cvs = document.querySelector('canvas');
const c = cvs.getContext('2d');
cvs.width = window.innerWidth;
cvs.height = window.innerHeight;
window.addEventListener('resize', function () {
cvs.width = window.innerWidth;
cvs.height = window.innerHeight;
});
@BretCameron
BretCameron / canvas.js
Created September 27, 2019 14:55
The complete JavaScript file to create "Expanding Diamonds"
const cvs = document.querySelector('canvas');
const c = cvs.getContext('2d');
cvs.width = window.innerWidth;
cvs.height = window.innerHeight;
window.addEventListener('resize', function () {
cvs.width = window.innerWidth;
cvs.height = window.innerHeight;
});
@BretCameron
BretCameron / index.html
Created September 25, 2019 15:22
A generic index page featuring a canvas tag
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Canvas</title>
@BretCameron
BretCameron / canvas.js
Last active October 22, 2021 20:43
Boilerplate code for creating animated, interactive artwork using HTML5 canvas
const cvs = document.querySelector('canvas');
const c = cvs.getContext('2d');
cvs.width = window.innerWidth;
cvs.height = window.innerHeight;
window.addEventListener('resize', function () {
cvs.width = window.innerWidth;
cvs.height = window.innerHeight;
});
@BretCameron
BretCameron / torus-knot.js
Created September 23, 2019 16:39
An example three.js scene
// Import dependencies
import * as THREE from 'three';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
// Create Scene
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x282c34);
// Define a camera, set it to fill the browser window and position it
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 10000);
@BretCameron
BretCameron / parseSTL.js
Created September 13, 2019 09:51
A forked version of THREE.js's STLLoader.js, containing only the parse function
import {
BufferAttribute,
BufferGeometry,
Float32BufferAttribute,
LoaderUtils,
Vector3
} from "three";
export default function parseSTL ( data ) {
@BretCameron
BretCameron / package.json
Last active August 22, 2019 14:11
An example package.json file, for publishing an npm module.
{
"name": "my-npm-package",
"version": "1.0.0",
"description": "An example package for a tutorial.",
"keywords": [
"example",
"package",
"tutorial"
],
"homepage": "https://github.com/UserName/package-name",