Skip to content

Instantly share code, notes, and snippets.

View danvk's full-sized avatar

Dan Vanderkam danvk

View GitHub Profile
@danvk
danvk / sprite-custom-layer.tsx
Created October 1, 2019 18:00
Mapbox custom layer which renders multiple models in a THREE.js scene
import MapboxGL, {LngLatLike, MercatorCoordinate} from 'mapbox-gl';
import React, {useEffect, useState} from 'react';
import {withMap} from 'react-mapbox-gl/lib-esm/context';
import {FeatureCollection} from 'geojson';
import * as THREE from 'three';
import {GLTFLoader} from 'three/examples/jsm/loaders/GLTFLoader';
export interface SpritePaint {
gltfPath: string;
@danvk
danvk / dom-errors.ts
Created September 25, 2019 15:45
Errors surfaced in DOM code (for Effective TypeScript)
function handleDrag(eDown: Event) {
const targetEl = eDown.currentTarget;
targetEl.classList.add('dragging');
// ~~~~~~~ Object is possibly 'null'.
// ~~~~~~~~~ Property 'classList' does not exist on type 'EventTarget'.
const dragStart = [
eDown.clientX, eDown.clientY
// ~~~~~~~ 'clientX' does not exist on 'Event'.
// ~~~~~~~ 'clientY' does not exist on 'Event'.
];
@danvk
danvk / gql.py
Created February 28, 2019 21:08
Pretty-print GeoJSON in a slightly more compact way by putting coordinates on one line.
#!/usr/bin/env python3
"""Pretty-print GeoJSON in a slightly more compact way by putting coordinates on one line.
Compare:
[
[
37.23423,
79.23423
],
const inferKeys = <V extends {}>() => <K extends string>(x: Record<K,V>): Record<K,V> => x;
const INIT_VIEW = inferKeys<Partial<MapProps>>()({
nyc: {
center: [-73.991284, 40.741263],
zoom: [14.5],
pitch: [45],
bearing: [-17.6],
},
sf: {
const inferPick = <V extends {}>() => <K extends keyof V>(x: Pick<V, K>): Pick<V, K> => x;
const INIT_VIEW = inferPick<MapProps>()({
center: [-73.991284, 40.741263],
zoom: [14.5],
pitch: [45],
bearing: [-17.6],
style: "mapbox://styles/mapbox/streets-v9"
});
const INIT_VIEW: Pick<MapProps, 'center'|'zoom'|'pitch'|'bearing'|'style'> = {
center: [-73.991284, 40.741263],
zoom: [14.5],
pitch: [45],
bearing: [-17.6],
style: "mapbox://styles/mapbox/streets-v9"
};
const INIT_VIEW: Partial<Props> = {
center: [-73.991284, 40.741263],
zoom: [14.5],
pitch: [45],
bearing: [-17.6],
};
export function render(): JSX.Element {
return (
<Map
export interface Props {
 style: string | MapboxGl.Style;
 center?: [number, number];
 zoom?: [number];
 maxBounds?: MapboxGl.LngLatBounds | FitBounds;
 fitBounds?: FitBounds;
 fitBoundsOptions?: FitBoundsOptions;
 bearing?: [number];
 pitch?: [number];
 containerStyle?: React.CSSProperties;
import * as React from 'react';
import ReactMapboxGl from 'react-mapbox-gl';
const Map = ReactMapboxGl({
accessToken: '....',
});
export function render(): JSX.Element {
return (
<Map
interface Order {
 what: Product;
 how: 'bitcoin' | 'usd';
}
const order: Order = {
 what: 'Macbook Air',
 how: 'bitcoin'
};
purchase(order.what); // ok