Skip to main content

Class: Grid

Defined in: packages/blockly/core/grid.ts:23

Class for a workspace's grid.

Methods index​

MethodDescription
setSpacingSets the spacing between the centers of the grid lines.
getSpacingGet the spacing of the grid points (in px).
setLengthSets the length of the grid lines.
getLengthGet the length of the grid lines (in px).
setSnapToGridSets whether blocks should snap to the grid or not.
shouldSnapWhether blocks should snap to the grid.
alignXYGiven a coordinate, return the nearest coordinate aligned to the grid.

Constructors​

Constructor​

new Grid(pattern, options): Grid;

Defined in: packages/blockly/core/grid.ts:37

Parameters​

ParameterTypeDescription
patternSVGElementThe grid's SVG pattern, created during injection.
optionsGridOptionsA dictionary of normalized options for the grid. See grid documentation: https://docs.blockly.com/guides/configure/grid/

Returns​

Grid

Methods​

setSpacing()​

setSpacing(spacing): void;

Defined in: packages/blockly/core/grid.ts:65

Sets the spacing between the centers of the grid lines.

This does not trigger snapping to the newly spaced grid. If you want to snap blocks to the grid programmatically that needs to be triggered on individual top-level blocks. The next time a block is dragged and dropped it will snap to the grid if snapping to the grid is enabled.

Parameters​

ParameterType
spacingnumber

Returns​

void


getSpacing()​

getSpacing(): number;

Defined in: packages/blockly/core/grid.ts:75

Get the spacing of the grid points (in px).

Returns​

number

The spacing of the grid points.


setLength()​

setLength(length): void;

Defined in: packages/blockly/core/grid.ts:80

Sets the length of the grid lines.

Parameters​

ParameterType
lengthnumber

Returns​

void


getLength()​

getLength(): number;

Defined in: packages/blockly/core/grid.ts:86

Get the length of the grid lines (in px).

Returns​

number


setSnapToGrid()​

setSnapToGrid(snap): void;

Defined in: packages/blockly/core/grid.ts:98

Sets whether blocks should snap to the grid or not.

Setting this to true does not trigger snapping. If you want to snap blocks to the grid programmatically that needs to be triggered on individual top-level blocks. The next time a block is dragged and dropped it will snap to the grid.

Parameters​

ParameterType
snapboolean

Returns​

void


shouldSnap()​

shouldSnap(): boolean;

Defined in: packages/blockly/core/grid.ts:107

Whether blocks should snap to the grid.

Returns​

boolean

True if blocks should snap, false otherwise.


alignXY()​

alignXY(xy): Coordinate;

Defined in: packages/blockly/core/grid.ts:195

Given a coordinate, return the nearest coordinate aligned to the grid.

Parameters​

ParameterTypeDescription
xyCoordinateA workspace coordinate.

Returns​

Coordinate

Workspace coordinate of nearest grid point. If there's no change, return the same coordinate object.