Two point perspective grid on canvas.

npm install perspective-grid
import { PerspectiveGrid, Point } from "perspective-grid";
import canvasContext from "canvas-context";
const { context, canvas } = canvasContext("2d", {
width: window.innerWidth,
height: window.innerHeight,
});
// Alternatively pass [rows, columns] for a grid with different rows and column units
const grid = new PerspectiveGrid(10);
grid.init(
new Point(300, 380),
new Point(canvas.width - 300, 300),
new Point(canvas.width, canvas.height),
new Point(0, canvas.height)
);
grid.update();
// Operations on lines...
context.save();
grid.drawLines(context);
context.restore();
context.save();
grid.drawSquares(context);
context.restore();
// ...or simply draw
context.save();
grid.draw(context);
context.restore();
LineEquation defines a line equation or vertical
| Param | Type | Description |
|---|---|---|
| lineParams | LineParams |
Line parameters |
| x | number |
X position |
PointGet intersection of two line equation
Kind: instance method of LineEquation
| Param | Type |
|---|---|
| equation | LineEquation |
A faked ENUM for referencing line types.
Kind: Exported constant
| Param | Type |
|---|---|
| VERTICAL | number |
| HORIZONTAL | number |
numbernumbernumbernumberPointnumberObjectObjectnumberPointPointnumberKind: static constant of MathHelper
numberKind: static constant of MathHelper
numberKind: static constant of MathHelper
numberGet the distance between two points
Kind: static method of MathHelper
Returns: number - Distance between point1 and point2
| Param | Type | Description |
|---|---|---|
| point1 | Object |
First point |
| point2 | Object |
Second point |
PointGet the convergence point of two line equation
Kind: static method of MathHelper
| Param | Type |
|---|---|
| m1 | number |
| c1 | number |
| m2 | number |
| c2 | number |
numberGet the vertical convergence point from a X position
Kind: static method of MathHelper
| Param | Type |
|---|---|
| x | number |
| m | number |
| c | number |
ObjectReturn line parameters
Kind: static method of MathHelper
| Param | Type |
|---|---|
| x1 | number |
| y1 | number |
| x2 | number |
| y2 | number |
ObjectGet parallel line parameters
Kind: static method of MathHelper
| Param | Type |
|---|---|
| line | number |
| point | Point |
numberGet the distance to a line parameters
Kind: static method of MathHelper
| Param | Type |
|---|---|
| line | Object |
| point | Point |
PointGet a projected point
Kind: static method of MathHelper
| Param | Type |
|---|---|
| line | Object |
| point | Point |
PointGet the center of four points
Kind: static method of MathHelper
| Param | Type |
|---|---|
| p1 | Point |
| p2 | Point |
| p3 | Point |
| p4 | Point |
Two point perspective grid on canvas.
Note: Does not work correctly when there is only one vanishing point.
Creates an instance of PerspectiveGrid.
| Param | Type | Description |
|---|---|---|
| units | number | Array.<number> |
Number of rows and columns (unit or [rows, columns]). |
| [squares] | Array.<Point> |
Highlighted squares in the grid |
Reset the corners (clockwise starting from top left)
Kind: instance method of PerspectiveGrid
| Param | Type | Description |
|---|---|---|
| tl | Point |
Top left corner |
| tr | Point |
Top right corner |
| br | Point |
Bottom right corner |
| bl | Point |
Bottom left corner |
Draw the grid in the instance context
Kind: instance method of PerspectiveGrid
| Param | Type | Description |
|---|---|---|
| context | CanvasRenderingContext2D |
The context to draw the grid in |
Update grid segments
Kind: instance method of PerspectiveGrid
Array.<Point>Get the four vertices of a point in grid
Kind: instance method of PerspectiveGrid
| Param | Type |
|---|---|
| column | number |
| row | number |
PointGet the center point from grid unit to pixel eg. (1, 1) is the first top left point
Kind: instance method of PerspectiveGrid
| Param | Type |
|---|---|
| column | number |
| row | number |
Actually draw the lines (vertical and horizontal) in the context
Kind: instance method of PerspectiveGrid
| Param | Type | Description |
|---|---|---|
| context | CanvasRenderingContext2D |
The context to draw the grid in |
Draw highlighted squares in the grid
Kind: instance method of PerspectiveGrid
| Param | Type | Description |
|---|---|---|
| context | CanvasRenderingContext2D |
The context to draw the grid in |
Draw a single point in the grid useful for debug purpose
Kind: instance method of PerspectiveGrid
| Param | Type | Description |
|---|---|---|
| context | CanvasRenderingContext2D |
The context to draw the grid in |
| point | Point |
|
| radius | number |
|
| color | string |
ObjectGet a line parallel to the horizon
Kind: instance method of PerspectiveGrid
| Param | Type |
|---|---|
| vVanishing | Point |
| hVanishing | Point |
BooleanAn object that defines a Point
| Param | Type | Description |
|---|---|---|
| x | number |
x coordinate |
| y | number |
y coordinate |
BooleanCheck if a point is in a list of points
Kind: instance method of Point
| Param | Type | Description |
|---|---|---|
| list | Array.<Point> |
Array of Points |
An object with two points that defines a segment
| Param | Type | Description |
|---|---|---|
| p1 | Point |
First point |
| p1 | Point |
Second point |
PointGet intersection of two segments
Kind: instance method of Segment
| Param | Type |
|---|---|
| segment | Segment |
MIT. See license file.