@reintent/scene/pick
The pick buffer: what is at a point, and what can be seen of each thing, from the pixels actually drawn.
A copy of the scene is painted once more, every semantic path in one flat colour, atmosphere left out, nothing translucent: SVG is cloned, recoloured and rasterised; Canvas drawings run again through a context that paints in their path's colour. Whatever the scene drew, and however, its pixels say whose they are, so a hit test and a visible box are the same on SVG and on Canvas.
Interfaces
Pick
Defined in: packages/scene/src/pick.ts
What the pick buffer says: each path's visible box, how much is claimed, and what is at a point.
Properties
| Property | Type | Description |
|---|---|---|
at | (x, y) => string | null | For hit tests: a scene point → the path drawn there (or within 3 pixels of it), or null. |
boxes | Map<string, Box> | What can be seen of each path (and of each of its prefixes), in scene units. |
coverage | number | Pixels that belong to some path, over pixels drawn at all (atmosphere aside). |
unclaimed | Box | null | Where the pixels nobody claims are, if any. |
Functions
measureVisible()
function measureVisible(
svg,
registry,
t
): Promise<Pick>;Defined in: packages/scene/src/pick.ts
Paints the pick buffer of a scene at time t, its svg and the registry's Canvas drawings, and
reads it: what can be seen of each thing, and what is at a point.
Parameters
| Parameter | Type |
|---|---|
svg | SVGSVGElement |
registry | Registry |
t | number |
Returns
Promise<Pick>
pickColour()
function pickColour(path): string;Defined in: packages/scene/src/pick.ts
The flat colour a path is painted in the pick buffer, as CSS: the same for the page's life, a different one for each path, never magenta (what no thing claims) and never black.
Parameters
| Parameter | Type |
|---|---|
path | string |
Returns
string
Example
import { pickColour } from '@reintent/scene/pick';
console.log(
pickColour('lamp.shade') === pickColour('lamp.shade'),
pickColour('lamp.base') !== pickColour('lamp.shade'),
);
// → true truepickContext()
function pickContext(ctx, colour): CanvasRenderingContext2D;Defined in: packages/scene/src/pick.ts
A Canvas context that paints everything a drawing does in one colour, and nothing translucent: what the drawing sets as its fill or stroke is ignored, an image is a filled rectangle, shadows, filters and blending are dropped. The drawing itself does not change.
Parameters
| Parameter | Type |
|---|---|
ctx | CanvasRenderingContext2D |
colour | string |
Returns
CanvasRenderingContext2D
Example
import { pickContext } from '@reintent/scene/pick';
const ctx = {
fillStyle: '',
strokeStyle: '',
globalAlpha: 1,
shadowBlur: 0,
filter: 'none',
};
const pick = pickContext(ctx as unknown as CanvasRenderingContext2D, 'rgb(67,109,151)');
pick.fillStyle = 'rgba(255,220,160,0.2)';
pick.globalAlpha = 0.3;
console.log(ctx.fillStyle, ctx.globalAlpha);
// → 'rgb(67,109,151)' 1pickFromPixels()
function pickFromPixels(
data,
width,
height,
frame
): Pick;Defined in: packages/scene/src/pick.ts
Reads a pick buffer's pixels (RGBA, width × height, as getImageData gives them) painted over
frame (the scene's viewBox), a pixel to a scene unit: each opaque pixel in a path's colour is that path's;
a magenta one is drawn by nothing declared; any other (a blended edge) is no one's.
Parameters
| Parameter | Type |
|---|---|
data | Uint8ClampedArray |
width | number |
height | number |
frame | { height: number; width: number; x: number; y: number; } |
frame.height | number |
frame.width | number |
frame.x | number |
frame.y | number |
Returns
Example
import { pickColour, pickFromPixels } from '@reintent/scene/pick';
const [r, g, b] = pickColour('lamp.shade').match(/\d+/g)!.map(Number);
const pixels = new Uint8ClampedArray(4 * 4 * 4); // 4 × 4, transparent
for (const i of [5, 6]) pixels.set([r, g, b, 255], i * 4); // two pixels of the shade, in row 1
const pick = pickFromPixels(pixels, 4, 4, { x: 0, y: 0, width: 4, height: 4 });
console.log(pick.boxes.get('lamp'), pick.at(2.5, 1.5), pick.coverage);
// → { x0: 1, y0: 1, x1: 3, y1: 2, px: 2 } 'lamp.shade' 1Last updated on
Edit on GitHub