Skip to content

Instantly share code, notes, and snippets.

View dcollien's full-sized avatar

David Collien dcollien

View GitHub Profile
@dcollien
dcollien / multipart.js
Last active March 30, 2024 18:15
Parse multi-part formdata in the browser
var Multipart = {
parse: (function() {
function Parser(arraybuf, boundary) {
this.array = arraybuf;
this.token = null;
this.current = null;
this.i = 0;
this.boundary = boundary;
}
@dcollien
dcollien / Canvas.tsx
Created March 20, 2024 01:23
React animated canvas component
import React, {
useRef,
useImperativeHandle
} from "react";
import useAnimationFrame from "../hooks/animationFrame";
type UpdateHandler = (dt: number, canvas: HTMLCanvasElement) => void;
export interface CanvasProps extends React.CanvasHTMLAttributes<HTMLCanvasElement> {
onAnimationFrame: UpdateHandler;
@dcollien
dcollien / compressJson.js
Last active February 21, 2024 02:55
Compress JSON data to use in a URL (GZIP)
/*
Compress (gzip) JSON-serialisable object and output as base 64; Decompress base 64 data and output as parsed JSON.
Useful for URLs, e.g.
Compress:
const bigObject = { ...lotsOfData };
const url = "https://www.example.com/?" + await compressToUrl(bigObject);
@dcollien
dcollien / artStyles.json
Created July 9, 2023 17:49
JSON lists of art styles and artists useful for image prompts (midjourney, stable diffusion)
[
{
"label": "1850's Daguerrotype",
"value": "Daguerrotype",
"filters": [
"photography",
"digital art"
]
},
{
@dcollien
dcollien / ImageTools.es6
Last active April 28, 2023 09:00
Resize Images in the Browser
let hasBlobConstructor = typeof(Blob) !== 'undefined' && (function () {
try {
return Boolean(new Blob());
} catch (e) {
return false;
}
}());
let hasArrayBufferViewSupport = hasBlobConstructor && typeof(Uint8Array) !== 'undefined' && (function () {
try {
@dcollien
dcollien / GitHub_WebHook.coffee
Last active April 7, 2021 23:23
GitHub WebHook Endpoint in CoffeeScript
# Simple GitHub Webhook Listener
express = require 'express'
bodyParser = require 'body-parser'
crypto = require 'crypto'
bufPack = require 'bufferpack'
app = express()
PORT = process.argv[2] or process.env.PORT or 8081
AUTH_SECRET = process.argv[3] or process.env.SECRET_TOKEN or 'test'
@dcollien
dcollien / c_json_servlet.c
Last active August 12, 2020 10:50
C program for a libevent server receiving JSON requests and replying with JSON responses over HTTP
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <event.h>
#include <evhttp.h>
@dcollien
dcollien / index.html
Last active May 5, 2020 12:39
xAPI iFrame Example
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>xAPI Form Example</title>
<!-- xAPI base functionality, from: https://github.com/RusticiSoftware/TinCanJS/blob/master/build/tincan-min.js -->
<script src="tincan-min.js"></script>
<!-- Setting up the xAPI connection from launch data -->
<script src="xapi-interface.js"></script>
const ContrastTools = {
relLuminance(rgba) {
// http://www.w3.org/TR/2008/REC-WCAG20-20081211/#relativeluminancedef
var rgb = rgba.slice();
for (var i = 0; i < 3; i++) {
var channel = rgb[i] / 255;
rgb[i] = channel < .03928 ? channel / 12.92 : Math.pow((channel + .055) / 1.055, 2.4);
}
@dcollien
dcollien / AnimatedCanvas.tsx
Last active February 12, 2020 08:51
Animated Canvas Component
import React, { useRef, useEffect, useCallback, useState } from "react";
type UpdateHandler = (dt: number) => void;
type ContextRenderer = (ctx: CanvasRenderingContext2D) => void;
export interface IAnimatedCanvasProps {
width: number;
height: number;
onFrame: UpdateHandler;
render: ContextRenderer;