Skip to content

Commit

Permalink
Add Composition Borders (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
dkaoster authored Dec 10, 2020
1 parent 4f345e5 commit 6e7f8cf
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"topojson-simplify": "^3.0.3"
},
"dependencies": {
"d3-geo": "^2.0.1"
"d3-geo": "^2.0.1",
"d3-path": "^2.0.0"
}
}
32 changes: 31 additions & 1 deletion src/mercatorTw.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/* eslint-disable no-plusplus */
import { geoMercator } from 'd3-geo';
import { path } from 'd3-path';
import { epsilon } from './math';
import { fitExtent, fitSize } from './fit';

const defaultScale = 10000;
const defaultCenter = [275, 300];

// Geo coordinates for projection boxes given defaultScale of 10000.
const geoCoordinates = {
Expand Down Expand Up @@ -175,5 +177,33 @@ export default () => {
mercatorTw.fitExtent = (extent, object) => fitExtent(mercatorTw, extent, object);
mercatorTw.fitSize = (size, object) => fitSize(mercatorTw, size, object);

return mercatorTw.scale(defaultScale).translate([275, 300]);
mercatorTw.drawCompositionBorders = (context) => {
['penghu', 'lienchiang', 'kinmen', 'wuqiu'].forEach((areaKey) => {
context.moveTo(
defaultCenter[0] + geoCoordinates[areaKey].offsetX - (geoCoordinates[areaKey].width / 2),
defaultCenter[1] + geoCoordinates[areaKey].offsetY - (geoCoordinates[areaKey].height / 2),
);
context.lineTo(
defaultCenter[0] + geoCoordinates[areaKey].offsetX + (geoCoordinates[areaKey].width / 2),
defaultCenter[1] + geoCoordinates[areaKey].offsetY - (geoCoordinates[areaKey].height / 2),
);
context.lineTo(
defaultCenter[0] + geoCoordinates[areaKey].offsetX + (geoCoordinates[areaKey].width / 2),
defaultCenter[1] + geoCoordinates[areaKey].offsetY + (geoCoordinates[areaKey].height / 2),
);
context.lineTo(
defaultCenter[0] + geoCoordinates[areaKey].offsetX - (geoCoordinates[areaKey].width / 2),
defaultCenter[1] + geoCoordinates[areaKey].offsetY + (geoCoordinates[areaKey].height / 2),
);
context.closePath();
});
};

mercatorTw.getCompositionBorders = () => {
const context = path();
mercatorTw.drawCompositionBorders(context);
return context.toString();
};

return mercatorTw.scale(defaultScale).translate(defaultCenter);
};

0 comments on commit 6e7f8cf

Please sign in to comment.