@rasamjs/boolean
v0.2.0
Published
Boolean path operations for rasam drawings: union, subtract, intersect and exclude.
Readme
@rasamjs/boolean
The optional editor adapter registers boolean commands through structural capabilities, with no editor runtime dependency.
Boolean path operations for rasam drawings: union, subtract, intersect and exclude. The
result keeps curves: arcs and Bézier segments stay curves instead of turning into many
short lines. Geometry comes from PathBool.js
(path-bool), a peer of this package only, so apps that don't use boolean operations
don't download it.
import { combinePaths, combineSelection } from '@rasamjs/boolean';
const square = drawing.rect({ x: 0, y: 0, width: 100, height: 100, fill: '#a5d8ff' });
const circle = drawing.circle({ cx: 100, cy: 50, r: 50, fill: '#ffd8a8' });
const shape = combinePaths([square, circle], 'union'); // one <path>, one undo step
// With a selection (rasam Selection, or anything with nodes and select()):
combineSelection(selection, 'subtract');Operations
| Operation | Result | Style from |
| ----------- | ---------------------------------------- | -------------------- |
| union | Everything covered by any shape | The front-most shape |
| subtract | The back-most shape minus all the others | The back-most shape |
| intersect | Only what every shape covers | The front-most shape |
| exclude | What an odd number of shapes covers | The front-most shape |
Front and back follow document order, so the result matches what you see. The new path
takes the front-most shape's place in its parent. Every transform, including those of
parent groups, is baked into its path data, so it has no transform attribute. Paint and
presentation attributes (fill, stroke, opacity, dashes, class and style) come from the
style source, and the path uses fill-rule="evenodd", which renders the result's holes
correctly.
The operation is one change group labelled "Union", "Subtract", "Intersect" or "Exclude",
so @rasamjs/history records one undo step that restores the original shapes. Pass
prepare to name the result inside the same step. When the result is empty, for example
intersecting shapes that don't overlap, nothing changes and the call returns null.
Shapes
Rectangles (including rounded corners), circles, ellipses, polygons, polylines and paths
are supported; booleanShapes lists them. A polyline counts as its filled area, closed the
way SVG fills it. Text, images, lines and groups are rejected; canCombine(nodes) tells
you whether a set of nodes can be combined. Geometry attributes must be plain numbers, and
an input's fill-rule="evenodd" attribute is honored; CSS fill-rule is not read.
outline(node, space) returns a shape's filled outline as path data in another node's
user space, with transforms applied. It is useful on its own, for example to convert a
shape to a path.
Requirements
@rasamjs/core, and path-bool 1.0.4 or a later 1.x release. PathBool.js describes itself
as early in development; report reduced failure cases upstream. See API.md.
