AStar-Typescript is a JavaScript library, written in TypeScript, that implements the A* (A-star) pathfinding algorithm. It is primarily designed for use in HTML5 games and other browser-based projects, providing a robust solution for calculating efficient paths on grid-based maps. The current stable version is 1.2.7, with development focused on feature enhancements and code improvements, such as the recent addition of a 'Get as close as possible' option for blocked paths. This library differentiates itself through its TypeScript-first approach, offering type safety and modern syntax. It supports various heuristic functions including Manhattan, Euclidean, Chebyshev, and Octile, and allows configuration for diagonal movements. Its API is designed for ease of use, accepting both predefined grid matrices and randomly generated grid structures, making it flexible for diverse game development scenarios. Releases appear to be ad-hoc based on feature additions and enhancements.
npm install astar-typescriptVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to instantiate `AStarFinder` using a predefined grid matrix, configure search parameters like diagonal movements and heuristic functions, and then find a path between specified start and goal coordinates. It also includes an example of the `closeWhenNoPath` option, which provides a path to the nearest reachable point if the direct goal is inaccessible.
Always check if the returned path array is empty and handle this scenario gracefully. For situations where a partial path to the nearest reachable point is desired even if the goal is blocked, set the `closeWhenNoPath` option to `true` during `AStarFinder` instantiation (available since v1.2.7).
Ensure that your `grid.matrix` adheres strictly to the `0` (walkable) and `1` (unwalkable) convention. Validate and sanitize any external grid data to match this format before passing it to the `AStarFinder` constructor.
For performance-critical scenarios, consider strategies like using a smaller, localized subgrid for pathfinding around agents, limiting the maximum search distance, or pre-calculating common paths. Experiment with different `heuristic` functions and disable `diagonalAllowed` if diagonal movement is not essential, as these can impact computational load.
Verify that `new AStarFinder(...)` was successfully called and the instance was correctly assigned to the variable before attempting to call any of its methods. For example: `const aStarInstance = new AStarFinder({ grid: { matrix: myMatrix } });`Inspect your `myMatrix` array to ensure all cell values are strictly `0` (walkable) or `1` (unwalkable). Convert any other values from your data source to this binary format before passing the matrix.
Confirm that the `x` and `y` properties of both `startPos` and `goalPos` are non-negative and are less than the respective `width` and `height` of your grid (i.e., `0 <= x < width` and `0 <= y < height`).
No dependency data recorded yet.