Skip to content

Instantly share code, notes, and snippets.

@nkint
nkint / index.js
Last active October 4, 2017 09:07
React + d3 + hover band
// install prot globally https://github.com/mattdesl/prot
import React, { Component } from 'react'
import ReactDOM from 'react-dom'
import { scaleTime } from 'd3-scale'
import format from 'd3-time-format'
function VerticalLine({ x, height, stroke="#888" }) {
return <line
x1={x}
@nkint
nkint / gist:c9c05d8a0de4d736f27036d594645fd0
Created September 22, 2017 12:49
Wrap text on canvas
export function wrapTextCanvas(context, text, x, y, maxWidth, lineHeight, drawFlag = true) {
const cars = text.split('\n')
for (let ii = 0; ii < cars.length; ii++) {
let line = ''
const words = cars[ii].split(' ')
for (let n = 0; n < words.length; n++) {
const testLine = line + words[n] + ' '
const metrics = context.measureText(testLine)
import defined from 'defined'
const TO_PX = 35.43307
const DEFAULT_SVG_LINE_WIDTH = 0.03
export function shapesToSVG(shapes, opt = {}) {
const dimensions = opt.dimensions
if (!dimensions) throw new TypeError('must specify dimensions currently')
const decimalPlaces = 5
@nkint
nkint / regs-camera.js
Created May 31, 2017 13:58
Sketch of new regl camera
import { projectionMatrix, viewMatrix } from './turntable-camera'
import mouseChange from 'mouse-change'
import mouseWheel from 'mouse-wheel'
import identity from 'gl-mat4/identity'
import key from 'key-pressed'
// const isBrowser = typeof window !== 'undefined'
const getStartingState = (initialStatePlusOptions) => ({
view: identity(new Float32Array(16)),
@nkint
nkint / gist:66db01619e3a15b57e7b78ce9b1b85d4
Created May 2, 2017 22:05
Vertex shader with 4 texture (tiling satellite image)
precision mediump float;
uniform sampler2D satellite0;
uniform sampler2D satellite1;
uniform sampler2D satellite2;
uniform sampler2D satellite3;
uniform sampler2D elevation;
varying vec2 vUv;
@nkint
nkint / regl-load-image-texture
Created April 29, 2017 23:08
load image from url into a regl.texture object
const emptyTextureDimension = {
width: 512,
height: 512,
channels: 3,
}
function loadImageIntoTexture(url, texture) {
const image = new Image()
image.crossOrigin = 'anonymous'
image.src = url
@nkint
nkint / ES6 d3 word wrap
Created January 26, 2017 13:31
ES6 port of famous mbostock's word wrap for d3 SVG text label
// ES6 port of famous mbostock's word wrap https://bl.ocks.org/mbostock/7555321
export function wrap(_text, width, lineHeightEm = 1.1) {
_text.each(function () {
const text = select(this)
const words = text.text().split(/\s+/).reverse()
const y = text.attr('y')
const dy = parseFloat(text.attr('dy')) || 0
let tspan = text.text(null).append('tspan').attr('x', 0).attr('y', y).attr('dy', dy + 'em')
let line = []
@nkint
nkint / pickWithRenderBuffer
Last active October 17, 2020 08:56
Regl and picking with off-screen render buffer
import createRegl from 'regl'
import createCamera from 'regl-camera'
import bunny from 'bunny'
import normals from 'angle-normals'
const regl = createRegl()
const camera = createCamera(regl, {
minDistance: 0.01,
distance: 20,
maxDistance: 30,
@nkint
nkint / pointTowards.vertex
Created December 12, 2016 11:32
Rotate mesh points towards a direction vector with vertex shader
// quaternion code from https://github.com/stackgl/gl-quat
// rotation from https://twistedpairdevelopment.wordpress.com/2013/02/11/rotating-a-vector-by-a-quaternion-in-glsl/
precision mediump float;
uniform mat4 projection, view;
uniform vec3 translate;
uniform vec3 scale;
attribute vec3 normal;
@nkint
nkint / pointTowards
Created December 12, 2016 11:29
Rotate mesh points towards a direction vector with stack-gl / regl
// inspired by toxiclibs
// https://bitbucket.org/postspectacular/toxiclibs/src/44d9932dbc9f9c69a170643e2d459f449562b750/src.core/toxi/geom/mesh/TriangleMesh.java?at=default&fileviewer=file-view-default#TriangleMesh.java-703
const vec3 = require('gl-vec3')
const mat3 = require('gl-mat3')
const mat4 = require('gl-mat4')
const quat = require('gl-quat')
function transform(matrix, v) {
const temp = mat3.create()