useLine
Hook used to a/multiple chaining line(s).
Configuration
One line can be created either by providing 2 vertices or line length.
Multiple (chaining) lines can be created by providing a list of 3 or more vertices.
(LineConfig | undefined) useLine
accepts following configurations:
Property | Type | Default | Description |
---|---|---|---|
vertices | Vertex[] | [] | Vertices that will form line(s). |
length | number | undefined | undefined | Length of a line. |
Among the configurations, only on of them is needed. If vertices
are provided, it will take precedence over length
and ignore it.
However, at least of the properties must be given for useLine
to work.
extends
Primitive Configuration
(PrimitiveConfig) Base configurations for all primitives.
Property | Type | Default | Description |
---|---|---|---|
scale | number | Vertex | 1 | Scaling factor of the primitive. If Vertex is provided, it will scale x and y dimension accordingly (optional). |
rotate | number | 0 | Rotation of the primitive, in degrees, in clockwise manner (optional). |
position | Vertex | { x: 0, y: 0 } | Moving distance away from the primitive drawing centre. By default, primitive will start drawing in origin (0, 0) (optional). |
scale
, rotate
and position
can be considered as transformation of polygon after drawn, where position
can be considered as transform: translate
of css to the primitive.
The polygon will first be scaled, then rotated and lastly, translated.
Returns
(Primitive) All primitive return the following properties:
Property | Type | Description |
---|---|---|
vertices | Vertex[] | Vertices of the primitive. |
edges | Edge[] | Edges of the primitive. |
faces | Face[] | Faces of the primitive. |
centroid | Vertex | Centroid (geometric centre) of primitive. |
boundingBox | BoundingBox | Space constraint of the primitive, with properties x , y , width and height . |
svgPath | string | SVG path representing the shape of the primitive. |
drawToCanvas | (ctx: CanvasRenderingContext2D) => void | Callback that draws the primitive to a canvas context. |
modifyConfig | (newConfig: Partial<LineConfig>) => void | Callback to update the configuration of the primitive. |
Example
import { useLine } from "react-use-polygon";
// Drawing a line from { x: -25, y: 0 } to { x: 25, y: 0 }
const line = useLine({ length: 50 });
// Drawing a square frame
const lines = useLine({
vertices: [
{ x: -20, y: -20 },
{ x: 20, y: -20 },
{ x: 20, y: 20 },
{ x: -20, y: 20 },
],
});