js动态修改path值svg_SVGpath+Javascript路径发现函数JavaScript
语⾔:
JaveScriptBabelCoffeeScript
确定
'use strict';
var _createClass = (function() {
function defineProperties(target, props) {
for (var i = 0; i < props.length; i++) {
var descriptor = props[i];
if ('value' in descriptor) descriptor.writable = true;
Object.defineProperty(target, descriptor.key, descriptor);
}
}
return function(Constructor, protoProps, staticProps) {
if (protoProps) defineProperties(Constructor.prototype, protoProps);
if (staticProps) defineProperties(Constructor, staticProps);
return Constructor;
};
})();
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError('Cannot call a class as a function');
}
}
var PathFinder = (function() {
function PathFinder(data) {
_classCallCheck(this, PathFinder);
this.data = data;
for (var y = 0; y < data.length; y++) {
for (var x = 0; x < this.data[0].length; x++) { deList[y][x] = {
g: 0,
h: 0,
f: 0,
open: false,
checked: false,
parent: null,
value: this.data[y][x],
x: x,
y: y
};
}
}
}
_createClass(PathFinder, [{
key: 'getPath',
value: function getPath(start, end) {
// reset pathfinder data
var openList = [],
closedList = [],
startNode = {},
nodeList = [];
startNode.parent = null;
startNode.g = 0;
startNode.h = Heuristic(startNode, end); startNode.f = startNode.g + startNode.h; startNode.open = true;
startNode.x = start.x;
startNode.y = start.y;
openList.push(startNode);
var i = 0,
neighbors = [],
neighbor = null;
while (openList.length > 0) {
var curNode = openList.pop();
if (curNode.x === end.x && curNode.y === end.y) { // build path
var cur = curNode,
path = [];
while (cur.parent) {
path.push(cur);
cur = cur.parent;
}
path.push(cur);
return path;
} else {
curNode.checked = true;
curNode.open = false;
neighbors = Neighbors(curNode);
for (i = 0; i < neighbors.length; i++) {
neighbor = neighbors[i];
if (neighbor.checked || neighbor.value > 0) { continue;
}
var gScore = curNode.g + 1,
betterGScore = false;
if (!neighbor.open) {
betterGScore = true;
svg canvasneighbor.h = Heuristic(neighbor, end); neighbor.open = true;
openList.push(neighbor);
openList.sort(this.sortByF);
} else if (gScore < neighbor.g) {
betterGScore = true;
}
if (betterGScore) {
neighbor.parent = curNode;
neighbor.g = gScore;
neighbor.f = neighbor.g + neighbor.h;
openList.sort(this.sortByF);
}
}
}
}
}
}, {
key: 'getHeuristic',
value: function getHeuristic(node1, node2) {
// This is the Manhattan distance
var d1 = Math.abs(node2.x - node1.x),
d2 = Math.abs(node2.y - node1.y);
return d1 + d2;
}
}, {
key: 'getNeighbors',
value: function getNeighbors(node) {
var ret = [],
x = node.y,
y = node.x;
if (deList[x - 1] && deList[x - 1][y]) { ret.deList[x - 1][y]);
}
if (deList[x + 1] && deList[x + 1][y]) { ret.deList[x + 1][y]);
}
if (deList[x][y - 1]) {
ret.deList[x][y - 1]);
}
if (deList[x][y + 1]) {
ret.deList[x][y + 1]);
}
// check angles
if (x - 1 >= 0 && y - 1 >= 0) {
if (deList[x - 1][y - 1]) {
ret.deList[x - 1][y - 1]);
}
}
if (x + 1 < deList.length && y + 1 <= deList[x].length) { if (deList[x + 1][y + 1]) {
ret.deList[x + 1][y + 1]);
}
}
return ret;
}
}, {
key: 'sortByF',
value: function sortByF(a, b) {
var aa = a.f,
bb = b.f;
return aa < bb ? -1 : aa > bb ? 1 : 0;
}
}]);
return PathFinder;
})();
/*************
******* Demo.
*************/
//initialize the grid of points
var Pattern = (function() {
function Pattern(width, height, gap) {
_classCallCheck(this, Pattern);
this.points = [];
this.width = width;
this.height = height;
this.gap = gap;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论