Optimization: when moving stuff around, don't exit/enter nodes

This avoids thrashing the DOM. The positions of the nodes will still
get updated by the update selection.
This commit is contained in:
Bryan Housel
2017-12-18 00:13:16 -05:00
parent 57ba6a9862
commit b5eaa76d1a
2 changed files with 21 additions and 4 deletions
+10 -1
View File
@@ -21,6 +21,15 @@ export function svgPoints(projection, context) {
}
// Avoid exit/enter if we're just moving stuff around.
// The node will get a new version but we only need to run the update selection.
function fastEntityKey(d) {
var mode = context.mode();
var isMoving = mode && /^(add|draw|drag|move|rotate)/.test(mode.id);
return isMoving ? d.id : osmEntity.key(d);
}
function drawTargets(selection, graph, entities, filter) {
var fillClass = context.getDebug('target') ? 'pink ' : 'nocolor ';
var targets = selection.selectAll('.point.target')
@@ -64,7 +73,7 @@ export function svgPoints(projection, context) {
var groups = layer.selectAll('g.point')
.filter(filter)
.data(points, osmEntity.key);
.data(points, fastEntityKey);
groups.exit()
.remove();
+11 -3
View File
@@ -32,6 +32,14 @@ export function svgVertices(projection, context) {
return b.loc[1] - a.loc[1];
}
// Avoid exit/enter if we're just moving stuff around.
// The node will get a new version but we only need to run the update selection.
function fastEntityKey(d) {
var mode = context.mode();
var isMoving = mode && /^(add|draw|drag|move|rotate)/.test(mode.id);
return isMoving ? d.id : osmEntity.key(d);
}
function draw(selection, graph, vertices, sets, filter) {
sets = sets || { selected: {}, important: {}, hovered: {} };
@@ -89,7 +97,7 @@ export function svgVertices(projection, context) {
var groups = selection.selectAll('g.vertex')
.filter(filter)
.data(vertices, osmEntity.key);
.data(vertices, fastEntityKey);
// exit
groups.exit()
@@ -252,7 +260,7 @@ export function svgVertices(projection, context) {
var wireframe = context.surface().classed('fill-wireframe');
var zoom = ktoz(projection.scale());
var mode = context.mode();
var isDrawing = mode && /^(add|draw|drag)/.test(mode.id);
var isMoving = mode && /^(add|draw|drag|move|rotate)/.test(mode.id);
// Collect important vertices from the `entities` list..
// (during a paritial redraw, it will not contain everything)
@@ -283,7 +291,7 @@ export function svgVertices(projection, context) {
hovered: _currHover // hovered + siblings of hovered (render only in draw modes)
};
var all = _assign({}, (isDrawing ? _currHover : {}), _currSelected, _currPersistent);
var all = _assign({}, (isMoving ? _currHover : {}), _currSelected, _currPersistent);
// Draw the vertices..
// The filter function controls the scope of what objects d3 will touch (exit/enter/update)