Skip to content

Commit

Permalink
Fix #1308. Fixed vector data export in leaflet (#1309)
Browse files Browse the repository at this point in the history
- uses canvg lib to get the svg and draw it on the map.
  • Loading branch information
offtherailz authored Nov 30, 2016
1 parent c170ad1 commit 1e69050
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"babel-preset-es2015": "6.6.0",
"babel-preset-react": "6.5.0",
"babel-preset-stage-0": "6.5.0",
"canvg-browser": "^1.0.0",
"codemirror": "5.18.2",
"css-loader": "0.19.0",
"download-cli": "1.0.1",
Expand Down
39 changes: 35 additions & 4 deletions web/client/components/map/leaflet/snapshot/GrabMap.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const ConfigUtils = require('../../../../utils/ConfigUtils');
const ProxyUtils = require('../../../../utils/ProxyUtils');
const {isEqual} = require('lodash');
const html2canvas = require('html2canvas');
const canvg = require('canvg-browser');
require("./snapshotMapStyle.css");
/**
* GrabMap for Leaflet uses HTML2CANVAS to generate the image for the existing
Expand Down Expand Up @@ -132,7 +133,19 @@ let GrabLMap = React.createClass({
doSnapshot(props) {
// get map style shifted
var leftString = window.getComputedStyle(this.mapDiv).getPropertyValue("left");
var left = 0;

// get all the informations needed to snap svg before
let svgs = this.mapDiv.getElementsByTagName("svg");
let svg = svgs && svgs[0];
let svgH;
let svgW;
let svgString;
if (svg && svg.outerHTML) {
svgString = svgs[0].outerHTML;
svgW = svg.getAttribute("width");
svgH = svg.getAttribute("height");
}
let left = 0;
if (leftString) {
left = parseInt( leftString.replace('px', ''), 10);
}
Expand Down Expand Up @@ -167,7 +180,7 @@ let GrabLMap = React.createClass({
// this is a workaround that apply the opacity on each layer snapshot,
// then merges all the snapshots.
Promise.all(queue).then((canvases) => {
canvases.reduce((pCanv, canv, idx) => {
let finalCanvas = canvases.reduce((pCanv, canv, idx) => {
let l = layers[idx - 1];
if (l === undefined) {
return pCanv;
Expand All @@ -182,8 +195,26 @@ let GrabLMap = React.createClass({
return pCanv;

});
this.props.onStatusChange("READY", this.isTainted(canvas));
this.props.onSnapshotReady(canvas, null, null, null, this.isTainted(canvas));
let finialize = () => {
this.props.onStatusChange("READY", this.isTainted(finalCanvas));
this.props.onSnapshotReady(canvas, null, null, null, this.isTainted(finalCanvas));
};

if (svg) {
let svgCanv = document.createElement('canvas');
svgCanv.setAttribute("width", svgW);
svgCanv.setAttribute("height", svgH);
canvg(svgCanv, svgString, {
ignoreClear: true,
renderCallback: () => {
let ctx = finalCanvas.getContext('2d');
ctx.drawImage(svgCanv, -1 * (svgW - finalCanvas.width) / 2, -1 * (svgH - finalCanvas.height) / 2);
finialize();
}
});
} else {
finialize();
}
});
}

Expand Down

0 comments on commit 1e69050

Please sign in to comment.