Skip to content

Commit

Permalink
Drawing the grid now much more efficient, zero lag
Browse files Browse the repository at this point in the history
  • Loading branch information
Lorp committed May 28, 2024
1 parent d0e19ec commit e2f4e29
Showing 1 changed file with 21 additions and 20 deletions.
41 changes: 21 additions & 20 deletions src/fencer.js
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,7 @@ function mappingsChanged(mode) {
location[1] = denormalizeTuple(normalizedLocation.map((coord, a) => coord + deltas[0][a]/0x4000));
}
else {
location[1] = [...location[0]];
location[0].forEach((val, i) => location[1][i] = val); // alternative to location[1] = location[0] without reassigning array
}
}

Expand Down Expand Up @@ -1022,28 +1022,29 @@ function mappingsChanged(mode) {

// draw grid locations as a grid
if (Q("#grid-style").value.startsWith("grid-")) {
gridLocations.forEach((location, l) => {
const xIndex = Math.floor(l / yGraticules.length);
const yIndex = l % yGraticules.length;
const [x1, y1] = svgCoordsFromAxisCoords(location[1]);
if (xIndex < xGraticules.length-1) {
const topRight = gridLocations[l+yGraticules.length];
if (topRight) {
const [x2, y2] = svgCoordsFromAxisCoords(topRight[1]);
const hLine = SVG("line", {x1: x1, y1: y1, x2: x2, y2: y2, stroke: "grey"});
GLOBAL.svgEl.append(hLine);
}

// build a single path element that draws all the grid lines
let pathStr = "";

// vertical lines
for (let xn=0; xn < xGraticules.length; xn++) {
for (let yn=0; yn < yGraticules.length; yn++) {
pathStr += (yn === 0 ? "M" : "L") + svgCoordsFromAxisCoords(gridLocations[xn * yGraticules.length + yn][1]).join();
}
if (yIndex < yGraticules.length-1) {
const bottomLeft = gridLocations[l+1];
if (bottomLeft) {
const [x2, y2] = svgCoordsFromAxisCoords(bottomLeft[1])
const vLine = SVG("line", {x1: x1, y1: y1, x2: x2, y2: y2, stroke: "grey"});
GLOBAL.svgEl.append(vLine);
}
}

// horizontal lines
for (let yn=0; yn < yGraticules.length; yn++) {
for (let xn=0; xn < xGraticules.length; xn++) {
pathStr += (xn === 0 ? "M" : "L") + svgCoordsFromAxisCoords(gridLocations[xn * yGraticules.length + yn][1]).join();
}
});
}

// add the path to the SVG
const path = SVG("path", {d: pathStr, stroke: "grey", fill: "none"});
GLOBAL.svgEl.append(path);
}

// draw grid locations as vectors
else {
gridLocations.forEach((location, l) => {
Expand Down

0 comments on commit e2f4e29

Please sign in to comment.