Get all the current points on the grid
29 | warning: gets the points array by reference. Changes to individual points will be reflected in the original grid object.
30 | To get a deep copy use grid.copy(). eg. grid.copy.get()
Replaces all the current points on the grid
34 | warning: sets a reference to the provided points. Changes in made by this grid object to the points will be reflected in the provided points array.
Transforms x, y values of points on the grid using the supplied transform function.
51 | control which points are getting affected by supplying a condition
Transforms x, y, z values of points on the grid using the supplied transform function.
66 | control which points are getting affected by supplying a condition
67 |
68 |
69 |
70 |
71 |
72 | ## Grid
73 | **Kind**: global class
74 |
75 |
76 | ### new Grid(cols, rows, width, height, [shape])
77 | The main Grid class containing all a two dimensional array of GridPoints and methods to manipulate the GridPoints on grid.
78 |
79 |
80 | | Param | Type | Description |
81 | | --- | --- | --- |
82 | | cols | number | the amount of columns the grid needs to contain |
83 | | rows | number | the amount of rows the grid needs to contain |
84 | | width | number | the width of the grid |
85 | | height | number | the height of the grid |
86 | | [shape] | [GridShape](#GridShape) | the shape of the grid (RECTANGLE or ELLIPSE). Defaults to a rectangular shaped grid. |
87 |
88 |
89 |
90 | ## Grid3D
91 | **Kind**: global class
92 |
93 |
94 | ### new Grid3D(cols, rows, layers, width, height, depth)
95 | The main Grid class containing all a two dimensional array of GridPoints and methods to manipulate the GridPoints on grid.
96 |
97 |
98 | | Param | Type | Description |
99 | | --- | --- | --- |
100 | | cols | number | the amount of columns the grid needs to contain |
101 | | rows | number | the amount of rows the grid needs to contain |
102 | | layers | number | the amount of layers the grid needs to contain |
103 | | width | number | the width of the grid |
104 | | height | number | the height of the grid |
105 | | depth | number | the depth of the grid |
106 |
107 |
108 |
109 | ## GridPoint
110 | **Kind**: global class
111 | **Properties**
112 |
113 | | Name | Type | Description |
114 | | --- | --- | --- |
115 | | x | number | the x coordinate of the point |
116 | | y | number | the y coordinate of the point |
117 | | z | number | the y coordinate of the point |
118 |
119 |
120 |
121 | ### new GridPoint(x, y, [z])
122 | Represent a single point on the grid.
123 |
124 |
125 | | Param | Type | Description |
126 | | --- | --- | --- |
127 | | x | number | the x coordinate of the point |
128 | | y | number | the y coordinate of the point |
129 | | [z] | number | the y coordinate of the point |
130 |
131 |
132 |
133 | ## createGrid ⇒ [Grid](#Grid)
134 | Create a grid
135 |
136 | **Kind**: global variable
137 |
138 | | Param | Type |
139 | | --- | --- |
140 | | options | GridOptions |
141 |
142 | **Example**
143 | ```js
144 | const grid = createGrid({cols: 3, rows: 5, width 1080, height: 1080});
145 | ```
146 |
147 |
148 | ## createGrid3D ⇒ [Grid3D](#Grid3D)
149 | **Kind**: global variable
150 |
151 | | Param |
152 | | --- |
153 | | options |
154 |
155 | **Example**
156 | ```js
157 | const grid = createGrid({cols: 3, rows: 5, layers: 8 , width 1080, height: 1080, depth: 1080});
158 | ```
159 |
160 |
161 | ## createPoint ⇒ [GridPoint](#GridPoint)
162 | **Kind**: global variable
163 | **Returns**: [GridPoint](#GridPoint) - a point on a the x, y, z plane. Can be set to a specific index on the grid using the `Grid` or `Grid3D` `setPoint` methods
164 |
165 | | Param |
166 | | --- |
167 | | x |
168 | | y |
169 | | z |
170 |
171 | **Example**
172 | ```js
173 | // Create and set a point on a grid
174 | const grid = createGrid({cols:5, rows: 8, width 1920, height: 1080});
175 | const firstPoint = createPoint(-10, -10);
176 | grid.setPoint(0, 0, firstPoint);
177 | ```
178 |
179 |
180 | ## GridShape : enum
181 | Enum used to determine the grid shape in the [Grid](#Grid) constructor.
182 | Values: RECTANGLE or ELLIPSE.
183 |
184 | **Kind**: global enum
185 | **Read only**: true
186 |
187 |
188 | ## getPoints() ⇒ Array.<Array.<GridPoint>>
189 | Get all the current points on the grid
190 | warning: gets the points array by reference. Changes to individual points will be reflected in the original grid object.
191 | To get a deep copy use grid.copy(). eg. grid.copy.get()
192 |
193 | **Kind**: global function
194 |
195 |
196 | ## set() ⇒ void
197 | Replaces all the current points on the grid
198 | warning: sets a reference to the provided points. Changes in made by this grid object to the points will be reflected in the provided points array.
199 |
200 | **Kind**: global function
201 |
202 |
203 | ## getPoint(col, row) ⇒ [GridPoint](#GridPoint)
204 | Gets a point from from indeces [col, row]
205 |
206 | **Kind**: global function
207 |
208 | | Param | Type | Description |
209 | | --- | --- | --- |
210 | | col | number | the col index |
211 | | row | number | the row index |
212 |
213 |
214 |
215 | ## getFlat() ⇒ [Array.<GridPoint>](#GridPoint)
216 | returns a one dimensional array of GridPoints of the grid. One column pushed after the other.
217 |
218 | **Kind**: global function
219 |
220 |
221 | ## ~~draw(func, condition) ⇒ [Grid](#Grid)~~
222 | ***Deprecated***
223 |
224 | Loops over the points in the grid, passing each point to the provided func parameter
225 | Provide a drawing function
226 |
227 | **Kind**: global function
228 | **Returns**: [Grid](#Grid) - returns @this Grid Object. Used for chaining Grid methods
229 |
230 | | Param | Type | Description |
231 | | --- | --- | --- |
232 | | func | GridFunction | a function that handles drawing of each individual point |
233 | | condition | Condition | an optional condition for which points to draw |
234 |
235 |
236 |
237 | ## every(func, condition) ⇒ [Grid](#Grid)
238 | Loops over the points in the grid, passing each point to the provided func parameter
239 |
240 | **Kind**: global function
241 | **Returns**: [Grid](#Grid) - returns @this Grid Object. Used for chaining Grid methods
242 |
243 | | Param | Type | Description |
244 | | --- | --- | --- |
245 | | func | GridFunction | a function to access each point and row/col indices |
246 | | condition | Condition | an optional condition for which points to execute func over |
247 |
248 |
249 |
250 | ## transform(func, condition) ⇒ [Grid](#Grid)
251 | Transforms x, y values of points on the grid using the supplied transform function.
252 | control which points are getting affected by supplying a condition
253 |
254 | **Kind**: global function
255 | **Returns**: [Grid](#Grid) - returns @this Grid Object. Used for chaining Grid methods
256 |
257 | * @example
258 | // Translates the grid on the x-axis by 5 and on the y-axis by 8
259 | grid.transform((point) => {
260 | point.x += 5;
261 | point.y += 8;
262 | return point; // Make sure to return the transformed point
263 | })
264 |
265 | | Param | Type | Description |
266 | | --- | --- | --- |
267 | | func | TransformFunction | a function to transform the point's x, y values. Must return the transformed point. |
268 | | condition | Condition | an optional condition for which points to be affected |
269 |
270 |
271 |
272 | ## translate(x, y, [condition]) ⇒ [Grid](#Grid)
273 | Translates the entire grid by x en y coordinates
274 |
275 | **Kind**: global function
276 | **Returns**: [Grid](#Grid) - returns @this Grid Object. Used for chaining Grid methods
277 |
278 | | Param | Type | Description |
279 | | --- | --- | --- |
280 | | x | number | the x coordinates to translate the points with |
281 | | y | number | the y coordinates to translate the points with |
282 | | [condition] | Condition | an optional condition for which points to translate |
283 |
284 |
285 |
286 | ## copy() ⇒ [Grid](#Grid)
287 | Creates a deep copy of the current grid object
288 |
289 | **Kind**: global function
290 | **Returns**: [Grid](#Grid) - a new instance of Grid of with the same coordinate values as @this Grid
291 |
292 |
293 | ## setPoint(col, row, layer, point)
294 | **Kind**: global function
295 |
296 | | Param |
297 | | --- |
298 | | col |
299 | | row |
300 | | layer |
301 | | point |
302 |
303 | **Example**
304 | ```js
305 | // Create and set a point on a grid
306 | const grid = createGrid({cols: 3, rows: 5, layers: 8 , width 1080, height: 1080, depth: 1080});
307 | const firstPoint = createPoint(-540, -540, -540);
308 | grid.setPoint(0, 0, 0, firstPoint);
309 | ```
310 |
311 |
312 | ## every(func, condition) ⇒ [Grid3D](#Grid3D)
313 | Loops over the points in the grid, passing each point to the provided func parameter
314 |
315 | **Kind**: global function
316 | **Returns**: [Grid3D](#Grid3D) - returns @this Grid3D Object. Used for chaining Grid methods
317 |
318 | | Param | Type | Description |
319 | | --- | --- | --- |
320 | | func | GridFunction | a function to access each point and row/col indices |
321 | | condition | Condition | an optional condition for which points to execute func over |
322 |
323 | **Example**
324 | ```js
325 | // draw a spheres on the grid where the size depends on the position on the grid
326 | // sphere is a pseudo function to draw spheres to a html canvas.
327 | grid.every((point, col, row, layer) => {
328 | const radius = (col + row + layer + 1) * 5;
329 | sphere(point.x, point.y, point.z, radius);
330 | })
331 | ```
332 |
333 |
334 | ## transform(func, condition) ⇒ [Grid3D](#Grid3D)
335 | Transforms x, y, z values of points on the grid using the supplied transform function.
336 | control which points are getting affected by supplying a condition
337 |
338 | **Kind**: global function
339 | **Returns**: [Grid3D](#Grid3D) - returns @this Grid3D Object. Used for chaining Grid methods
340 |
341 | | Param | Type | Description |
342 | | --- | --- | --- |
343 | | func | TransformFunction | a function to transform the point's x, y values. **Must return the transformed point.** |
344 | | condition | Condition | an optional condition for which points to be affected |
345 |
346 | **Example**
347 | ```js
348 | // Translates the grid on the y-axis by 10 and on the z-axis by 5
349 | grid.transform((point) => {
350 | point.y += 10;
351 | point.z += 5;
352 | return point; // Make sure to return the transformed point
353 | })
354 | ```
355 |
--------------------------------------------------------------------------------
/api-docs/generate.js:
--------------------------------------------------------------------------------
1 | // import exec method from child_process module
2 | const { execSync } = require("child_process");
3 | const jsdoc2md = require('jsdoc-to-markdown')
4 | const fs = require('fs');
5 |
6 | // tsc throws error on linting issues, don't break the script
7 | try {
8 | execSync("tsc src/grid.ts src/grid.3d.ts src/grid-point.ts --outDir api-docs");
9 | } catch (err) {
10 |
11 | }
12 |
13 | // Jsdocs to Markdown
14 | const md = jsdoc2md.renderSync({ files: ["api-docs/grid.js", "api-docs/grid.3d.js", "api-docs/grid-point.js"], configure: "api-docs/jsdocs.json" });
15 |
16 | // Output
17 | fs.writeFileSync('api-docs/api.md', md);
18 |
19 | // Cleanup
20 | execSync("rm api-docs/grid.js api-docs/conditions.js api-docs/globals.js");
21 |
--------------------------------------------------------------------------------
/api-docs/jsdocs.json:
--------------------------------------------------------------------------------
1 | {
2 |
3 | }
--------------------------------------------------------------------------------
/api-docs/test.md:
--------------------------------------------------------------------------------
1 |
4 |
5 | ## Instantiates a new 2 Dimensional Grid.
6 |
7 | The horizontal distance between each column: width / (cols - 1)
8 | The vertical distance between each row : height / (rows - 1)
9 | **Kind**: global class
10 |
13 |
14 | ### new Instantiates a new 2 Dimensional Grid.
15 |
16 | The horizontal distance between each column: width / (cols - 1)
17 | The vertical distance between each row : height / (rows - 1)(cols, rows, width, height)
18 |
19 | | Param |
20 | | ------ |
21 | | cols |
22 | | rows |
23 | | width |
24 | | height |
25 |
--------------------------------------------------------------------------------
/api-docs/wiki-examples/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | P5 Typescript Example Project
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/api-docs/wiki-examples/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "node-typescript-p5",
3 | "version": "1.0.0",
4 | "description": "",
5 | "main": "dist/index.js",
6 | "scripts": {
7 | "start": "webpack serve --open --mode development ",
8 | "test": "echo \"Error: no test specified\" && exit 1"
9 | },
10 | "author": "",
11 | "license": "MIT",
12 | "dependencies": {
13 | "p5": "^1.4.0",
14 | "pretty-grid": "^0.0.10"
15 | },
16 | "devDependencies": {
17 | "@types/p5": "^1.3.3",
18 | "typescript": "^4.5.4",
19 | "webpack": "^5.49.0",
20 | "webpack-cli": "^4.7.2",
21 | "webpack-dev-server": "^3.11.2",
22 | "html-webpack-plugin": "^5.3.2",
23 | "ts-loader": "^9.2.5"
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/api-docs/wiki-examples/src/index.ts:
--------------------------------------------------------------------------------
1 | import { Grid, evenRows, and, evenCols, oddRows, oddCols, odd, GridShape } from 'pretty-grid';
2 | import p5 from 'p5';
3 |
4 | const s = (p: p5) => {
5 | const CANVAS_WIDTH = 250;
6 | const CANVAS_HEIGHT = 250;
7 | let exportName = 'simple';
8 |
9 | const grid = new Grid(5, 8, CANVAS_WIDTH, CANVAS_HEIGHT);
10 |
11 | p.setup = () => {
12 | const canvas = p.createCanvas(CANVAS_WIDTH, CANVAS_HEIGHT);
13 | canvas.parent('sketch');
14 | p.noLoop();
15 | };
16 |
17 | p.draw = () => {
18 | p.background(32);
19 | p.ellipseMode(p.CENTER);
20 |
21 | // Drawing
22 |
23 | // Translating
24 | /*
25 | exportName = "translate";
26 | grid.draw(point => whiteDot(point.x, point.y));
27 | grid.translate(10, 20);
28 | grid.draw(point => orangeCircle(point.x, point.y));
29 | */
30 | // Chaining
31 |
32 | // Conditions
33 | /*
34 | exportName = "conditions-evenRows";
35 | grid.draw(point => whiteDot(point.x, point.y));
36 | */
37 | //grid.draw(point => orangeCircle(point.x, point.y), evenRows());
38 |
39 | // Operators
40 | /*
41 | exportName = "operators";
42 | grid.draw(point => whiteDot(point.x, point.y));
43 | grid.draw(point => orangeCircle(point.x, point.y), and(evenCols(), oddRows()));
44 | */
45 |
46 | // introduction
47 | /*
48 | exportName = "intro";
49 | const introGrid = new Grid(20, 10, 500, 500);
50 | introGrid.draw(point => whiteDot(point.x, point.y));
51 | introGrid.draw(point => orangeCircle(point.x, point.y), and(oddRows(), oddCols()));
52 | introGrid.translate(10,10)
53 | introGrid.draw(point => blueDot(point.x, point.y), evenRows());
54 | */
55 |
56 | // vanilla js
57 | /*
58 | exportName = "vanillajs"
59 | // initialize grid
60 | const COLS_AMOUNT = 5;
61 | const ROWS_AMOUNT = 8;
62 | const GRID_WIDTH = 500;
63 | const GRID_HEIGHT = 500;
64 |
65 | const COLS_DISTANCE = GRID_WIDTH / (COLS_AMOUNT - 1);
66 | const ROWS_DISTANCE = GRID_HEIGHT/ (ROWS_AMOUNT - 1);
67 | const points = [];
68 |
69 | for (let i = 0; i < COLS_AMOUNT; i++) {
70 | points[i] = [];
71 | for (let j = 0; j < ROWS_AMOUNT; j++) {
72 | points[i][j] = {
73 | x : i * COLS_DISTANCE,
74 | y : j * ROWS_DISTANCE
75 | };
76 | }
77 | }
78 | */
79 |
80 | // Ellipse grid
81 |
82 | exportName = 'ellipse-grid';
83 |
84 | const ellipseGrid = new Grid(16, 8, CANVAS_WIDTH, CANVAS_WIDTH, GridShape.ELLIPSE);
85 | ellipseGrid.translate(CANVAS_WIDTH / 2, CANVAS_HEIGHT / 2);
86 | ellipseGrid.draw((point) => whiteDot(point.x, point.y));
87 |
88 | p.saveCanvas(exportName, 'png');
89 | };
90 |
91 | const orangeDot = (x: number, y: number) => {
92 | drawDot(x, y, '#ff6700');
93 | };
94 |
95 | const blueDot = (x: number, y: number) => {
96 | drawDot(x, y, '#0000ff');
97 | };
98 |
99 | const whiteDot = (x: number, y: number) => {
100 | drawDot(x, y, '#fff');
101 | };
102 |
103 | const drawDot = (x: number, y: number, color: string) => {
104 | p.fill(color);
105 | p.circle(x, y, 5);
106 | };
107 |
108 | const orangeCircle = (x: number, y: number) => {
109 | p.push();
110 | p.noFill();
111 | p.stroke('#ff6700');
112 | p.strokeWeight(3);
113 | p.circle(x, y, 20);
114 | p.pop();
115 | };
116 | };
117 |
118 | // eslint-disable-next-line @typescript-eslint/no-unused-vars
119 | const myP5 = new p5(s);
120 |
--------------------------------------------------------------------------------
/api-docs/wiki-examples/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "module": "commonjs",
4 | "target": "es2015",
5 | "declaration": true,
6 | "outDir": "./dist",
7 | "esModuleInterop": true
8 | },
9 | "include": [
10 | "src/**/*"
11 | ]
12 | }
--------------------------------------------------------------------------------
/api-docs/wiki-examples/webpack.config.js:
--------------------------------------------------------------------------------
1 | const path = require('path');
2 | const HtmlWebpackPlugin = require('html-webpack-plugin');
3 |
4 |
5 | module.exports = {
6 | entry: './src/index.ts',
7 | plugins: [
8 | new HtmlWebpackPlugin({
9 | template: 'index.html'
10 | }),
11 | ],
12 | module: {
13 | rules: [
14 | {
15 | test: /\.ts$/,
16 | use: 'ts-loader',
17 | exclude: /node_modules/,
18 | },
19 | ],
20 | },
21 | resolve: {
22 | extensions: ['.ts', '.js'],
23 | },
24 | output: {
25 | filename: 'bundle.js',
26 | path: path.resolve(__dirname, 'dist'),
27 | clean: true,
28 | },
29 | // TODO: add images, fonts, data loaders https://webpack.js.org/guides/asset-management/#loading-images
30 | };
--------------------------------------------------------------------------------
/assets/banner.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/VadimGouskov/pretty-grid/28fa3e168d15d6a31fcaef3cd9acc7316c3ab33d/assets/banner.png
--------------------------------------------------------------------------------
/assets/banner2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/VadimGouskov/pretty-grid/28fa3e168d15d6a31fcaef3cd9acc7316c3ab33d/assets/banner2.png
--------------------------------------------------------------------------------
/assets/intro.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/VadimGouskov/pretty-grid/28fa3e168d15d6a31fcaef3cd9acc7316c3ab33d/assets/intro.png
--------------------------------------------------------------------------------
/dist/conditions.d.ts:
--------------------------------------------------------------------------------
1 | import { GridPoint } from "./grid-point";
2 | export declare type Condition = (point: GridPoint, col?: number, row?: number, layer?: number) => boolean;
3 | export declare type ConditionCreator = (...args: any[]) => Condition;
4 | export declare const all: ConditionCreator;
5 | export declare const even: ConditionCreator;
6 | export declare const odd: ConditionCreator;
7 | export declare const evenCols: ConditionCreator;
8 | export declare const oddCols: ConditionCreator;
9 | export declare const evenRows: ConditionCreator;
10 | export declare const oddRows: ConditionCreator;
11 | export declare const rows: (start: number, end: number) => Condition;
12 | export declare const cols: (start: number, end: number) => Condition;
13 |
--------------------------------------------------------------------------------
/dist/gird.function.d.ts:
--------------------------------------------------------------------------------
1 | import { GridPoint } from "./grid-point";
2 | export declare type GridFunction = (point: GridPoint, col?: number, row?: number, layer?: number) => void;
3 | export declare type TransformFunction = (point: GridPoint, col?: number, row?: number, layer?: number) => GridPoint;
4 |
--------------------------------------------------------------------------------
/dist/globals.d.ts:
--------------------------------------------------------------------------------
1 | export declare type ShapeOrigin = "top-left" | "center";
2 | /**
3 | * Sets the origin mode for all rectangular grids that are initialized after calling this function. Default is CORNER.
4 | * @param {ShapeOrigin} origin The new origin for rectangle grids
5 | */
6 | export declare const rectangleShapeOrigin: (origin: ShapeOrigin) => void;
7 | /**
8 | * Sets the origin mode for all ellipse grids that are initialized after calling this function. Default is CENTER.
9 | * @param {ShapeOrigin} origin the The new origin for ellipse grids
10 | */
11 | export declare const ellipseShapeOrigin: (origin: ShapeOrigin) => void;
12 | /**
13 | * Sets the origin mode for all ellipse grids that are initialized after calling this function. Default is CENTER.
14 | * @param {ShapeOrigin} origin the The new origin for ellipse grids
15 | */
16 | export declare const cuboidShapeOrigin: (origin: ShapeOrigin) => void;
17 | export declare type ShapeOrigins = {
18 | Rectangle: ShapeOrigin;
19 | Ellipse: ShapeOrigin;
20 | Cuboid: ShapeOrigin;
21 | };
22 | export declare const shapeOrigins: ShapeOrigins;
23 |
--------------------------------------------------------------------------------
/dist/grid-init.d.ts:
--------------------------------------------------------------------------------
1 | import { GridPoint } from "./grid-point";
2 | export declare function initRectangleGrid(cols: number, rows: number, width: number, height: number, depth?: number): GridPoint[][];
3 | export declare const initEllipseGrid: (cols: number, rows: number, width: number, height: number, depth?: number) => GridPoint[][];
4 |
--------------------------------------------------------------------------------
/dist/grid-point.d.ts:
--------------------------------------------------------------------------------
1 | export interface GridPointInterface {
2 | x: number;
3 | y: number;
4 | z?: number;
5 | }
6 | /**
7 | * Represent a single point on the grid.
8 | * @class
9 | * @name GridPoint
10 | * @param {number} x the x coordinate of the point
11 | * @param {number} y the y coordinate of the point
12 | * @param {number} [z] the y coordinate of the point
13 | * @property {number} x the x coordinate of the point
14 | * @property {number} y the y coordinate of the point
15 | * @property {number} z the y coordinate of the point
16 | */
17 | export declare class GridPoint implements GridPointInterface {
18 | x: number;
19 | y: number;
20 | z: number;
21 | constructor(x: number, y: number, z?: number);
22 | }
23 | /**
24 | * @name createPoint
25 | * @param x
26 | * @param y
27 | * @param z
28 | * @returns {GridPoint} a point on a the x, y, z plane. Can be set to a specific index on the grid using the `Grid` or `Grid3D` `setPoint` methods
29 | *
30 | * @example
31 | * // Create and set a point on a grid
32 | * const grid = createGrid({cols:5, rows: 8, width 1920, height: 1080});
33 | * const firstPoint = createPoint(-10, -10);
34 | * grid.setPoint(0, 0, firstPoint);
35 | */
36 | export declare const createPoint: (x: number, y: number, z?: number) => GridPoint;
37 |
--------------------------------------------------------------------------------
/dist/grid.3d.d.ts:
--------------------------------------------------------------------------------
1 | import { Condition } from "./conditions";
2 | import { GridFunction, TransformFunction } from "./grid.function";
3 | import { GridPoint } from "./grid-point";
4 | declare type Points3D = GridPoint[][][];
5 | interface Grid3DInterface {
6 | getPoints: () => Points3D;
7 | getPoint: (col: number, row: number, layer: number) => GridPoint;
8 | setPoint: (col: number, row: number, layer: number, point: GridPoint) => void;
9 | every: (func: GridFunction, condition?: Condition) => Grid3D;
10 | translate: (x: number, y: number, z: number, condition?: Condition) => Grid3D;
11 | }
12 | declare type Grid3DOptions = {
13 | cols: number;
14 | rows: number;
15 | layers: number;
16 | width: number;
17 | height: number;
18 | depth: number;
19 | };
20 | /**
21 | * The main Grid class containing all a two dimensional array of GridPoints and methods to manipulate the GridPoints on grid.
22 | * @class
23 | * @name Grid3D
24 | * @param {number} cols the amount of columns the grid needs to contain
25 | * @param {number} rows the amount of rows the grid needs to contain
26 | * @param {number} layers the amount of layers the grid needs to contain
27 | * @param {number} width the width of the grid
28 | * @param {number} height the height of the grid
29 | * @param {number} depth the depth of the grid
30 | */
31 | export declare class Grid3D implements Grid3DInterface {
32 | private points;
33 | /**
34 | *
35 | * @param {Grid3DOptions} options
36 | */
37 | constructor(options: Grid3DOptions);
38 | getPoints(): GridPoint[][][];
39 | getPoint(col: number, row: number, layer: number): GridPoint;
40 | /**
41 | * @method
42 | * @name setPoint
43 | * @param col
44 | * @param row
45 | * @param layer
46 | * @param point
47 | *
48 | * @example
49 | * // Create and set a point on a grid
50 | * const grid = createGrid({cols: 3, rows: 5, layers: 8 , width 1080, height: 1080, depth: 1080});
51 | * const firstPoint = createPoint(-540, -540, -540);
52 | * grid.setPoint(0, 0, 0, firstPoint);
53 | */
54 | setPoint(col: number, row: number, layer: number, point: GridPoint): void;
55 | /**
56 | * Loops over the points in the grid, passing each point to the provided func parameter
57 | * @method
58 | * @name every
59 | * @param {GridFunction} func - a function to access each point and row/col indices
60 | * @param {Condition} condition - an optional condition for which points to execute func over
61 | * @returns { Grid3D } returns @this Grid3D Object. Used for chaining Grid methods
62 | *
63 | * @example
64 | * // draw a spheres on the grid where the size depends on the position on the grid
65 | * // sphere is a pseudo function to draw spheres to a html canvas.
66 | * grid.every((point, col, row, layer) => {
67 | * const radius = (col + row + layer + 1) * 5;
68 | * sphere(point.x, point.y, point.z, radius);
69 | * })
70 | */
71 | every(func: GridFunction, condition?: Condition): Grid3D;
72 | /**
73 | * Transforms x, y, z values of points on the grid using the supplied transform function.
74 | * control which points are getting affected by supplying a condition
75 | *
76 | * @method
77 | * @name transform
78 | * @param {TransformFunction} func - a function to transform the point's x, y values. **Must return the transformed point.**
79 | * @param {Condition} condition - an optional condition for which points to be affected
80 | * @returns { Grid3D } returns @this Grid3D Object. Used for chaining Grid methods
81 | *
82 | * @example
83 | * // Translates the grid on the y-axis by 10 and on the z-axis by 5
84 | * grid.transform((point) => {
85 | * point.y += 10;
86 | * point.z += 5;
87 | * return point; // Make sure to return the transformed point
88 | * })
89 | *
90 | */
91 | transform(func: TransformFunction, condition?: Condition): Grid3D;
92 | translate(x: number, y: number, z: number, condition?: Condition): this;
93 | }
94 | /**
95 | * @name createGrid3D
96 | * @param options
97 | * @returns {Grid3D}
98 | *
99 | * @example
100 | * const grid = createGrid({cols: 3, rows: 5, layers: 8 , width 1080, height: 1080, depth: 1080});
101 | */
102 | export declare const createGrid3D: (options: Grid3DOptions) => Grid3D;
103 | export {};
104 |
--------------------------------------------------------------------------------
/dist/grid.d.ts:
--------------------------------------------------------------------------------
1 | import { Condition } from './conditions';
2 | import { GridFunction, TransformFunction } from './grid.function';
3 | import { GridPoint } from './grid-point';
4 | /**
5 | * Enum used to determine the grid shape in the [Grid]{@link #Grid} constructor.
6 | * Values: RECTANGLE or ELLIPSE.
7 | * @readonly
8 | * @enum {number}
9 | */
10 | export declare enum GridShape {
11 | RECTANGLE = 0,
12 | ELLIPSE = 1
13 | }
14 | /**
15 | * The main Grid class containing all a two dimensional array of GridPoints and methods to manipulate the GridPoints on grid.
16 | * @class
17 | * @name Grid
18 | * @param {number} cols the amount of columns the grid needs to contain
19 | * @param {number} rows the amount of rows the grid needs to contain
20 | * @param {number} width the width of the grid
21 | * @param {number} height the height of the grid
22 | * @param {GridShape} [shape] the shape of the grid (RECTANGLE or ELLIPSE). Defaults to a rectangular shaped grid.
23 | */
24 | export declare class Grid {
25 | private points;
26 | /**
27 | * Instantiates a new 2 Dimensional Grid.
28 | * The horizontal distance between each column: width / (cols - 1)
29 | * The vertical distance between each row : height / (rows - 1)
30 |
31 | */
32 | constructor(cols: number, rows: number, width: number, height: number, shape?: GridShape);
33 | /**
34 | * Get all the current points on the grid
35 | * warning: gets the points array by reference. Changes to individual points will be reflected in the original grid object.
36 | * To get a deep copy use grid.copy(). eg. grid.copy.get()
37 | *
38 | * @method
39 | * @name getPoints
40 | * @returns {GridPoint[][]}
41 | *
42 |
43 | */
44 | getPoints(): GridPoint[][];
45 | /**
46 | * Replaces all the current points on the grid
47 | * warning: sets a reference to the provided points. Changes in made by this grid object to the points will be reflected in the provided points array.
48 | * @method
49 | * @name set
50 | *
51 | * @returns {void}
52 | */
53 | set(points: GridPoint[][]): void;
54 | /**
55 | * Gets a point from from indeces [col, row]
56 | * @method
57 | * @name getPoint
58 | * @param {number} col - the col index
59 | * @param {number} row - the row index
60 | * @returns {GridPoint}
61 | */
62 | getPoint(col: number, row: number): GridPoint;
63 | /**
64 | * returns a one dimensional array of GridPoints of the grid. One column pushed after the other.
65 | * @method
66 | * @name getFlat
67 | * @type {GridPoint[]}
68 | */
69 | getFlat(): GridPoint[];
70 | /**
71 | * Loops over the points in the grid, passing each point to the provided func parameter
72 | * Provide a drawing function
73 | * @deprecated
74 | * @method
75 | * @name draw
76 | * @param {GridFunction} func - a function that handles drawing of each individual point
77 | * @param {Condition} condition - an optional condition for which points to draw
78 | * @returns { Grid } returns @this Grid Object. Used for chaining Grid methods
79 | */
80 | draw(func: GridFunction, condition?: Condition): Grid;
81 | /**
82 | * Loops over the points in the grid, passing each point to the provided func parameter
83 | * @method
84 | * @name every
85 | * @param {GridFunction} func - a function to access each point and row/col indices
86 | * @param {Condition} condition - an optional condition for which points to execute func over
87 | * @returns { Grid } returns @this Grid Object. Used for chaining Grid methods
88 | */
89 | every(func: GridFunction, condition?: Condition): Grid;
90 | /**
91 | * Transforms x, y values of points on the grid using the supplied transform function.
92 | * control which points are getting affected by supplying a condition
93 | *
94 | * @method
95 | * @name transform
96 | * @param {TransformFunction} func - a function to transform the point's x, y values. Must return the transformed point.
97 | * @param {Condition} condition - an optional condition for which points to be affected
98 | * @returns { Grid } returns @this Grid Object. Used for chaining Grid methods
99 | *
100 | * * @example
101 | * // Translates the grid on the x-axis by 5 and on the y-axis by 8
102 | * grid.transform((point) => {
103 | * point.x += 5;
104 | * point.y += 8;
105 | * return point; // Make sure to return the transformed point
106 | * })
107 | */
108 | transform(func: TransformFunction, condition?: Condition): Grid;
109 | /**
110 | * Translates the entire grid by x en y coordinates
111 | * @method
112 | * @name translate
113 | * @param {number} x - the x coordinates to translate the points with
114 | * @param {number} y - the y coordinates to translate the points with
115 | * @param {Condition} [condition] - an optional condition for which points to translate
116 | *
117 | * @returns { Grid } returns @this Grid Object. Used for chaining Grid methods
118 | */
119 | translate(x: number, y: number, condition?: Condition): Grid;
120 | /**
121 | * Creates a deep copy of the current grid object
122 | * @method
123 | * @name copy
124 | * @returns { Grid } a new instance of Grid of with the same coordinate values as @this Grid
125 | */
126 | copy(): Grid;
127 | }
128 | /**
129 | * Options to configure a grid
130 | * @typedef GridOptions
131 | * @property {number} cols
132 | * @property {number} rows
133 | * @property {number} width
134 | * @property {number} height
135 | * @property {GridShape} [shape]
136 | */
137 | declare type GridOptionsType = {
138 | cols: number;
139 | rows: number;
140 | width: number;
141 | height: number;
142 | shape?: GridShape;
143 | };
144 | /**
145 | * Create a grid
146 | *
147 | * @name createGrid
148 | * @param {GridOptions} options
149 | * @returns {Grid}
150 | *
151 | * @example
152 | * const grid = createGrid({cols: 3, rows: 5, width 1080, height: 1080});
153 | */
154 | export declare const createGrid: (options: GridOptionsType) => Grid;
155 | export {};
156 |
--------------------------------------------------------------------------------
/dist/grid.function.d.ts:
--------------------------------------------------------------------------------
1 | import { GridPoint } from "./grid-point";
2 | export declare type GridFunction = (point: GridPoint, col?: number, row?: number, layer?: number) => void;
3 | export declare type TransformFunction = (point: GridPoint, col?: number, row?: number, layer?: number) => GridPoint;
4 |
--------------------------------------------------------------------------------
/dist/operators.d.ts:
--------------------------------------------------------------------------------
1 | import { Condition } from './conditions';
2 | export declare type Operator = (...conditions: Condition[]) => Condition;
3 | export declare const and: Operator;
4 | export declare const or: Operator;
5 | export declare const not: Operator;
6 |
--------------------------------------------------------------------------------
/dist/pretty-grid.d.ts:
--------------------------------------------------------------------------------
1 | export { createGrid, Grid, GridShape } from './grid';
2 | export { createGrid3D, Grid3D } from "./grid.3d";
3 | export { Condition, ConditionCreator, even, odd, evenCols, oddCols, evenRows, oddRows, rows, cols } from './conditions';
4 | export { and, or, not } from './operators';
5 | export { ellipseShapeOrigin, rectangleShapeOrigin, ShapeOrigin } from './globals';
6 | export { createPoint, GridPoint } from "./grid-point";
7 |
--------------------------------------------------------------------------------
/examples/browser-simple-p5/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/examples/browser-simple-p5/sketch.js:
--------------------------------------------------------------------------------
1 | console.log(this)
2 |
3 | const grid = new prettyGrid.Grid(5, 5, 500, 500);
4 |
5 | function setup() {
6 | createCanvas(500, 500);
7 | }
8 |
9 | function draw() {
10 | background(32);
11 | fill('yellow');
12 | grid.draw(point => circle(point.x, point.y, 20));
13 | }
--------------------------------------------------------------------------------
/examples/node-typescript-p5/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | P5 Typescript Example Project
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/examples/node-typescript-p5/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "node-typescript-p5",
3 | "version": "1.0.0",
4 | "description": "",
5 | "main": "dist/index.js",
6 | "scripts": {
7 | "start": "webpack serve --open --mode development ",
8 | "test": "echo \"Error: no test specified\" && exit 1"
9 | },
10 | "author": "",
11 | "license": "MIT",
12 | "dependencies": {
13 | "p5": "^1.4.0",
14 | "pretty-grid": "^0.0.8"
15 | },
16 | "devDependencies": {
17 | "@types/p5": "^1.3.3",
18 | "html-webpack-plugin": "^5.3.2",
19 | "ts-loader": "^9.2.5",
20 | "typescript": "^4.5.4",
21 | "webpack": "^5.49.0",
22 | "webpack-cli": "^4.7.2",
23 | "webpack-dev-server": "^3.11.2"
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/examples/node-typescript-p5/src/index.ts:
--------------------------------------------------------------------------------
1 | import { Grid } from 'pretty-grid';
2 | import p5 from 'p5';
3 |
4 | const s = (p: p5) => {
5 | const CANVAS_WIDTH = 500;
6 | const CANVAS_HEIGHT = 500;
7 |
8 | const grid = new Grid(5, 10, CANVAS_WIDTH, CANVAS_HEIGHT);
9 |
10 | p.setup = () => {
11 | const canvas = p.createCanvas(CANVAS_WIDTH, CANVAS_HEIGHT);
12 | canvas.parent('sketch');
13 | };
14 |
15 | p.draw = () => {
16 | p.background(32);
17 | p.fill('hotpink');
18 | p.ellipseMode(p.CENTER);
19 |
20 | // Drawing the main grid
21 | grid.draw((point) => {
22 | p.circle(point.x, point.y, 10);
23 | });
24 | };
25 | };
26 |
27 | // eslint-disable-next-line @typescript-eslint/no-unused-vars
28 | const myP5 = new p5(s);
29 |
--------------------------------------------------------------------------------
/examples/node-typescript-p5/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "module": "commonjs",
4 | "target": "es2015",
5 | "declaration": true,
6 | "outDir": "./dist",
7 | "esModuleInterop": true
8 | },
9 | "include": [
10 | "src/**/*"
11 | ]
12 | }
--------------------------------------------------------------------------------
/examples/node-typescript-p5/webpack.config.js:
--------------------------------------------------------------------------------
1 | const path = require('path');
2 | const HtmlWebpackPlugin = require('html-webpack-plugin');
3 |
4 |
5 | module.exports = {
6 | entry: './src/index.ts',
7 | plugins: [
8 | new HtmlWebpackPlugin({
9 | template: 'index.html'
10 | }),
11 | ],
12 | module: {
13 | rules: [
14 | {
15 | test: /\.ts$/,
16 | use: 'ts-loader',
17 | exclude: /node_modules/,
18 | },
19 | ],
20 | },
21 | resolve: {
22 | extensions: ['.ts', '.js'],
23 | },
24 | output: {
25 | filename: 'bundle.js',
26 | path: path.resolve(__dirname, 'dist'),
27 | clean: true,
28 | },
29 | // TODO: add images, fonts, data loaders https://webpack.js.org/guides/asset-management/#loading-images
30 | };
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "pretty-grid",
3 | "version": "0.0.15",
4 | "description": "Grid creation and manipulation made pretty.",
5 | "author": "Vadim Gouskov",
6 | "homepage": "https://prettygrid.vadimgouskov.com",
7 | "repository": {
8 | "type": "git",
9 | "url": "https://github.com/VadimGouskov/pretty-grid"
10 | },
11 | "bugs": {
12 | "url": "https://github.com/VadimGouskov/pretty-grid/issues"
13 | },
14 | "main": "dist/pretty-grid.js",
15 | "browser": "dist/pretty-grid.js",
16 | "types": "dist/pretty-grid.d.ts",
17 | "files": [
18 | "/dist"
19 | ],
20 | "scripts": {
21 | "build": "webpack",
22 | "test": "jest",
23 | "publish:safe": "npm run test && npm publish",
24 | "api-docs": "node api-docs/generate.js"
25 | },
26 | "keywords": [
27 | "grid",
28 | "create",
29 | "canvas",
30 | "pgrid",
31 | "prettygrid",
32 | "typescript",
33 | "javascript",
34 | "ts",
35 | "shape",
36 | "ellipse",
37 | "circular"
38 | ],
39 | "license": "MIT",
40 | "devDependencies": {
41 | "jest": "^27.4.7",
42 | "jsdoc-to-markdown": "^7.1.0",
43 | "prettier": "2.5.1",
44 | "ts-loader": "^9.2.6",
45 | "typescript": "^4.5.4",
46 | "webpack": "^5.65.0",
47 | "webpack-cli": "^4.9.1"
48 | }
49 | }
--------------------------------------------------------------------------------
/src/conditions.ts:
--------------------------------------------------------------------------------
1 | import { GridPoint } from "./grid-point";
2 |
3 | export type Condition = (point: GridPoint, col?: number, row?: number, layer?: number) => boolean;
4 |
5 | export type ConditionCreator = (...args: any[]) => Condition;
6 |
7 | export const all: ConditionCreator = (): Condition => (_, __, ___) => true;
8 |
9 | export const even: ConditionCreator = (): Condition => (_, col, row) => col % 2 === 0 && row % 2 === 0;
10 |
11 | export const odd: ConditionCreator = (): Condition => (_, col, row) => col % 2 !== 0 && row % 2 !== 0;
12 |
13 | export const evenCols: ConditionCreator = (): Condition => (_, col, __) => col % 2 === 0;
14 |
15 | export const oddCols: ConditionCreator = (): Condition => (_, col, __) => col % 2 !== 0;
16 |
17 | export const evenRows: ConditionCreator = (): Condition => (_, __, row) => row % 2 === 0;
18 |
19 | export const oddRows: ConditionCreator = (): Condition => (_, __, row) => row % 2 !== 0;
20 |
21 | export const rows =
22 | (start: number, end: number): Condition =>
23 | (_, __, row) => {
24 | return row >= start && row <= end;
25 | };
26 |
27 | export const cols =
28 | (start: number, end: number): Condition =>
29 | (_, col, __) => {
30 | return col >= start && col <= end;
31 | };
32 |
--------------------------------------------------------------------------------
/src/globals.ts:
--------------------------------------------------------------------------------
1 |
2 | export type ShapeOrigin = "top-left" | "center"
3 | /**
4 | * Sets the origin mode for all rectangular grids that are initialized after calling this function. Default is CORNER.
5 | * @param {ShapeOrigin} origin The new origin for rectangle grids
6 | */
7 | export const rectangleShapeOrigin = (origin: ShapeOrigin) => {
8 | shapeOrigins.Rectangle = origin;
9 | };
10 |
11 | /**
12 | * Sets the origin mode for all ellipse grids that are initialized after calling this function. Default is CENTER.
13 | * @param {ShapeOrigin} origin the The new origin for ellipse grids
14 | */
15 | export const ellipseShapeOrigin = (origin: ShapeOrigin) => {
16 | shapeOrigins.Ellipse = origin;
17 | };
18 |
19 | /**
20 | * Sets the origin mode for all ellipse grids that are initialized after calling this function. Default is CENTER.
21 | * @param {ShapeOrigin} origin the The new origin for ellipse grids
22 | */
23 | export const cuboidShapeOrigin = (origin: ShapeOrigin) => {
24 | shapeOrigins.Ellipse = origin;
25 | };
26 |
27 | // Only accesible within the library
28 | export type ShapeOrigins = {
29 | Rectangle: ShapeOrigin;
30 | Ellipse: ShapeOrigin;
31 | Cuboid: ShapeOrigin;
32 | };
33 |
34 | export const shapeOrigins: ShapeOrigins = {
35 | Rectangle: "top-left",
36 | Ellipse: "top-left",
37 | Cuboid: "top-left",
38 | };
39 |
--------------------------------------------------------------------------------
/src/grid-init.ts:
--------------------------------------------------------------------------------
1 | import { ShapeOrigin, shapeOrigins } from "./globals";
2 | import { GridPoint } from "./grid-point";
3 |
4 | export function initRectangleGrid(cols: number, rows: number, width: number, height: number, depth?: number): GridPoint[][] {
5 | const stepCols = width / (cols - 1);
6 | const stepRows = height / (rows - 1);
7 |
8 | depth ??= 0;
9 |
10 | if (cols === 0) {
11 | cols = 1;
12 | console.warn('Cannot create a grid with 0 columns, cols defaults to 1');
13 | }
14 |
15 | if (rows === 0) {
16 | rows = 1;
17 | console.warn('Cannot create a grid with 0 rows, rows defaults to 1');
18 | }
19 |
20 | const points: GridPoint[][] = [[]]
21 |
22 | for (let i = 0; i < cols; i++) {
23 | points[i] = [];
24 | for (let j = 0; j < rows; j++) {
25 | points[i][j] = new GridPoint(i * stepCols, j * stepRows, depth);
26 | }
27 | }
28 |
29 | return points;
30 | }
31 |
32 | export const initEllipseGrid = (cols: number, rows: number, width: number, height: number, depth?: number): GridPoint[][] => {
33 | const heightStep = height / rows;
34 | const widthStep = width / rows;
35 | const radialStep = (Math.PI * 2) / cols;
36 |
37 | depth ??= 0;
38 |
39 | const points: GridPoint[][] = [[]];
40 | for (let col = 0; col < cols; col++) {
41 | points[col] = [];
42 | const theta = col * radialStep;
43 |
44 | for (let row = rows; row >= 1; row--) {
45 | const ringWidth = row * widthStep;
46 | const ringHeight = row * heightStep;
47 |
48 | let pointX = (ringWidth / 2) * Math.cos(theta);
49 | let pointY = (ringHeight / 2) * Math.sin(theta);
50 |
51 | points[col][row - 1] = new GridPoint(pointX, pointY, depth);
52 | }
53 | }
54 | return points;
55 | }
--------------------------------------------------------------------------------
/src/grid-point.ts:
--------------------------------------------------------------------------------
1 | export interface GridPointInterface {
2 | x: number;
3 | y: number;
4 | z?: number;
5 |
6 | }
7 |
8 | /**
9 | * Represent a single point on the grid.
10 | * @class
11 | * @name GridPoint
12 | * @param {number} x the x coordinate of the point
13 | * @param {number} y the y coordinate of the point
14 | * @param {number} [z] the y coordinate of the point
15 | * @property {number} x the x coordinate of the point
16 | * @property {number} y the y coordinate of the point
17 | * @property {number} z the y coordinate of the point
18 | */
19 | export class GridPoint implements GridPointInterface {
20 | x: number;
21 | y: number;
22 | z: number;
23 | constructor(x: number, y: number, z?: number) {
24 | this.x = x;
25 | this.y = y;
26 | this.z = z;
27 | }
28 | }
29 | /**
30 | * @name createPoint
31 | * @param x
32 | * @param y
33 | * @param z
34 | * @returns {GridPoint} a point on a the x, y, z plane. Can be set to a specific index on the grid using the `Grid` or `Grid3D` `setPoint` methods
35 | *
36 | * @example
37 | * // Create and set a point on a grid
38 | * const grid = createGrid({cols:5, rows: 8, width 1920, height: 1080});
39 | * const firstPoint = createPoint(-10, -10);
40 | * grid.setPoint(0, 0, firstPoint);
41 | */
42 | export const createPoint = (x: number, y: number, z?: number): GridPoint => {
43 | return new GridPoint(x, y, z);
44 | }
--------------------------------------------------------------------------------
/src/grid.3d.ts:
--------------------------------------------------------------------------------
1 | import { Condition } from "./conditions";
2 | import { GridFunction, TransformFunction } from "./grid.function";
3 | import { Grid } from "./grid";
4 | import { initRectangleGrid } from "./grid-init";
5 | import { createPoint, GridPoint } from "./grid-point";
6 |
7 | type Points3D = GridPoint[][][];
8 |
9 | interface Grid3DInterface {
10 | getPoints: () => Points3D;
11 | getPoint: (col: number, row: number, layer: number) => GridPoint;
12 | setPoint: (col: number, row: number, layer: number, point: GridPoint) => void
13 | every: (func: GridFunction, condition?: Condition) => Grid3D;
14 | translate: (x: number, y: number, z: number, condition?: Condition) => Grid3D;
15 | }
16 |
17 | type Grid3DOptions = {
18 | cols: number;
19 | rows: number;
20 | layers: number;
21 | width: number;
22 | height: number;
23 | depth: number;
24 | }
25 |
26 | /**
27 | * The main Grid class containing all a two dimensional array of GridPoints and methods to manipulate the GridPoints on grid.
28 | * @class
29 | * @name Grid3D
30 | * @param {number} cols the amount of columns the grid needs to contain
31 | * @param {number} rows the amount of rows the grid needs to contain
32 | * @param {number} layers the amount of layers the grid needs to contain
33 | * @param {number} width the width of the grid
34 | * @param {number} height the height of the grid
35 | * @param {number} depth the depth of the grid
36 | */
37 | export class Grid3D implements Grid3DInterface {
38 | private points: Points3D = [[[]]];
39 |
40 | /**
41 | *
42 | * @param {Grid3DOptions} options
43 | */
44 | constructor(options: Grid3DOptions) {
45 | let { cols, rows, layers, width, height, depth } = options;
46 | const stepLayer = depth / (layers - 1)
47 |
48 | if (layers === 0) {
49 | layers = 1;
50 | console.warn('Cannot create a grid with 0 layers, layers defaults to 1');
51 | }
52 |
53 | for (let layer = 0; layer < layers; layer++) {
54 | this.points[layer] = initRectangleGrid(cols, rows, width, height, stepLayer * layer);
55 | }
56 | }
57 |
58 | getPoints() {
59 | return [...this.points];
60 | }
61 |
62 | getPoint(col: number, row: number, layer: number) {
63 | return this.points[layer][col][row]
64 | }
65 |
66 | /**
67 | * @method
68 | * @name setPoint
69 | * @param col
70 | * @param row
71 | * @param layer
72 | * @param point
73 | *
74 | * @example
75 | * // Create and set a point on a grid
76 | * const grid = createGrid({cols: 3, rows: 5, layers: 8 , width 1080, height: 1080, depth: 1080});
77 | * const firstPoint = createPoint(-540, -540, -540);
78 | * grid.setPoint(0, 0, 0, firstPoint);
79 | */
80 | setPoint(col: number, row: number, layer: number, point: GridPoint) {
81 | this.points[layer][col][row] = point;
82 | }
83 |
84 | /**
85 | * Loops over the points in the grid, passing each point to the provided func parameter
86 | * @method
87 | * @name every
88 | * @param {GridFunction} func - a function to access each point and row/col indices
89 | * @param {Condition} condition - an optional condition for which points to execute func over
90 | * @returns { Grid3D } returns @this Grid3D Object. Used for chaining Grid methods
91 | *
92 | * @example
93 | * // draw a spheres on the grid where the size depends on the position on the grid
94 | * // sphere is a pseudo function to draw spheres to a html canvas.
95 | * grid.every((point, col, row, layer) => {
96 | * const radius = (col + row + layer + 1) * 5;
97 | * sphere(point.x, point.y, point.z, radius);
98 | * })
99 | */
100 | every(func: GridFunction, condition?: Condition): Grid3D {
101 | this.points.forEach((layer, layerIndex) => {
102 | layer.forEach((col, colIndex) => {
103 | col.forEach((point, rowIndex) => {
104 | if (!!condition && !condition(point, colIndex, rowIndex, layerIndex)) return;
105 | func(point, colIndex, rowIndex, layerIndex);
106 | })
107 | })
108 | })
109 | return this;
110 | }
111 |
112 | /**
113 | * Transforms x, y, z values of points on the grid using the supplied transform function.
114 | * control which points are getting affected by supplying a condition
115 | *
116 | * @method
117 | * @name transform
118 | * @param {TransformFunction} func - a function to transform the point's x, y values. **Must return the transformed point.**
119 | * @param {Condition} condition - an optional condition for which points to be affected
120 | * @returns { Grid3D } returns @this Grid3D Object. Used for chaining Grid methods
121 | *
122 | * @example
123 | * // Translates the grid on the y-axis by 10 and on the z-axis by 5
124 | * grid.transform((point) => {
125 | * point.y += 10;
126 | * point.z += 5;
127 | * return point; // Make sure to return the transformed point
128 | * })
129 | *
130 | */
131 | transform(func: TransformFunction, condition?: Condition): Grid3D {
132 | let warning = false;
133 | this.points.forEach((layer, layerIndex) => {
134 | layer.forEach((col, colIndex) => {
135 | col.forEach((point, rowIndex) => {
136 | if (!!condition && !condition(point, colIndex, rowIndex, layerIndex)) return;
137 | const transformedPoint = func(createPoint(point.x, point.y, point.z), colIndex, rowIndex, layerIndex);
138 |
139 | if (!transformedPoint) {
140 | warning = true;
141 | return;
142 | }
143 |
144 | point.x = transformedPoint.x;
145 | point.y = transformedPoint.y;
146 | point.z = transformedPoint.z;
147 | })
148 | })
149 | });
150 |
151 | if (warning) {
152 | console.warn("One or more functions did not return a point, make sure your transform function returns a point for it to be updated in the grid")
153 | }
154 |
155 | return this;
156 | }
157 |
158 | translate(x: number, y: number, z: number, condition?: Condition) {
159 | this.points.forEach((layer, layerIndex) => {
160 | layer.forEach((col, colIndex) => {
161 | col.forEach((point, rowIndex) => {
162 | if (!!condition && !condition(point, colIndex, rowIndex, layerIndex)) return;
163 | point.x += x;
164 | point.y += y;
165 | point.z += z;
166 | })
167 | })
168 | })
169 | return this;
170 | }
171 |
172 | }
173 |
174 | /**
175 | * @name createGrid3D
176 | * @param options
177 | * @returns {Grid3D}
178 | *
179 | * @example
180 | * const grid = createGrid({cols: 3, rows: 5, layers: 8 , width 1080, height: 1080, depth: 1080});
181 | */
182 | export const createGrid3D = (options: Grid3DOptions) => {
183 | return new Grid3D(options);
184 | }
--------------------------------------------------------------------------------
/src/grid.function.ts:
--------------------------------------------------------------------------------
1 | import { GridPoint } from "./grid-point";
2 |
3 | export type GridFunction = (point: GridPoint, col?: number, row?: number, layer?: number) => void;
4 |
5 | export type TransformFunction = (point: GridPoint, col?: number, row?: number, layer?: number) => GridPoint
6 |
--------------------------------------------------------------------------------
/src/grid.ts:
--------------------------------------------------------------------------------
1 | import { Condition } from './conditions';
2 | import { GridFunction, TransformFunction } from './grid.function';
3 | import { initEllipseGrid, initRectangleGrid } from './grid-init';
4 | import { createPoint, GridPoint } from './grid-point';
5 |
6 |
7 | /**
8 | * Enum used to determine the grid shape in the [Grid]{@link #Grid} constructor.
9 | * Values: RECTANGLE or ELLIPSE.
10 | * @readonly
11 | * @enum {number}
12 | */
13 | export enum GridShape {
14 | RECTANGLE = 0,
15 | ELLIPSE = 1,
16 | }
17 |
18 |
19 | /**
20 | * The main Grid class containing all a two dimensional array of GridPoints and methods to manipulate the GridPoints on grid.
21 | * @class
22 | * @name Grid
23 | * @param {number} cols the amount of columns the grid needs to contain
24 | * @param {number} rows the amount of rows the grid needs to contain
25 | * @param {number} width the width of the grid
26 | * @param {number} height the height of the grid
27 | * @param {GridShape} [shape] the shape of the grid (RECTANGLE or ELLIPSE). Defaults to a rectangular shaped grid.
28 | */
29 | export class Grid {
30 | private points: GridPoint[][] = [[]];
31 |
32 | /**
33 | * Instantiates a new 2 Dimensional Grid.
34 | * The horizontal distance between each column: width / (cols - 1)
35 | * The vertical distance between each row : height / (rows - 1)
36 |
37 | */
38 | constructor(cols: number, rows: number, width: number, height: number, shape?: GridShape) {
39 | if (cols === 0) {
40 | cols = 1;
41 | console.warn('Cannot create a grid with 0 columns, cols defaults to 1');
42 | }
43 |
44 | if (rows === 0) {
45 | rows = 1;
46 | console.warn('Cannot create a grid with 0 rows, rows defaults to 1');
47 | }
48 |
49 | // initialize grid
50 | if (shape === GridShape.ELLIPSE) {
51 | this.points = initEllipseGrid(cols, rows, width, height);
52 | } else {
53 | // default to rectangle grid
54 | this.points = initRectangleGrid(cols, rows, width, height)
55 | }
56 | }
57 |
58 |
59 | /**
60 | * Get all the current points on the grid
61 | * warning: gets the points array by reference. Changes to individual points will be reflected in the original grid object.
62 | * To get a deep copy use grid.copy(). eg. grid.copy.get()
63 | *
64 | * @method
65 | * @name getPoints
66 | * @returns {GridPoint[][]}
67 | *
68 |
69 | */
70 | getPoints(): GridPoint[][] {
71 | return [...this.points];
72 | }
73 |
74 | /**
75 | * Replaces all the current points on the grid
76 | * warning: sets a reference to the provided points. Changes in made by this grid object to the points will be reflected in the provided points array.
77 | * @method
78 | * @name set
79 | *
80 | * @returns {void}
81 | */
82 | set(points: GridPoint[][]): void {
83 | // TODO set a deep copy makes more sence? or make it optional and provide a deep copy helper function
84 | this.points = points;
85 | }
86 |
87 | /**
88 | * Gets a point from from indeces [col, row]
89 | * @method
90 | * @name getPoint
91 | * @param {number} col - the col index
92 | * @param {number} row - the row index
93 | * @returns {GridPoint}
94 | */
95 |
96 | getPoint(col: number, row: number): GridPoint {
97 | return this.points[col][row];
98 | }
99 |
100 | /**
101 | * returns a one dimensional array of GridPoints of the grid. One column pushed after the other.
102 | * @method
103 | * @name getFlat
104 | * @type {GridPoint[]}
105 | */
106 | getFlat(): GridPoint[] {
107 | return this.points.reduce((acc, val) => acc.concat(val), []);
108 | }
109 |
110 | /**
111 | * Loops over the points in the grid, passing each point to the provided func parameter
112 | * Provide a drawing function
113 | * @deprecated
114 | * @method
115 | * @name draw
116 | * @param {GridFunction} func - a function that handles drawing of each individual point
117 | * @param {Condition} condition - an optional condition for which points to draw
118 | * @returns { Grid } returns @this Grid Object. Used for chaining Grid methods
119 | */
120 | draw(func: GridFunction, condition?: Condition): Grid {
121 | this.points.forEach((col, colIndex) =>
122 | col.forEach((point, rowIndex) => {
123 | if (!!condition && !condition(point, colIndex, rowIndex)) return;
124 | func(point, colIndex, rowIndex);
125 | }),
126 | );
127 | return this;
128 | }
129 |
130 | /**
131 | * Loops over the points in the grid, passing each point to the provided func parameter
132 | * @method
133 | * @name every
134 | * @param {GridFunction} func - a function to access each point and row/col indices
135 | * @param {Condition} condition - an optional condition for which points to execute func over
136 | * @returns { Grid } returns @this Grid Object. Used for chaining Grid methods
137 | */
138 | every(func: GridFunction, condition?: Condition): Grid {
139 | this.points.forEach((col, colIndex) =>
140 | col.forEach((point, rowIndex) => {
141 | if (!!condition && !condition(point, colIndex, rowIndex)) return;
142 | func(point, colIndex, rowIndex);
143 | }),
144 | );
145 | return this;
146 | }
147 |
148 | /**
149 | * Transforms x, y values of points on the grid using the supplied transform function.
150 | * control which points are getting affected by supplying a condition
151 | *
152 | * @method
153 | * @name transform
154 | * @param {TransformFunction} func - a function to transform the point's x, y values. Must return the transformed point.
155 | * @param {Condition} condition - an optional condition for which points to be affected
156 | * @returns { Grid } returns @this Grid Object. Used for chaining Grid methods
157 | *
158 | * * @example
159 | * // Translates the grid on the x-axis by 5 and on the y-axis by 8
160 | * grid.transform((point) => {
161 | * point.x += 5;
162 | * point.y += 8;
163 | * return point; // Make sure to return the transformed point
164 | * })
165 | */
166 | transform(func: TransformFunction, condition?: Condition): Grid {
167 | let warning = false;
168 | this.points.forEach((col, colIndex) =>
169 | col.forEach((point, rowIndex) => {
170 | if (!!condition && !condition(point, colIndex, rowIndex)) return;
171 | const transformedPoint = func(createPoint(point.x, point.y), colIndex, rowIndex);
172 |
173 | if (!transformedPoint) {
174 | warning = true;
175 | return;
176 | }
177 |
178 | point.x = transformedPoint.x;
179 | point.y = transformedPoint.y;
180 | }),
181 | );
182 |
183 | if (warning) {
184 | console.warn("One or more functions did not return a point, make sure your transform function returns a point for it to be updated in the grid")
185 | }
186 |
187 | return this;
188 | }
189 |
190 | /**
191 | * Translates the entire grid by x en y coordinates
192 | * @method
193 | * @name translate
194 | * @param {number} x - the x coordinates to translate the points with
195 | * @param {number} y - the y coordinates to translate the points with
196 | * @param {Condition} [condition] - an optional condition for which points to translate
197 | *
198 | * @returns { Grid } returns @this Grid Object. Used for chaining Grid methods
199 | */
200 | translate(x: number, y: number, condition?: Condition): Grid {
201 | this.points.forEach((col, colIndex) =>
202 | col.forEach((point, rowIndex) => {
203 | if (!!condition && !condition(point, colIndex, rowIndex)) return;
204 | point.x += x;
205 | point.y += y;
206 | }),
207 | );
208 | return this;
209 | }
210 |
211 | /**
212 | * Creates a deep copy of the current grid object
213 | * @method
214 | * @name copy
215 | * @returns { Grid } a new instance of Grid of with the same coordinate values as @this Grid
216 | */
217 | copy(): Grid {
218 | const cols = this.points.length;
219 | // TODO take into account posibility of modified row
220 | const rows = this.points[0].length;
221 |
222 | // Width and height of grid do not matter, these will get set
223 | const copiedGrid = new Grid(cols, rows, 0, 0);
224 | const pointsDeepCopy: GridPoint[][] = JSON.parse(JSON.stringify(this.points));
225 | copiedGrid.set(pointsDeepCopy);
226 |
227 | return copiedGrid;
228 | }
229 |
230 | /*
231 | getSection(startCol: number, endCol: number, startRow: number, endRow: number): Grid {
232 | const slice = this.points.slice(startCol, endCol + 1).map((i) => i.slice(startRow, endRow + 1));
233 | const gridFromSlice = new Grid(slice[0].length, slice.length, 0, 0);
234 | gridFromSlice.set(slice);
235 | return gridFromSlice;
236 | }
237 | */
238 | }
239 |
240 | /**
241 | * Options to configure a grid
242 | * @typedef GridOptions
243 | * @property {number} cols
244 | * @property {number} rows
245 | * @property {number} width
246 | * @property {number} height
247 | * @property {GridShape} [shape]
248 | */
249 |
250 |
251 | type GridOptionsType = {
252 | cols: number;
253 | rows: number;
254 | width: number;
255 | height: number;
256 | shape?: GridShape;
257 | }
258 |
259 | /**
260 | * Create a grid
261 | *
262 | * @name createGrid
263 | * @param {GridOptions} options
264 | * @returns {Grid}
265 | *
266 | * @example
267 | * const grid = createGrid({cols: 3, rows: 5, width 1080, height: 1080});
268 | */
269 | export const createGrid = (options: GridOptionsType) => {
270 | return new Grid(options.cols, options.rows, options.width, options.height, options.shape);
271 | }
--------------------------------------------------------------------------------
/src/operators.ts:
--------------------------------------------------------------------------------
1 | import { Condition } from './conditions';
2 |
3 | export type Operator = (...conditions: Condition[]) => Condition;
4 |
5 | export const and: Operator = (...conditions: Condition[]) => {
6 | return (point, col, row) => {
7 | for (let condition of conditions) {
8 | if (!condition(point, col, row)) return false;
9 | }
10 | return true;
11 | };
12 | };
13 |
14 | export const or: Operator = (...conditions: Condition[]) => {
15 | return (point, col, row) => {
16 | for (let condition of conditions) {
17 | if (condition(point, col, row)) return true;
18 | }
19 | return false;
20 | };
21 | };
22 |
23 | export const not: Operator = (condition: Condition) => {
24 | return (point, col, row) => !condition(point, col, row);
25 | };
26 |
--------------------------------------------------------------------------------
/src/pretty-grid.ts:
--------------------------------------------------------------------------------
1 | export { createGrid, Grid, GridShape } from './grid';
2 | export { createGrid3D, Grid3D } from "./grid.3d"
3 | export { Condition, ConditionCreator, even, odd, evenCols, oddCols, evenRows, oddRows, rows, cols } from './conditions';
4 | export { and, or, not } from './operators';
5 | export { ellipseShapeOrigin, rectangleShapeOrigin, ShapeOrigin } from './globals';
6 | export { createPoint, GridPoint } from "./grid-point";
7 |
--------------------------------------------------------------------------------
/tests/conditions.test.js:
--------------------------------------------------------------------------------
1 | const { Grid, even, odd } = require('../dist/pretty-grid.js');
2 |
3 | const GRID_ROWS = 5;
4 | const GRID_COLS = 5;
5 | const WIDTH = 100;
6 | const HEIGHT = 100;
7 | const grid = new Grid(GRID_COLS, GRID_ROWS, WIDTH, HEIGHT);
8 |
9 | test('Even Condition', () => {
10 | grid.getPoints().forEach((col, colİndex) => col.forEach((point, rowİndex) => {
11 | const result = even()(point, colİndex, rowİndex);
12 | expect(result).toBe((colİndex % 2 === 0 && rowİndex % 2 === 0));
13 | }))
14 | })
15 |
16 | test('Odd Condition', () => {
17 | grid.getPoints().forEach((col, colİndex) => col.forEach((point, rowİndex) => {
18 | const result = odd()(point, colİndex, rowİndex);
19 | expect(result).toBe((colİndex % 2 !== 0 && rowİndex % 2 !== 0));
20 | }))
21 | })
22 |
--------------------------------------------------------------------------------
/tests/create-grid.test.js:
--------------------------------------------------------------------------------
1 | const { createGrid } = require('../dist/pretty-grid.js');
2 |
3 | const GRID_ROWS = 5;
4 | const GRID_COLS = 7;
5 | const WIDTH = 100;
6 | const HEIGHT = 100;
7 | const grid = createGrid({ cols: GRID_COLS, rows: GRID_ROWS, width: WIDTH, height: HEIGHT });
8 |
9 | test("Grid intervals", () => {
10 | const INTERVAL_ROWS = HEIGHT / (GRID_ROWS - 1);
11 | const INTERVAL_COLS = WIDTH / (GRID_COLS - 1);
12 |
13 | expect(grid.getPoint(1, 1).x).toBe(INTERVAL_COLS); // cols
14 | expect(grid.getPoint(1, 1).y).toBe(INTERVAL_ROWS); // rows
15 | expect(grid.getPoint(GRID_COLS - 1, GRID_ROWS - 1).x).toBe(WIDTH); // last col
16 | expect(grid.getPoint(GRID_COLS - 1, GRID_ROWS - 1).y).toBe(HEIGHT); // last col
17 | })
18 |
19 | test("columns length", () => {
20 | expect(grid.getPoints().length).toBe(GRID_COLS);
21 | })
22 |
23 | test("row length", () => {
24 | expect(grid.getPoints()[0].length).toBe(GRID_ROWS);
25 | })
--------------------------------------------------------------------------------
/tests/grid.3d.test.js:
--------------------------------------------------------------------------------
1 | const { createGrid3D } = require("../dist/pretty-grid.js");
2 |
3 |
4 | const ROWS = 5;
5 | const COLS = 7;
6 | const LAYERS = 4;
7 | const WIDTH = 100;
8 | const HEIGHT = 100;
9 | const DEPTH = 100;
10 | const grid = new createGrid3D({ cols: COLS, rows: ROWS, layers: LAYERS, width: WIDTH, height: HEIGHT, depth: DEPTH });
11 |
12 |
13 | test('Check dimensions', () => {
14 | expect(grid.getPoints().length).toBe(LAYERS); // cols
15 | expect(grid.getPoints()[0].length).toBe(COLS); // rows
16 | expect(grid.getPoints()[0][0].length).toBe(ROWS); // rows
17 | })
18 |
19 | test("test translations", () => {
20 | // const layerStep = DEPTH / (LAYERS - 1);
21 | // const colStep = WIDTH / (COLS - 1);
22 | // const rowStep = HEIGHT / (ROWS - 1);
23 |
24 | const DX = 3;
25 | const DY = 5;
26 | const DZ = 8;
27 |
28 | grid.translate(DX, DY, DZ);
29 | const firstPoint = grid.getPoint(0, 0, 0);
30 | expect(firstPoint.x).toBe(DX);
31 | expect(firstPoint.y).toBe(DY);
32 | expect(firstPoint.z).toBe(DZ);
33 |
34 | const lastPoint = grid.getPoint(COLS - 1, ROWS - 1, LAYERS - 1);
35 | expect(lastPoint.x).toBe(WIDTH + DX);
36 | expect(lastPoint.y).toBe(HEIGHT + DY);
37 | expect(lastPoint.z).toBe(DEPTH + DZ);
38 |
39 | // cleanup
40 | grid.translate(-DX, -DY, -DZ);
41 |
42 | })
--------------------------------------------------------------------------------
/tests/grid.test.js:
--------------------------------------------------------------------------------
1 | const { Grid, createGrid } = require('../dist/pretty-grid.js');
2 |
3 | const GRID_ROWS = 5;
4 | const GRID_COLS = 7;
5 | const WIDTH = 100;
6 | const HEIGHT = 100;
7 | const grid = new createGrid({ cols: GRID_COLS, rows: GRID_ROWS, width: WIDTH, height: HEIGHT });
8 |
9 |
10 | test('Check dimensions', () => {
11 | expect(grid.getPoints().length).toBe(GRID_COLS); // cols
12 | expect(grid.getPoints()[0].length).toBe(GRID_ROWS); // rows
13 | })
14 |
15 | test('Check interval', () => {
16 | const INTERVAL_ROWS = HEIGHT / (GRID_ROWS - 1);
17 | const INTERVAL_COLS = WIDTH / (GRID_COLS - 1);
18 |
19 | expect(grid.getPoint(1, 1).x).toBe(INTERVAL_COLS); // cols
20 | expect(grid.getPoint(1, 1).y).toBe(INTERVAL_ROWS); // rows
21 | expect(grid.getPoint(GRID_COLS - 1, GRID_ROWS - 1).x).toBe(WIDTH); // last col
22 | expect(grid.getPoint(GRID_COLS - 1, GRID_ROWS - 1).y).toBe(HEIGHT); // last col
23 | })
24 |
25 | test('Deep copy of grid', () => {
26 | const deepCopy = grid.copy();
27 | const copiedPoint = deepCopy.getPoint(0, 0);
28 | copiedPoint.x += 10;
29 | // check if modification of copiedPoint affects original grid
30 | expect(grid.getPoint(0, 0).x).toBe(0);
31 | })
32 |
33 | test('Translate the entire grid', () => {
34 | const tx = 10;
35 | const ty = 15;
36 | const point = grid.getPoint(0, 0);
37 | const originalX = point.x;
38 | const originalY = point.y;
39 |
40 | grid.translate(tx, ty);
41 |
42 | expect(grid.getPoint(0, 0).x).toBe(originalX + tx);
43 | expect(grid.getPoint(0, 0).y).toBe(originalY + ty);
44 |
45 | // cleanup
46 | grid.translate(-tx, -ty)
47 | })
48 |
49 | test('Test the getFlat funcions length', () => {
50 | const pointsArray = grid.getFlat();
51 |
52 | expect(pointsArray.length).toBe(GRID_COLS * GRID_ROWS);
53 | })
54 |
55 | test('Transform points', () => {
56 | const tranformGrid = createGrid({ cols: GRID_COLS, rows: GRID_ROWS, width: WIDTH, height: HEIGHT });
57 | tranformGrid.transform((point) => {
58 | point.x += 10;
59 | return point;
60 | })
61 |
62 | expect(tranformGrid.getPoint(0, 0).x).toBe(10);
63 | })
64 |
65 | test('Tranform without point return should not mutate the point', () => {
66 | const nonTranfsormedGrid = createGrid({ cols: GRID_COLS, rows: GRID_ROWS, width: WIDTH, height: HEIGHT });
67 | nonTranfsormedGrid.transform((point) => {
68 | point.x += 10;
69 | })
70 | expect(nonTranfsormedGrid.getPoint(0, 0).x).toBe(0);
71 | })
72 |
--------------------------------------------------------------------------------
/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | "compilerOptions": {
3 | "outDir": "./dist",
4 | "module": "es6",
5 | "target": "es5",
6 | "declaration": true,
7 | "moduleResolution": "node",
8 | "sourceMap": true,
9 | },
10 | "include": [
11 | "src/**/*"
12 | ]
13 | }
--------------------------------------------------------------------------------
/webpack.config.js:
--------------------------------------------------------------------------------
1 | const path = require('path');
2 |
3 | module.exports = {
4 | entry: './src/pretty-grid.ts',
5 | mode: "development",
6 | module: {
7 | rules: [
8 | {
9 | test: /\.ts?$/,
10 | use: 'ts-loader',
11 | exclude: /node_modules/,
12 | },
13 | ],
14 | },
15 | resolve: {
16 | extensions: ['.ts'],
17 | },
18 | output: {
19 | path: path.resolve(__dirname, 'dist'),
20 | filename: 'pretty-grid.js',
21 | library: {
22 | name: 'prettyGrid',
23 | type: 'umd',
24 | },
25 | globalObject: 'this',
26 | },
27 | };
--------------------------------------------------------------------------------
/website/.gitignore:
--------------------------------------------------------------------------------
1 | # Dependencies
2 | /node_modules
3 |
4 | # Production
5 | /build
6 |
7 | # Generated files
8 | .docusaurus
9 | .cache-loader
10 |
11 | # Misc
12 | .DS_Store
13 | .env.local
14 | .env.development.local
15 | .env.test.local
16 | .env.production.local
17 |
18 | npm-debug.log*
19 | yarn-debug.log*
20 | yarn-error.log*
21 |
--------------------------------------------------------------------------------
/website/README.md:
--------------------------------------------------------------------------------
1 | # Website
2 |
3 | This website is built using [Docusaurus 2](https://docusaurus.io/), a modern static website generator.
4 |
5 | ### Installation
6 |
7 | ```
8 | $ yarn
9 | ```
10 |
11 | ### Local Development
12 |
13 | ```
14 | $ yarn start
15 | ```
16 |
17 | This command starts a local development server and opens up a browser window. Most changes are reflected live without having to restart the server.
18 |
19 | ### Build
20 |
21 | ```
22 | $ yarn build
23 | ```
24 |
25 | This command generates static content into the `build` directory and can be served using any static contents hosting service.
26 |
27 | ### Deployment
28 |
29 | Using SSH:
30 |
31 | ```
32 | $ USE_SSH=true yarn deploy
33 | ```
34 |
35 | Not using SSH:
36 |
37 | ```
38 | $ GIT_USER= yarn deploy
39 | ```
40 |
41 | If you are using GitHub pages for hosting, this command is a convenient way to build the website and push to the `gh-pages` branch.
42 |
--------------------------------------------------------------------------------
/website/babel.config.js:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | presets: [require.resolve('@docusaurus/core/lib/babel/preset')],
3 | };
4 |
--------------------------------------------------------------------------------
/website/blog/2019-05-28-first-blog-post.md:
--------------------------------------------------------------------------------
1 | ---
2 | slug: first-blog-post
3 | title: First Blog Post
4 | authors:
5 | name: Gao Wei
6 | title: Docusaurus Core Team
7 | url: https://github.com/wgao19
8 | image_url: https://github.com/wgao19.png
9 | tags: [hola, docusaurus]
10 | ---
11 |
12 | Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque elementum dignissim ultricies. Fusce rhoncus ipsum tempor eros aliquam consequat. Lorem ipsum dolor sit amet
13 |
--------------------------------------------------------------------------------
/website/blog/2019-05-29-long-blog-post.md:
--------------------------------------------------------------------------------
1 | ---
2 | slug: long-blog-post
3 | title: Long Blog Post
4 | authors: endi
5 | tags: [hello, docusaurus]
6 | ---
7 |
8 | This is the summary of a very long blog post,
9 |
10 | Use a `` comment to limit blog post size in the list view.
11 |
12 |
13 |
14 | Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque elementum dignissim ultricies. Fusce rhoncus ipsum tempor eros aliquam consequat. Lorem ipsum dolor sit amet
15 |
16 | Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque elementum dignissim ultricies. Fusce rhoncus ipsum tempor eros aliquam consequat. Lorem ipsum dolor sit amet
17 |
18 | Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque elementum dignissim ultricies. Fusce rhoncus ipsum tempor eros aliquam consequat. Lorem ipsum dolor sit amet
19 |
20 | Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque elementum dignissim ultricies. Fusce rhoncus ipsum tempor eros aliquam consequat. Lorem ipsum dolor sit amet
21 |
22 | Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque elementum dignissim ultricies. Fusce rhoncus ipsum tempor eros aliquam consequat. Lorem ipsum dolor sit amet
23 |
24 | Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque elementum dignissim ultricies. Fusce rhoncus ipsum tempor eros aliquam consequat. Lorem ipsum dolor sit amet
25 |
26 | Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque elementum dignissim ultricies. Fusce rhoncus ipsum tempor eros aliquam consequat. Lorem ipsum dolor sit amet
27 |
28 | Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque elementum dignissim ultricies. Fusce rhoncus ipsum tempor eros aliquam consequat. Lorem ipsum dolor sit amet
29 |
30 | Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque elementum dignissim ultricies. Fusce rhoncus ipsum tempor eros aliquam consequat. Lorem ipsum dolor sit amet
31 |
32 | Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque elementum dignissim ultricies. Fusce rhoncus ipsum tempor eros aliquam consequat. Lorem ipsum dolor sit amet
33 |
34 | Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque elementum dignissim ultricies. Fusce rhoncus ipsum tempor eros aliquam consequat. Lorem ipsum dolor sit amet
35 |
36 | Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque elementum dignissim ultricies. Fusce rhoncus ipsum tempor eros aliquam consequat. Lorem ipsum dolor sit amet
37 |
38 | Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque elementum dignissim ultricies. Fusce rhoncus ipsum tempor eros aliquam consequat. Lorem ipsum dolor sit amet
39 |
40 | Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque elementum dignissim ultricies. Fusce rhoncus ipsum tempor eros aliquam consequat. Lorem ipsum dolor sit amet
41 |
42 | Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque elementum dignissim ultricies. Fusce rhoncus ipsum tempor eros aliquam consequat. Lorem ipsum dolor sit amet
43 |
44 | Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque elementum dignissim ultricies. Fusce rhoncus ipsum tempor eros aliquam consequat. Lorem ipsum dolor sit amet
45 |
--------------------------------------------------------------------------------
/website/blog/2021-08-01-mdx-blog-post.mdx:
--------------------------------------------------------------------------------
1 | ---
2 | slug: mdx-blog-post
3 | title: MDX Blog Post
4 | authors: [slorber]
5 | tags: [docusaurus]
6 | ---
7 |
8 | Blog posts support [Docusaurus Markdown features](https://docusaurus.io/docs/markdown-features), such as [MDX](https://mdxjs.com/).
9 |
10 | :::tip
11 |
12 | Use the power of React to create interactive blog posts.
13 |
14 | ```js
15 |
16 | ```
17 |
18 |
19 |
20 | :::
21 |
--------------------------------------------------------------------------------
/website/blog/2021-08-26-welcome/docusaurus-plushie-banner.jpeg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/VadimGouskov/pretty-grid/28fa3e168d15d6a31fcaef3cd9acc7316c3ab33d/website/blog/2021-08-26-welcome/docusaurus-plushie-banner.jpeg
--------------------------------------------------------------------------------
/website/blog/2021-08-26-welcome/index.md:
--------------------------------------------------------------------------------
1 | ---
2 | slug: welcome
3 | title: Welcome
4 | authors: [slorber, yangshun]
5 | tags: [facebook, hello, docusaurus]
6 | ---
7 |
8 | [Docusaurus blogging features](https://docusaurus.io/docs/blog) are powered by the [blog plugin](https://docusaurus.io/docs/api/plugins/@docusaurus/plugin-content-blog).
9 |
10 | Simply add Markdown files (or folders) to the `blog` directory.
11 |
12 | Regular blog authors can be added to `authors.yml`.
13 |
14 | The blog post date can be extracted from filenames, such as:
15 |
16 | - `2019-05-30-welcome.md`
17 | - `2019-05-30-welcome/index.md`
18 |
19 | A blog post folder can be convenient to co-locate blog post images:
20 |
21 | 
22 |
23 | The blog supports tags as well!
24 |
25 | **And if you don't want a blog**: just delete this directory, and use `blog: false` in your Docusaurus config.
26 |
--------------------------------------------------------------------------------
/website/blog/authors.yml:
--------------------------------------------------------------------------------
1 | endi:
2 | name: Endilie Yacop Sucipto
3 | title: Maintainer of Docusaurus
4 | url: https://github.com/endiliey
5 | image_url: https://github.com/endiliey.png
6 |
7 | yangshun:
8 | name: Yangshun Tay
9 | title: Front End Engineer @ Facebook
10 | url: https://github.com/yangshun
11 | image_url: https://github.com/yangshun.png
12 |
13 | slorber:
14 | name: Sébastien Lorber
15 | title: Docusaurus maintainer
16 | url: https://sebastienlorber.com
17 | image_url: https://github.com/slorber.png
18 |
--------------------------------------------------------------------------------
/website/docs/api/API.md:
--------------------------------------------------------------------------------
1 | ## Classes
2 |
3 |
Get all the current points on the grid
29 | warning: gets the points array by reference. Changes to individual points will be reflected in the original grid object.
30 | To get a deep copy use grid.copy(). eg. grid.copy.get()
Replaces all the current points on the grid
34 | warning: sets a reference to the provided points. Changes in made by this grid object to the points will be reflected in the provided points array.
Transforms x, y values of points on the grid using the supplied transform function.
51 | control which points are getting affected by supplying a condition
Transforms x, y, z values of points on the grid using the supplied transform function.
66 | control which points are getting affected by supplying a condition
44 |
45 | );
46 | }
47 |
48 | export default function Home(): JSX.Element {
49 | const { siteConfig } = useDocusaurusContext();
50 | const features = useFeatures();
51 | const examples = useExamples();
52 | return (
53 |
54 |
57 |
58 |
59 |
60 |
61 |
62 |
63 | Why Pretty Grid ?
64 |
65 | Creating 2D or 3D grids involves a lot of boiler plate code.
66 | Especially if you want to select or manipulate different areas across your grid.
67 |
68 | Pretty-Grid let's you define a 2D or 3D grid in a single line of code. Additionally, Pretty-grid provides
69 | numerous helper methods to make complex point selections and transformations across multiple grids a breeze.
70 |
71 |
72 |
73 |
74 |
75 | {features.map(({ img, title }) => < Feature img={img} title={title}>)}
76 |
77 |
78 |
79 |
80 | Examples
81 |
82 |
83 | The examples below illustrate some basic capabilities of pretty-grid. If you want to learn more, check out the documentation section.
84 |
85 |
86 |
87 | These examples use the powerful p5.js library to draw to the html canvas. For brevity, the examples include the pretty-grid code only .
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 | {examples.map(({ title, setup, draw, code }) =>
97 | )}
98 |
99 |
100 |
101 |
102 |
103 |
104 | );
105 | }
106 |
--------------------------------------------------------------------------------
/website/src/pages/markdown-page.md:
--------------------------------------------------------------------------------
1 | ---
2 | title: Markdown page example
3 | ---
4 |
5 | # Markdown page example
6 |
7 | You don't need React to write simple standalone pages.
8 |
--------------------------------------------------------------------------------
/website/static/.nojekyll:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/VadimGouskov/pretty-grid/28fa3e168d15d6a31fcaef3cd9acc7316c3ab33d/website/static/.nojekyll
--------------------------------------------------------------------------------
/website/static/2d-transform-scatter.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/VadimGouskov/pretty-grid/28fa3e168d15d6a31fcaef3cd9acc7316c3ab33d/website/static/2d-transform-scatter.png
--------------------------------------------------------------------------------
/website/static/2d-transform-sine.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/VadimGouskov/pretty-grid/28fa3e168d15d6a31fcaef3cd9acc7316c3ab33d/website/static/2d-transform-sine.png
--------------------------------------------------------------------------------
/website/static/conditions-evenRows-3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/VadimGouskov/pretty-grid/28fa3e168d15d6a31fcaef3cd9acc7316c3ab33d/website/static/conditions-evenRows-3.png
--------------------------------------------------------------------------------
/website/static/creating-grid.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/VadimGouskov/pretty-grid/28fa3e168d15d6a31fcaef3cd9acc7316c3ab33d/website/static/creating-grid.png
--------------------------------------------------------------------------------
/website/static/ellipse-grid.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/VadimGouskov/pretty-grid/28fa3e168d15d6a31fcaef3cd9acc7316c3ab33d/website/static/ellipse-grid.png
--------------------------------------------------------------------------------
/website/static/ellipse-indices.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/VadimGouskov/pretty-grid/28fa3e168d15d6a31fcaef3cd9acc7316c3ab33d/website/static/ellipse-indices.png
--------------------------------------------------------------------------------
/website/static/features/2d-grid-ellipse-orange.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/VadimGouskov/pretty-grid/28fa3e168d15d6a31fcaef3cd9acc7316c3ab33d/website/static/features/2d-grid-ellipse-orange.png
--------------------------------------------------------------------------------
/website/static/features/2d-grid-select-orange.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/VadimGouskov/pretty-grid/28fa3e168d15d6a31fcaef3cd9acc7316c3ab33d/website/static/features/2d-grid-select-orange.png
--------------------------------------------------------------------------------
/website/static/features/2d-grid-transform-orange.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/VadimGouskov/pretty-grid/28fa3e168d15d6a31fcaef3cd9acc7316c3ab33d/website/static/features/2d-grid-transform-orange.png
--------------------------------------------------------------------------------
/website/static/features/2d-pattern.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/VadimGouskov/pretty-grid/28fa3e168d15d6a31fcaef3cd9acc7316c3ab33d/website/static/features/2d-pattern.png
--------------------------------------------------------------------------------
/website/static/features/2dgrid-orange.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/VadimGouskov/pretty-grid/28fa3e168d15d6a31fcaef3cd9acc7316c3ab33d/website/static/features/2dgrid-orange.png
--------------------------------------------------------------------------------
/website/static/features/3d-grid-orange.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/VadimGouskov/pretty-grid/28fa3e168d15d6a31fcaef3cd9acc7316c3ab33d/website/static/features/3d-grid-orange.png
--------------------------------------------------------------------------------
/website/static/features/3d-grid-select-multiple.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/VadimGouskov/pretty-grid/28fa3e168d15d6a31fcaef3cd9acc7316c3ab33d/website/static/features/3d-grid-select-multiple.png
--------------------------------------------------------------------------------
/website/static/grid3d.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/VadimGouskov/pretty-grid/28fa3e168d15d6a31fcaef3cd9acc7316c3ab33d/website/static/grid3d.png
--------------------------------------------------------------------------------
/website/static/img/docusaurus.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/VadimGouskov/pretty-grid/28fa3e168d15d6a31fcaef3cd9acc7316c3ab33d/website/static/img/docusaurus.png
--------------------------------------------------------------------------------
/website/static/img/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/VadimGouskov/pretty-grid/28fa3e168d15d6a31fcaef3cd9acc7316c3ab33d/website/static/img/favicon.ico
--------------------------------------------------------------------------------
/website/static/img/logo.svg:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/website/static/img/undraw_docusaurus_mountain.svg:
--------------------------------------------------------------------------------
1 |
172 |
--------------------------------------------------------------------------------
/website/static/img/undraw_docusaurus_tree.svg:
--------------------------------------------------------------------------------
1 |
41 |
--------------------------------------------------------------------------------
/website/static/intro.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/VadimGouskov/pretty-grid/28fa3e168d15d6a31fcaef3cd9acc7316c3ab33d/website/static/intro.png
--------------------------------------------------------------------------------
/website/static/operators.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/VadimGouskov/pretty-grid/28fa3e168d15d6a31fcaef3cd9acc7316c3ab33d/website/static/operators.png
--------------------------------------------------------------------------------
/website/static/translate.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/VadimGouskov/pretty-grid/28fa3e168d15d6a31fcaef3cd9acc7316c3ab33d/website/static/translate.png
--------------------------------------------------------------------------------
/website/tsconfig.json:
--------------------------------------------------------------------------------
1 | {
2 | // This file is not used in compilation. It is here just for a nice editor experience.
3 | "extends": "@tsconfig/docusaurus/tsconfig.json",
4 | "compilerOptions": {
5 | "baseUrl": "."
6 | }
7 | }
--------------------------------------------------------------------------------