Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a d3 quadFeature #581

Merged
merged 11 commits into from
Jun 6, 2016
19 changes: 19 additions & 0 deletions examples/common/js/examples.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var exampleUtils = {
/* Decode query components into a dictionary of values.
*
* @returns {object}: the query parameters as a dictionary.
*/
getQuery: function () {
var query = document.location.search.replace(/(^\?)/, '').split(
'&').map(function (n) {
n = n.split('=');
if (n[0]) {
this[decodeURIComponent(n[0])] = decodeURIComponent(n[1]);
}
return this;
}.bind({}))[0];
return query;
}
};

window.utils = exampleUtils;
7 changes: 6 additions & 1 deletion examples/quads/main.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
/* globals $, geo, utils */

var quadDebug = {};

// Run after the DOM loads
$(function () {
'use strict';

var query = utils.getQuery();
var map = geo.map({
node: '#map',
center: {
Expand All @@ -12,7 +15,9 @@ $(function () {
},
zoom: 4
});
var layer = map.createLayer('feature', {renderer: 'vgl'});
var layer = map.createLayer('feature', {
renderer: query.renderer ? (query.renderer === 'html' ? null : query.renderer) : 'vgl'
});
var quads = layer.createFeature('quad', {selectionAPI: true});
var previewImage = new Image();
previewImage.onload = function () {
Expand Down
12 changes: 3 additions & 9 deletions examples/transitions/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* globals $, d3, geo, utils */

// Run after the DOM loads
$(function () {
'use strict';
Expand All @@ -9,15 +11,7 @@ $(function () {
center: {x: 28.9550, y: 41.0136}
});

// Parse query parameters into an object for ease of access
var query = document.location.search.replace(/(^\?)/, '').split(
'&').map(function (n) {
n = n.split('=');
if (n[0]) {
this[decodeURIComponent(n[0])] = decodeURIComponent(n[1]);
}
return this;
}.bind({}))[0];
var query = utils.getQuery();

if (query.test) {
$('#test').removeClass('hidden');
Expand Down
7 changes: 7 additions & 0 deletions jsdoc.conf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"templates": {
"default": {
"useLongnameInNav": true
}
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
},
"scripts": {
"build": "webpack --config webpack.config.js && webpack --config external.config.js",
"build-examples": "webpack --config webpack-examples.config.js",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

"lint": "eslint --cache .",
"test": "karma start karma-cov.conf.js --single-run",
"start": "karma start karma.conf.js",
Expand All @@ -78,7 +79,7 @@
"examples": "webpack-dev-server --config webpack-examples.config.js --port 8082 --content-base dist/",
"start-test": "node examples/build.js; forever start ./testing/test-runners/server.js",
"stop-test": "forever stop ./testing/test-runners/server.js",
"docs": "jsdoc --pedantic -d dist/apidocs -r src"
"docs": "jsdoc --pedantic -d dist/apidocs -r src -c jsdoc.conf.json"
},
"keywords": [
"map",
Expand Down
2 changes: 1 addition & 1 deletion src/canvas/tileLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ var canvas_tileLayer = function () {

/* These functions don't need to do anything. */
this._getSubLayer = function () {};
this._updateSubLayer = undefined;
this._updateSubLayers = undefined;
};

registerLayerAdjustment('canvas', 'tile', canvas_tileLayer);
Expand Down
102 changes: 66 additions & 36 deletions src/d3/d3Renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ var d3Renderer = function (arg) {
m_diagonal = null,
m_scale = 1,
m_transform = {dx: 0, dy: 0, rx: 0, ry: 0, rotation: 0},
m_renderAnimFrameRef = null,
m_renderIds = {},
m_svg = null,
m_defs = null;

Expand Down Expand Up @@ -81,13 +83,6 @@ var d3Renderer = function (arg) {
};
};

this._convertPosition = function (f) {
f = util.ensureFunction(f);
return function () {
return m_this.layer().map().worldToDisplay(f.apply(m_this, arguments));
};
};

this._convertScale = function (f) {
f = util.ensureFunction(f);
return function () {
Expand Down Expand Up @@ -207,35 +202,24 @@ var d3Renderer = function (arg) {
return;
}

var layer = m_this.layer(),
map = layer.map(),
var layer = m_this.layer();

var map = layer.map(),
upperLeft = map.gcsToDisplay(m_corners.upperLeft, null),
lowerRight = map.gcsToDisplay(m_corners.lowerRight, null),
center = map.gcsToDisplay(m_corners.center, null),
group = getGroup(),
canvas = m_this.canvas(),
dx, dy, scale, rotation, rx, ry;

if (canvas.attr('scale') !== null) {
scale = parseFloat(canvas.attr('scale') || 1);
rx = (parseFloat(canvas.attr('dx') || 0) +
parseFloat(canvas.attr('offsetx') || 0));
ry = (parseFloat(canvas.attr('dy') || 0) +
parseFloat(canvas.attr('offsety') || 0));
rotation = parseFloat(canvas.attr('rotation') || 0);
dx = scale * rx + map.size().width / 2;
dy = scale * ry + map.size().height / 2;
} else {
scale = Math.sqrt(
Math.pow(lowerRight.y - upperLeft.y, 2) +
Math.pow(lowerRight.x - upperLeft.x, 2)) / m_diagonal;
// calculate the translation
rotation = map.rotation();
rx = -m_width / 2;
ry = -m_height / 2;
dx = scale * rx + center.x;
dy = scale * ry + center.y;
}
scale = Math.sqrt(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So now we compute scale everytime vs earlier it was stored / cached in the html?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before, we computed a scale for each layer of tiles and modified it upon movement. Now, we only calculate one transform (rather than one per layer). This is done initially and on movement (not every render). So we actually calculate less transforms than we did.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay, thanks.

Math.pow(lowerRight.y - upperLeft.y, 2) +
Math.pow(lowerRight.x - upperLeft.x, 2)) / m_diagonal;
// calculate the translation
rotation = map.rotation();
rx = -m_width / 2;
ry = -m_height / 2;
dx = scale * rx + center.x;
dy = scale * ry + center.y;

// set the group transform property
var transform = 'matrix(' + [scale, 0, 0, scale, dx, dy].join() + ')';
Expand Down Expand Up @@ -465,11 +449,19 @@ var d3Renderer = function (arg) {
* {
* id: A unique string identifying the feature.
* data: Array of data objects used in a d3 data method.
* index: A function that returns a unique id for each data element.
* dataIndex: A function that returns a unique id for each data element.
* defs: If set, a dictionary with values to render in the defs
* section. This can contain data, index, append, attributes,
* classes, style, and enter. enter is a function that is
* called on new elements.
* style: An object containing element CSS styles.
* attributes: An object containing element attributes.
* classes: An array of classes to add to the elements.
* append: The element type as used in d3 append methods.
* onlyRenderNew: a boolean. If true, features only get attributes and
* styles set when new. If false, features always have
* attributes and styles updated.
* sortByZ: a boolean. If true, sort features by the d.zIndex.
* parentId: If set, the group ID of the parent element.
* }
*/
Expand All @@ -482,6 +474,8 @@ var d3Renderer = function (arg) {
attributes: arg.attributes,
classes: arg.classes,
append: arg.append,
onlyRenderNew: arg.onlyRenderNew,
sortByZ: arg.sortByZ,
parentId: arg.parentId
};
return m_this.__render(arg.id, arg.parentId);
Expand All @@ -503,18 +497,50 @@ var d3Renderer = function (arg) {
}
return m_this;
}
if (parentId) {
m_this._renderFeature(id, parentId);
} else {
m_renderIds[id] = true;
if (m_renderAnimFrameRef === null) {
m_renderAnimFrameRef = window.requestAnimationFrame(m_this._renderFrame);
}
}
};

this._renderFrame = function () {
var ids = m_renderIds;
m_renderIds = {};
m_renderAnimFrameRef = null;
for (var id in ids) {
if (ids.hasOwnProperty(id)) {
m_this._renderFeature(id);
}
}
};

this._renderFeature = function (id, parentId) {
if (!m_features[id]) {
return;
}
var data = m_features[id].data,
index = m_features[id].index,
style = m_features[id].style,
attributes = m_features[id].attributes,
classes = m_features[id].classes,
append = m_features[id].append,
selection = m_this.select(id, parentId).data(data, index);
selection.enter().append(append);
selection = m_this.select(id, parentId).data(data, index),
entries, rendersel;
entries = selection.enter().append(append);
selection.exit().remove();
setAttrs(selection, attributes);
selection.attr('class', classes.concat([id]).join(' '));
setStyles(selection, style);
rendersel = m_features[id].onlyRenderNew ? entries : selection;
setAttrs(rendersel, attributes);
rendersel.attr('class', classes.concat([id]).join(' '));
setStyles(rendersel, style);
if (entries.size() && m_features[id].sortByZ) {
selection.sort(function (a, b) {
return (a.zIndex || 0) - (b.zIndex || 0);
});
}
return m_this;
};

Expand All @@ -534,7 +560,11 @@ var d3Renderer = function (arg) {
////////////////////////////////////////////////////////////////////////////
this._removeFeature = function (id) {
m_this.select(id).remove();
m_defs.selectAll('.' + id).remove();
delete m_features[id];
if (m_renderIds[id]) {
delete m_renderIds[id];
}
return m_this;
};

Expand Down
1 change: 1 addition & 0 deletions src/d3/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = {
pathFeature: require('./pathFeature'),
planeFeature: require('./planeFeature'),
pointFeature: require('./pointFeature'),
quadFeature: require('./quadFeature'),
renderer: require('./d3Renderer'),
tileLayer: require('./tileLayer'),
uniqueID: require('./uniqueID'),
Expand Down
Loading