Skip to content

Latest commit

 

History

History
140 lines (100 loc) · 7.38 KB

images.mdx

File metadata and controls

140 lines (100 loc) · 7.38 KB
title Images
description Line art from SVG files, and image tone and direction as fields that drive marks.

Files come from the studio's Assets page. A sketch names a file by string literal, so the studio can load it before the sketch runs. The examples here use ivy.png and church.svg from the studio's sample assets. Upload your own on the Assets page and use its name, or inline an SVG as text. An image never draws itself: it gives values, and the sketch decides the marks.

svg()

/_sig/svg.mdx

Turns SVG line art into ordinary open shapes: polylines, lines, and paths of straight segments and Bézier curves. The engine occludes, clips, masks and modifies them like any shape. width scales the drawing, and the height follows the aspect. layers keeps only the top-level groups named. Transforms in the file apply exactly. Elliptical arcs have no exact form here, so the import rejects them and names the path.

import { sketch, svg, circle, mm } from 'occlude';

const ART = `<svg viewBox="0 0 100 60"><g id="ink"><path d="M10 50 C 30 10, 50 10, 70 50 Q 80 65 90 50" fill="none" stroke="black"/><line x1="10" y1="55" x2="90" y2="55"/></g></svg>`;

export default sketch({ aspect: [1, 1] }, (t) => [svg(ART, { x: 5, y: 20, width: 90, bridge: mm(0.7) }), circle(50, 46, 12, { opaque: true, pen: 'stabilo-88-blue' })]);

t.asset()

/_sig/Toolkit.asset.mdx

The text of an uploaded file, for svg(). The name must be a string literal in the sketch.

import { sketch, svg } from 'occlude';

export default sketch({ aspect: [1, 1] }, (t) => svg(t.asset('church.svg'), { x: 5, y: 5, width: 90 }));

t.image()

/_sig/Toolkit.image.mdx

Places an image in drawable coordinates and returns a sampler. The sampler takes a position and an optional area, a half-size to average over. lum is luminance, 0 to 1. a is alpha. edge is the gradient magnitude and dir is its angle. Average over the mark's own footprint when a mark covers more paper than a pixel.

import { sketch, circle } from 'occlude';

export default sketch({ aspect: [1, 1] }, (t) => {
  const img = t.image('ivy.png', { x: 8, y: 2, width: 84 });
  return t.grid({ cols: 46, rows: 46 }).faces.map((f) => { const { cx, cy, w } = f.bounds; const dark = 1 - img.lum(cx, cy, w / 2); return img.a(cx, cy) > 0.5 && circle(cx, cy, dark * w * 0.5); });
});

img.field()

/_sig/ImageSampler.field.mdx

img.field(channel?, { area? }) is a channel as a scalar field. The rest of the toolkit reads fields. The image goes behind t.isolines, t.scatter, t.streamlines or a modifier amount with no wrapper. Outside the placed rectangle the field is 0.

Channel Value
'lum' luminance, 0 to 1. The default
'dark' 1 − lum
'a' alpha
'edge' the magnitude of the luminance gradient
'r', 'g', 'b' the colour channels as the file stores them
'c', 'm', 'y', 'k' the four printing inks, with the grey taken out

k is the tone all three inks share. Each of c, m and y then carries only the ink that remains. So a grey area asks for black alone, and four pens print the picture.

import { sketch, circle, mm } from 'occlude';

export default sketch({ aspect: [1, 1], seed: 3 }, (t) => {
  const img = t.image('ivy.png', { x: 8, y: 2, width: 84 });
  const dark = img.field('dark', { area: 1 });
  return t.scatter((x, y) => img.a(x, y) * dark(x, y), { spacing: mm(1.6) }).points.map((p) => circle(p, 0.5));
});

img.flow()

/_sig/ImageSampler.flow.mdx

img.flow({ radius?, iterations? }) is the direction that the picture's structure runs, as a vector field. It runs along an edge, not across it. It is smooth over radius, so noise does not turn it. Feed it to t.streamlines for hatching that follows the subject.

import { sketch, strokes, mm } from 'occlude';

export default sketch({ aspect: [1, 1] }, (t) => {
  const img = t.image('ivy.png', { x: 8, y: 2, width: 84 });
  const lines = t.streamlines(img.flow({ radius: 3 }), { spacing: (x, y) => mm(0.8 + img.lum(x, y, 1) * 3) });
  return strokes(lines);
});

img.palette()

/_sig/ImageSampler.palette.mdx

The colours a picture holds, most of the paper first. Give a count and occlude fits that many colours to the pixels, with a weighted k-means in Lab space. The fit reads the picture and never the sketch's seed, so one file gives one palette in every sketch. Give colours of your own, as hex strings or as pens. The fit then drops out, and each pixel takes the nearest colour you named.

An entry holds color, share, field(opts?) and contours(level?). color comes back as lower case #rrggbb, so pens[entry.color] is a stable lookup. share is the fraction of the picture that colour holds. field() is membership as an ordinary scalar field. The value is 1 at the colour itself, 0.5 halfway to the next nearest, and 0 where another colour wins. contours(level) marches that field into contours, closed along the edge of the picture. An entry is an area, so polygon(entry) fills it at the level 0.5. A picture with fewer distinct colours than you asked for gives fewer entries.

import { sketch, polygon, pen, mm } from 'occlude';

// Three colours fitted to the photograph, and the boundary of each one's
// territory drawn in a pen of that colour. The entries are plain data, so
// the sketch sorts them light to dark before it hands out the pens.
export default sketch({ aspect: [1, 1], pens: {
  pale: pen({ width: mm(0.25), color: '#C8B49A' }),
  mid: pen({ width: mm(0.25), color: '#8A7157' }),
  dark: pen({ width: mm(0.25), color: '#2B2723' }),
} }, (t) => {
  const img = t.image('ivy.png', { x: 7.5, y: 2, width: 85 });
  const inks = ['pale', 'mid', 'dark'];
  const tone = (hex) => parseInt(hex.slice(1, 3), 16) + parseInt(hex.slice(3, 5), 16) + parseInt(hex.slice(5, 7), 16);
  const bands = img.palette(3).sort((a, b) => tone(b.color) - tone(a.color));
  return bands.map((e, i) => polygon(e.contours(0.55), { pen: inks[i] }));
});

img.regions()

/_sig/ImageSampler.regions.mdx

The picture as a stack of flat colour areas, lightest first. It is img.palette(count) and then a sweep for connected patches. A patch smaller than tolerance of the picture joins what surrounds it, so a separation reads as areas and not as confetti. Draw the list in order with opaque: true and each dark area hides the lighter ones under it. count: 2 is a monochrome trace, and share is the fraction of the picture a region covers, as on a palette entry. A region answers contours(), so polygon(region, …) draws it and t.within(material, region) cuts to it.

import { sketch, polygon, fill, pen, mm } from 'occlude';

// A three-tone separation. Each region is opaque, so the darker ones hide
// the lighter ones they sit on, and each pen hatches at its own angle.
export default sketch({ aspect: [1, 1], pens: {
  pale: pen({ width: mm(0.3), color: '#C8B49A' }),
  mid: pen({ width: mm(0.3), color: '#8A7157' }),
  dark: pen({ width: mm(0.3), color: '#2B2723' }),
} }, (t) => {
  const img = t.image('ivy.png', { x: 7.5, y: 2, width: 85 });
  const inks = ['pale', 'mid', 'dark'];
  return img.regions({ count: 3 }).map((r, i) =>
    polygon(r, { opaque: true, pen: inks[i], fill: fill('hatch', { angle: 25 + i * 55, spacing: mm(1.1) }) }));
});