Skip to content

Instantly share code, notes, and snippets.

View ChrisDobby's full-sized avatar

Chris Dobson ChrisDobby

View GitHub Profile
@ChrisDobby
ChrisDobby / canvasDraw.jsx
Last active March 9, 2023 09:23
React component to redraw a canvas when resized
import React from "react";
const scaleWidth = 500;
const scaleHeight = 500;
function draw(canvas, scaleX, scaleY) {
const context = canvas.getContext("2d");
context.scale(scaleX, scaleY);
context.clearRect(0, 0, canvas.clientWidth, canvas.clientHeight);
@ChrisDobby
ChrisDobby / widthAndHeight.jsx
Created July 28, 2019 13:06
React component to display the width and height of the window
import React from "react";
function WidthAndHeight() {
const [width, setWidth] = React.useState(window.innerWidth);
const [height, setHeight] = React.useState(window.innerHeight);
React.useEffect(() => {
window.addEventListener("resize", updateWidthAndHeight);
return () => window.removeEventListener("resize", updateWidthAndHeight);
});
@ChrisDobby
ChrisDobby / lighthouse.js
Created January 23, 2019 11:55
Runs lighthouse against a web application - successful if everything scores 0.9 or above
require('colors');
const lighthouse = require('lighthouse');
const chromeLauncher = require('chrome-launcher');
const launchAndRun = (url, opts, config = null) =>
chromeLauncher.launch({ chromeFlags: opts.chromeFlags })
.then(chrome => {
opts.port = chrome.port;
return lighthouse(url, opts, config).then(results => chrome.kill().then(() => results.lhr));
});
@ChrisDobby
ChrisDobby / classNameFunc.js
Last active June 6, 2018 10:18
Creates a css class on the fly from an object
const ClassName = () => {
const createdClasses = [];
const stringToCss = str => str.split('').map(c => c.charCodeAt(0) >= 97
? c
: `-${String.fromCharCode(c.charCodeAt(0) + 32)}`).join('');
const cssString = css => Object.keys(css).map(key => `${stringToCss(key)}:${css[key]}`).join(';');
const addStyle = (name, css) => {
@ChrisDobby
ChrisDobby / Visitor.fs
Created August 28, 2015 09:31
Visitor in F#
namespace Visitor
type IPrescription =
abstract member Accept : IPrescriptionVisitor -> unit
and IPrescriptionVisitor =
abstract member Visit : PointInTime -> unit
abstract member Visit : Infusion -> unit
and PointInTime() =