Elicit.js

Looks

A look changes how the finished chart is drawn: by hand with looks.rough(), or through a shader with looks.sketch(), looks.cartoon(), looks.halftone() and looks.custom(). Set them in looks: [...] on the Elicit spec. A look changes no data, scale or gesture. Looks are experimental, so their names and options may change.

API reference

looks (on Elicit) · experimental

Import looks from elicitjs and pass in the chart’s looks: [...]. A look changes how the chart is drawn and nothing else. rough draws in both renderers; the others are shaders and need CanvasRenderer.

looks.rough({ scope?, roughness?, fill?, seed? }) → Look
looks.sketch({ scope?, amount?, grain?, seed? }) → Look
looks.cartoon({ scope?, levels?, outline?, ink? }) → Look
looks.halftone({ scope?, size?, angle? }) → Look
looks.custom(fragment, { scope?, uniforms?, seed? }) → Look
Options
OptionType · defaultDescription
scope
'marks' | 'frame'default'marks'
What the look covers: the marks that draw data, or the whole chart.
rough.roughness
numberdefault1
How far the pen strays, 0 to 3. 0 draws true lines.
rough.fill
'hachure' | 'solid'default'hachure'
How a mark's fill is drawn: hatched with lines, or filled.
rough.seed / sketch.seed / custom.seed
numberdefault1
Picks the randomness. The same seed draws the same chart. A custom shader reads it as u_seed.
sketch.amount
numberdefault1.2
How far lines wobble, 0 to 2.
sketch.grain
numberdefault0.25
The strength of the pencil grain, 0 to 1.
cartoon.levels
numberdefault4
Steps of colour per channel, 2 to 16.
cartoon.outline
numberdefault1
The strength of the ink line, 0 to 1.
cartoon.ink
stringdefaultnear-black
The ink line's colour.
halftone.size
numberdefault5
The distance between dots in px, 2 to 40.
halftone.angle
numberdefault30
The angle of the dot screen, in degrees.
custom fragment
string
GLSL ES 1.00 source that defines vec4 look(vec2 uv).
custom.uniforms
{ [name]: number | number[] }
Numbers the fragment reads by name. A number is a float; two to four numbers are a vec2, vec3 or vec4.

Drawn by hand

looks.rough() redraws each shape with a pen that wobbles. Lines are drawn twice, corners overshoot, and a mark's fill is hatched. It draws in both renderers.

Each shape keeps its true outline for the pointer, so a drag grabs a bar where it sits. roughness sets how far the pen strays, from 0 to 3. fill: "solid" fills a mark with colour in place of hatching. seed picks another hand, and the same seed draws the same chart every time.

A hand-drawn bar chart

looks: [looks.rough()]. The bars are hatched and their edges wobble; the drag still reads each bar's true shape.

const chart = Elicit({
width: 380, height: 260,
margins: { top: 14, right: 14, bottom: 26, left: 30 },
looks: [looks.rough()],
data: [
{ x: "A", y: 20 }, { x: "B", y: 45 },
{ x: "C", y: 30 }, { x: "D", y: 60 },
],
schema: {
x: { type: "categorical", domain: ["A", "B", "C", "D"] },
y: { type: "quantitative", domain: [0, 100] },
},
marks: [
barY({
channels: {
x: { field: "x" },
y: { field: "y", edit: move() },
fill: { field: "x" },
},
}),
],
});
mount(chart.dom());
Try: Drag a bar up or down.

What a look covers

scope is "marks" by default: the look covers the marks that draw data, and axes, labels, legends and guides stay as drawn. scope: "frame" covers the whole chart.

looks.rough() leaves text as it is in either scope. A shader look with scope: "frame" runs over text too, so tick labels wobble or break into dots.

The whole frame

scope: "frame" redraws the axis lines and ticks with the marks. fill: "solid" fills the dots.

const points = d3.range(11).map((t) => ({ t, v: 30 + 40 * Math.sin(t / 2) + t * 2 }));
const chart = Elicit({
width: 380, height: 260,
margins: { top: 14, right: 14, bottom: 26, left: 30 },
// "frame" redraws the axes too. Text stays as it is.
looks: [looks.rough({ scope: "frame", fill: "solid", roughness: 1.5 })],
data: points,
schema: {
t: { type: "quantitative", domain: [0, 10] },
v: { type: "quantitative", domain: [0, 100] },
},
marks: [
lineY({ strokeWidth: 2, curve: "catmullRom", channels: { x: { field: "t" }, y: { field: "v" } } }),
point({ size: 6, channels: { x: { field: "t" }, y: { field: "v", edit: move() } } }),
],
});
mount(chart.dom());
Try: Drag a dot. The line follows, redrawn.

Shader looks

A shader look runs over the chart's pixels once it is painted. It needs the canvas renderer, so set renderer: "canvas". Under SVG the chart draws without the look and logs a warning.

  • looks.sketch() wobbles and doubles every line and gives fills a pencil grain. amount sets the wobble, from 0 to 2, and grain the grain, from 0 to 1.
  • looks.cartoon() flattens colour into levels steps and inks an outline where colour changes, in the colour ink.
  • looks.halftone() redraws the chart as dots size px apart, on a screen turned by angle degrees.

sketch and cartoon move a pixel by less than 4px, so a mark is drawn where the pointer hits it. So does halftone up to a size of about 5.7. At a larger size, a dot can sit more than 4px from its mark, which is further than a press reaches.

Sketch

looks.sketch() on canvas: wobbling edges and a pencil grain.

const chart = Elicit({
width: 380, height: 260,
renderer: "canvas",
margins: { top: 14, right: 14, bottom: 26, left: 30 },
looks: [looks.sketch()],
data: [
{ x: "A", y: 20 }, { x: "B", y: 45 },
{ x: "C", y: 30 }, { x: "D", y: 60 },
],
schema: {
x: { type: "categorical", domain: ["A", "B", "C", "D"] },
y: { type: "quantitative", domain: [0, 100] },
},
marks: [
barY({
channels: {
x: { field: "x" },
y: { field: "y", edit: move() },
fill: { field: "x" },
},
}),
],
});
mount(chart.dom());
Try: Drag a bar.

Cartoon

looks.cartoon(): flat colour and an ink line.

const chart = Elicit({
width: 380, height: 260,
renderer: "canvas",
margins: { top: 14, right: 14, bottom: 26, left: 30 },
looks: [looks.cartoon()],
data: [
{ x: "A", y: 20 }, { x: "B", y: 45 },
{ x: "C", y: 30 }, { x: "D", y: 60 },
],
schema: {
x: { type: "categorical", domain: ["A", "B", "C", "D"] },
y: { type: "quantitative", domain: [0, 100] },
},
marks: [
barY({
channels: {
x: { field: "x" },
y: { field: "y", edit: move() },
fill: { field: "x" },
},
}),
],
});
mount(chart.dom());
Try: Drag a bar.

Halftone

looks.halftone(): the marks as a screen of dots. The axes are outside the default scope.

const chart = Elicit({
width: 380, height: 260,
renderer: "canvas",
margins: { top: 14, right: 14, bottom: 26, left: 30 },
looks: [looks.halftone()],
data: [
{ x: "A", y: 20 }, { x: "B", y: 45 },
{ x: "C", y: 30 }, { x: "D", y: 60 },
],
schema: {
x: { type: "categorical", domain: ["A", "B", "C", "D"] },
y: { type: "quantitative", domain: [0, 100] },
},
marks: [
barY({
channels: {
x: { field: "x" },
y: { field: "y", edit: move() },
fill: { field: "x" },
},
}),
],
});
mount(chart.dom());
Try: Drag a bar.

Your own shader

looks.custom(fragment, options) runs your GLSL. fragment is GLSL ES 1.00 source that defines vec4 look(vec2 uv), which returns the pixel at uv. Colour is premultiplied by alpha, both in the frame you read and in the pixel you return.

A fragment can read and call:

NameTypeWhat it is
u_framesampler2DThe painted chart.
u_resolutionvec2The frame's size in device px.
u_pixelRatiofloatDevice px per chart px.
u_seedfloatThe seed option.
chartPx(uv)vec2A uv as chart px, whose origin is the top-left corner.
toUv(px)vec2Chart px as a uv.
hash(p)floatNoise from 0 to 1 at a vec2, a new value at every point.
noise(p)floatNoise from 0 to 1 at a vec2, smooth between whole numbers.

uniforms passes your own numbers under their own names: a number arrives as a float, and two to four numbers as a vec2, vec3 or vec4.

A fragment that fails to compile is skipped, and the compiler's error is logged. A shader that moves pixels further than 4px draws a mark away from where the pointer hits it.

A custom shader

looks.custom(fragment, { uniforms }). The fragment shifts each row sideways by its strength uniform and mixes in its tint.

// `look` returns the pixel at uv. This one shifts each row sideways and tints it.
const wave = `
vec4 look(vec2 uv) {
vec2 p = chartPx(uv);
p.x += sin(p.y / 9.0) * strength;
vec4 c = texture2D(u_frame, toUv(p));
return vec4(mix(c.rgb, tint * c.a, 0.3), c.a);
}`;
const chart = Elicit({
width: 380, height: 260,
renderer: "canvas",
margins: { top: 14, right: 14, bottom: 26, left: 30 },
looks: [looks.custom(wave, { uniforms: { strength: 2.5, tint: [1, 0.3, 0.6] } })],
data: [
{ x: "A", y: 20 }, { x: "B", y: 45 },
{ x: "C", y: 30 }, { x: "D", y: 60 },
],
schema: {
x: { type: "categorical", domain: ["A", "B", "C", "D"] },
y: { type: "quantitative", domain: [0, 100] },
},
marks: [
barY({
channels: {
x: { field: "x" },
y: { field: "y", edit: move() },
fill: { field: "x" },
},
}),
],
});
mount(chart.dom());
Try: Edit strength or tint in the code.

Several looks

looks.rough() runs first. Shader looks with scope: "marks" run next, in list order, and shader looks with scope: "frame" run last, in list order.

Rough, then sketch

The shapes are redrawn by hand, then the frame takes a light wobble and a pencil grain.

const chart = Elicit({
width: 380, height: 260,
renderer: "canvas",
margins: { top: 14, right: 14, bottom: 26, left: 30 },
looks: [
looks.rough({ scope: "frame" }),
looks.sketch({ scope: "frame", amount: 0.5, grain: 0.4 }),
],
data: [
{ x: "A", y: 20 }, { x: "B", y: 45 },
{ x: "C", y: 30 }, { x: "D", y: 60 },
],
schema: {
x: { type: "categorical", domain: ["A", "B", "C", "D"] },
y: { type: "quantitative", domain: [0, 100] },
},
marks: [
barY({
channels: {
x: { field: "x" },
y: { field: "y", edit: move() },
fill: { field: "x" },
},
}),
],
});
mount(chart.dom());
Try: Drag a bar.

Faces, emoji and glyphs

A look covers a glyph part by part, and each part keeps its own edit. looks.rough() redraws shapes, so it reaches a face and a composite glyph, such as the plant from the glyph case study. An emoji from a symbol channel is text, so looks.rough() leaves it as drawn and a shader look reaches it.

A face, by hand

looks.rough({ fill: "solid", roughness: 1.6 }) over a face. The head, eyes, brows and mouth are each redrawn.

const chart = Elicit({
width: 340, height: 340,
// Every part is redrawn by hand, and each still takes its own drag.
looks: [looks.rough({ fill: "solid", roughness: 1.6 })],
margins: { top: 12, right: 12, bottom: 12, left: 12 },
data: [ {
curve: 0.7, smirk: 0.5,
aperture: 0.6, squint: 0.2, browH: 0.5, browT: 0.4,
} ],
schema: {
curve: { type: "quantitative", domain: [0, 1] },
smirk: { type: "quantitative", domain: [0, 1] },
aperture: { type: "quantitative", domain: [0, 1] },
squint: { type: "quantitative", domain: [0, 1] },
browH: { type: "quantitative", domain: [0, 1] },
browT: { type: "quantitative", domain: [0, 1] },
},
marks: [
face({
channels: {
// The mouth is one part, so one drag drives both of its params: the
// vertical component bends it, the horizontal one tilts it into a smirk.
mouthCurve: { field: "curve", edit: slide({ axis: "y", increase: "up" }) },
mouthAsym: { field: "smirk", edit: slide({ axis: "x", increase: "right" }) },
// Each eye is one ellipse: rx opens it, ry squints it. Horizontal and
// vertical drag, so one gesture on an eye reads as reshaping it.
eyeScale: { field: "aperture", edit: slide({ axis: "x", increase: "right" }) },
eyeSquint: { field: "squint", edit: slide({ axis: "y", increase: "down" }) },
// Each brow is one tick: height is where it SITS — a positional channel in
// the face's frame, so `move()` inverts straight back through it — and tilt
// is a magnitude, so it slides.
browHeight: { field: "browH", edit: move({ channels: ["y"] }) },
browTilt: { field: "browT", edit: slide({ axis: "x", increase: "right" }) },
},
}),
],
});
mount(chart.dom());
Try: Drag the mouth, an eye or a brow.

A face, as a cartoon

The same face on canvas under looks.cartoon(): flat colour and an ink line round every part.

const chart = Elicit({
width: 340, height: 340,
renderer: "canvas",
looks: [looks.cartoon()],
margins: { top: 12, right: 12, bottom: 12, left: 12 },
data: [ {
curve: 0.7, smirk: 0.5,
aperture: 0.6, squint: 0.2, browH: 0.5, browT: 0.4,
} ],
schema: {
curve: { type: "quantitative", domain: [0, 1] },
smirk: { type: "quantitative", domain: [0, 1] },
aperture: { type: "quantitative", domain: [0, 1] },
squint: { type: "quantitative", domain: [0, 1] },
browH: { type: "quantitative", domain: [0, 1] },
browT: { type: "quantitative", domain: [0, 1] },
},
marks: [
face({
channels: {
// The mouth is one part, so one drag drives both of its params: the
// vertical component bends it, the horizontal one tilts it into a smirk.
mouthCurve: { field: "curve", edit: slide({ axis: "y", increase: "up" }) },
mouthAsym: { field: "smirk", edit: slide({ axis: "x", increase: "right" }) },
// Each eye is one ellipse: rx opens it, ry squints it. Horizontal and
// vertical drag, so one gesture on an eye reads as reshaping it.
eyeScale: { field: "aperture", edit: slide({ axis: "x", increase: "right" }) },
eyeSquint: { field: "squint", edit: slide({ axis: "y", increase: "down" }) },
// Each brow is one tick: height is where it SITS — a positional channel in
// the face's frame, so `move()` inverts straight back through it — and tilt
// is a magnitude, so it slides.
browHeight: { field: "browH", edit: move({ channels: ["y"] }) },
browTilt: { field: "browT", edit: slide({ axis: "x", increase: "right" }) },
},
}),
],
});
mount(chart.dom());
Try: Drag the mouth, an eye or a brow.

Emoji, inked

looks.cartoon() over emoji symbols. The axes are outside the default scope and stay as drawn.

const chart = Elicit({
width: 380, height: 250,
renderer: "canvas",
// An emoji is text, which only a shader look reaches.
looks: [looks.cartoon()],
margins: { top: 16, right: 16, bottom: 30, left: 64 },
data: [
{ day: "Mon", mood: "ok" }, { day: "Tue", mood: "good" },
],
schema: {
day: { type: "categorical", domain: ["Mon","Tue","Wed","Thu","Fri"] },
mood: { type: "ordinal", domain: ["bad","ok","good","great"] },
},
marks: [
point({
size: 15,
channels: {
x: { field: "day" },
y: { field: "mood" },
symbol: {
field: "mood",
scale: { range: ["😢","😐","🙂","😄"] },
edit: cycle(),
},
},
}),
],
});
mount(chart.dom());
Try: Click an emoji to cycle its mood.

Emoji, sketched

looks.sketch({ amount: 2, grain: 0.5 }): the emoji wobble and take a pencil grain.

const chart = Elicit({
width: 380, height: 250,
renderer: "canvas",
looks: [looks.sketch({ amount: 2, grain: 0.5 })],
margins: { top: 16, right: 16, bottom: 30, left: 64 },
data: [
{ day: "Mon", mood: "ok" }, { day: "Tue", mood: "good" },
],
schema: {
day: { type: "categorical", domain: ["Mon","Tue","Wed","Thu","Fri"] },
mood: { type: "ordinal", domain: ["bad","ok","good","great"] },
},
marks: [
point({
size: 15,
channels: {
x: { field: "day" },
y: { field: "mood" },
symbol: {
field: "mood",
scale: { range: ["😢","😐","🙂","😄"] },
edit: cycle(),
},
},
}),
],
});
mount(chart.dom());
Try: Click an emoji to cycle its mood.

A plant, hatched

The plant glyph from the glyph case study under looks.rough(). Hatching is see-through, so the stem shows behind the flower.

const chart = Elicit({
width: 320, height: 340,
looks: [looks.rough()],
margins: { top: 10, right: 10, bottom: 10, left: 10 },
// How is your week going? Three answers, one plant.
data: [{ energy: 0.5, joy: 0.4, support: 0.5 }],
schema: {
energy: { type: "quantitative", domain: [0, 1] }, // how tall it grows
joy: { type: "quantitative", domain: [0, 1] }, // how big it blooms
support: { type: "quantitative", domain: [0, 1] }, // how full its leaves are
},
marks: [
// Every part is placed in the glyph's own units: x and y run from -1 to 1, y up.
composite({
channels: { size: { value: 150 } },
parts: [
// The stem's top is `energy`; the flower below sits on the same field
// and range, so the two always meet.
ruleX({ channels: {
x: { frame: 0 }, y1: { frame: -0.55 },
y2: { field: "energy", frame: [-0.15, 0.65] },
stroke: { value: "#4d7c0f" }, strokeWidth: { value: 6 },
} }),
// Two leaves. A leaf's centre and its width both read `support`, so its
// inner edge stays on the stem as it grows.
ellipse({ channels: {
x: { field: "support", frame: [-0.08, -0.3] }, y: { frame: -0.3 },
rx: { field: "support", frame: [0.06, 0.28],
edit: slide({ axis: "x", increase: "left" }) },
ry: { frame: 0.07 }, angle: { value: -25 },
fill: { value: "#65a30d" },
} }),
ellipse({ channels: {
x: { field: "support", frame: [0.08, 0.3] }, y: { frame: -0.3 },
rx: { field: "support", frame: [0.06, 0.28],
edit: slide({ axis: "x", increase: "right" }) },
ry: { frame: 0.07 }, angle: { value: 25 },
fill: { value: "#65a30d" },
} }),
// The pot.
rect({ channels: {
x1: { frame: -0.3 }, x2: { frame: 0.3 }, y1: { frame: -0.95 }, y2: { frame: -0.55 },
fill: { value: "#b45309" },
} }),
// The flower: drag it up or down to set `energy`, sideways to set `joy`.
point({ channels: {
y: { field: "energy", frame: [-0.15, 0.65], edit: move({ channels: ["y"] }) },
size: { field: "joy", frame: [0.08, 0.3],
edit: slide({ axis: "x", increase: "right" }) },
fill: { value: "#db2777" },
} }),
point({ channels: {
y: { field: "energy", frame: [-0.15, 0.65] },
size: { field: "joy", frame: [0.03, 0.11] },
fill: { value: "#fde047" },
} }),
],
}),
],
});
mount(chart.dom());
Try: Drag the flower up, down or sideways, or a leaf outwards.

A plant, in dots

The same plant on canvas under looks.halftone({ size: 4 }).

const chart = Elicit({
width: 320, height: 340,
renderer: "canvas",
looks: [looks.halftone({ size: 4 })],
margins: { top: 10, right: 10, bottom: 10, left: 10 },
// How is your week going? Three answers, one plant.
data: [{ energy: 0.5, joy: 0.4, support: 0.5 }],
schema: {
energy: { type: "quantitative", domain: [0, 1] }, // how tall it grows
joy: { type: "quantitative", domain: [0, 1] }, // how big it blooms
support: { type: "quantitative", domain: [0, 1] }, // how full its leaves are
},
marks: [
// Every part is placed in the glyph's own units: x and y run from -1 to 1, y up.
composite({
channels: { size: { value: 150 } },
parts: [
// The stem's top is `energy`; the flower below sits on the same field
// and range, so the two always meet.
ruleX({ channels: {
x: { frame: 0 }, y1: { frame: -0.55 },
y2: { field: "energy", frame: [-0.15, 0.65] },
stroke: { value: "#4d7c0f" }, strokeWidth: { value: 6 },
} }),
// Two leaves. A leaf's centre and its width both read `support`, so its
// inner edge stays on the stem as it grows.
ellipse({ channels: {
x: { field: "support", frame: [-0.08, -0.3] }, y: { frame: -0.3 },
rx: { field: "support", frame: [0.06, 0.28],
edit: slide({ axis: "x", increase: "left" }) },
ry: { frame: 0.07 }, angle: { value: -25 },
fill: { value: "#65a30d" },
} }),
ellipse({ channels: {
x: { field: "support", frame: [0.08, 0.3] }, y: { frame: -0.3 },
rx: { field: "support", frame: [0.06, 0.28],
edit: slide({ axis: "x", increase: "right" }) },
ry: { frame: 0.07 }, angle: { value: 25 },
fill: { value: "#65a30d" },
} }),
// The pot.
rect({ channels: {
x1: { frame: -0.3 }, x2: { frame: 0.3 }, y1: { frame: -0.95 }, y2: { frame: -0.55 },
fill: { value: "#b45309" },
} }),
// The flower: drag it up or down to set `energy`, sideways to set `joy`.
point({ channels: {
y: { field: "energy", frame: [-0.15, 0.65], edit: move({ channels: ["y"] }) },
size: { field: "joy", frame: [0.08, 0.3],
edit: slide({ axis: "x", increase: "right" }) },
fill: { value: "#db2777" },
} }),
point({ channels: {
y: { field: "energy", frame: [-0.15, 0.65] },
size: { field: "joy", frame: [0.03, 0.11] },
fill: { value: "#fde047" },
} }),
],
}),
],
});
mount(chart.dom());
Try: Drag the flower or a leaf.

Limits

  • A shader look reads the chart's pixels. A chart that draws geoTile() tiles from a server without CORS headers draws without its shader looks and logs a warning.
  • A browser with no WebGL draws the chart without its shader looks and logs a warning.
  • looks.rough() leaves text and images as drawn.