• Stars
    star
    1,307
  • Rank 34,586 (Top 0.8 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 13 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A* Search / Pathfinding Algorithm in Javascript

javascript-astar

An implementation of the A* Search Algorithm in JavaScript

See a demo at http://www.briangrinstead.com/files/astar/

Sample Usage

If you want just the A* search code (not the demo visualization), use code like this http://gist.github.com/581352

<script type='text/javascript' src='astar.js'></script>
<script type='text/javascript'>
	var graph = new Graph([
		[1,1,1,1],
		[0,1,1,0],
		[0,0,1,1]
	]);
	var start = graph.grid[0][0];
	var end = graph.grid[1][2];
	var result = astar.search(graph, start, end);
	// result is an array containing the shortest path
	var graphDiagonal = new Graph([
		[1,1,1,1],
		[0,1,1,0],
		[0,0,1,1]
	], { diagonal: true });
	
	var start = graphDiagonal.grid[0][0];
	var end = graphDiagonal.grid[1][2];
	var resultWithDiagonals = astar.search(graphDiagonal, start, end, { heuristic: astar.heuristics.diagonal });
	// Weight can easily be added by increasing the values within the graph, and where 0 is infinite (a wall)
	var graphWithWeight = new Graph([
		[1,1,2,30],
		[0,4,1.3,0],
		[0,0,5,1]
	]);
	var startWithWeight = graphWithWeight.grid[0][0];
	var endWithWeight = graphWithWeight.grid[1][2];
	var resultWithWeight = astar.search(graphWithWeight, startWithWeight, endWithWeight);
	// resultWithWeight is an array containing the shortest path taking into account the weight of a node
</script>

A few notes about weight values:

  1. A weight of 0 denotes a wall.
  2. A weight cannot be negative.
  3. A weight cannot be between 0 and 1 (exclusive).
  4. A weight can contain decimal values (greater than 1).

Original (slower) implementation

The original version of the algorithm used a list, and was a bit clearer but much slower. It was based off the original blog post. The code is available at: https://github.com/bgrins/javascript-astar/tree/0.0.1/original-implementation.

The newest version of the algorithm using a Binary Heap. It is quite faster than the original. http://www.briangrinstead.com/blog/astar-search-algorithm-in-javascript-updated Binary Heap taken from http://eloquentjavascript.net/appendix2.html (license: http://creativecommons.org/licenses/by/3.0/)

Running the test suite

Build Status

If you don't have grunt installed, follow the grunt getting started guide first.

Pull down the project, then run:

	npm install
	grunt

More Repositories

1

TinyColor

Fast, small color manipulation and conversion for JavaScript
JavaScript
4,917
star
2

devtools-snippets

A collection of helpful snippets to use inside of browser devtools
HTML
2,945
star
3

videoconverter.js

[UNMAINTAINED] Convert videos in your browser
JavaScript
2,342
star
4

spectrum

The No Hassle JavaScript Colorpicker
JavaScript
2,305
star
5

filereader.js

A lightweight wrapper for the JavaScript FileReader interface
JavaScript
414
star
6

ExpandingTextareas

jQuery plugin for elegant expanding textareas
JavaScript
262
star
7

bindWithDelay

jQuery Plugin For Delayed Event Execution
JavaScript
151
star
8

InstantSprite

A fast browser based CSS Sprite Generator
JavaScript
141
star
9

nativesortable

Use native drag and drop APIs to provide sortable items. No dependancies.
JavaScript
87
star
10

colorwheel-1k

HSV colorpicker entry for js1k
JavaScript
32
star
11

DrawWindow

A Javascript implementation for canvas.drawWindow
JavaScript
29
star
12

ui.anglepicker

A jQuery UI Angle Picker Widget
JavaScript
28
star
13

devtools-demos

JavaScript
18
star
14

browser-diff

Get the differences between text files
JavaScript
16
star
15

window-performance

A small JavaScript plugin to view the window.performance data
JavaScript
15
star
16

xbl-analysis

HTML
13
star
17

codemirror-accessible

JavaScript
9
star
18

musicviz

A Canvas / Audio Experiment
JavaScript
9
star
19

inverse-intersection

Given a parent rectangle and a collection of children rectangles on a 2D plane, find a collection of non-overlapping rectangles that cover the inverse of the original set.
JavaScript
8
star
20

wordsolver.js

Unscramble words in browser with a variety of dictionaries
JavaScript
7
star
21

ColorStash

JavaScript
6
star
22

photo-editor

JavaScript
5
star
23

webdesigner

A totally unfinished visual web page editor...
JavaScript
5
star
24

neon-mentat

JavaScript
5
star
25

esm_sandbox_eval

JavaScript
4
star
26

arewefissionyet

JavaScript
3
star
27

firefox-patches

Patch queue for Firefox
3
star
28

bgrins.github.com

My Github Page
HTML
3
star
29

html5-workshop

Content to go along with ComoRichWeb workshop night for learning HTML5
3
star
30

sqlite-datagrid-demo

JavaScript
2
star
31

image-editor-scaffold

Experimental image editor project scaffold. Not ready for prime time in any way
JavaScript
2
star
32

firefox-user-testing-helpers

Batchfile
2
star
33

asciigram

JavaScript
2
star
34

data-ui-tests

HTML
2
star
35

unicode_searcher

search for unicode characters
JavaScript
1
star
36

bug-tools

Some scripts for working with bugzilla and commits to mozilla-central
JavaScript
1
star
37

webdata

JavaScript
1
star
38

console-output-experiment

JavaScript
1
star
39

-super-spork

JavaScript
1
star
40

caption-o-matic

CSS
1
star
41

xul-document-burndown

JavaScript
1
star
42

component-prototype

JavaScript
1
star
43

comorichweb

JavaScript
1
star
44

colorwheel-full

1
star
45

rust-ci-starter

Rust
1
star
46

marquee-test

HTML
1
star
47

areweyet

Shared code for arewe*yet sites
JavaScript
1
star
48

table_to_csv

Convert HTML table to CSV
HTML
1
star
49

Original_DrawWindow_Development

Render HTML in the Canvas Element
PHP
1
star
50

imgedit

Playing around with canvas... totally unfinished
JavaScript
1
star
51

ColorStashMini

A devtools playground for talks
JavaScript
1
star
52

opfs-sqlite-demo

JavaScript
1
star
53

kitchen-sink

JavaScript
1
star
54

webdev-sandbox

TypeScript
1
star
55

editor-tests

JavaScript
1
star
56

benchmark-reporting-server

JavaScript
1
star
57

crossbrowser-selenium-action-test

JavaScript
1
star
58

alt-text-test-results

HTML
1
star