From bc7e8f27fcd0ee6708384023d2731c57166114b8 Mon Sep 17 00:00:00 2001 From: David Kaltschmidt Date: Fri, 11 Sep 2015 16:26:22 +0200 Subject: [PATCH] animate shifting of canvas --- client/app/scripts/charts/nodes-chart.js | 44 +++++++++++++++++------- 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/client/app/scripts/charts/nodes-chart.js b/client/app/scripts/charts/nodes-chart.js index fec3fc742..ded648668 100644 --- a/client/app/scripts/charts/nodes-chart.js +++ b/client/app/scripts/charts/nodes-chart.js @@ -3,6 +3,7 @@ const d3 = require('d3'); const debug = require('debug')('scope:nodes-chart'); const React = require('react'); const timely = require('timely'); +const Spring = require('react-motion').Spring; const AppStore = require('../stores/app-store'); const Edge = require('./edge'); @@ -29,6 +30,7 @@ const NodesChart = React.createClass({ edges: {}, nodeScale: 1, translate: [0, 0], + panTranslate: [0, 0], scale: 1, hasZoomed: false }; @@ -144,20 +146,37 @@ const NodesChart = React.createClass({ render: function() { const nodeElements = this.renderGraphNodes(this.state.nodes, this.state.nodeScale); const edgeElements = this.renderGraphEdges(this.state.edges, this.state.nodeScale); - const transform = 'translate(' + this.state.translate + ')' + - ' scale(' + this.state.scale + ')'; - debug('translate', transform); + const scale = this.state.scale; + + // only animate shift behavior, not panning + const panTranslate = this.state.panTranslate; + const shiftTranslate = this.state.translate; + let translate = panTranslate; + let wasShifted = false; + if (shiftTranslate[0] !== panTranslate[0] || shiftTranslate[1] !== panTranslate[1]) { + translate = shiftTranslate; + wasShifted = true; + } return ( - - - {edgeElements} - - - {nodeElements} - - + + {function(interpolated) { + let interpolatedTranslate = wasShifted ? interpolated.val : panTranslate; + const transform = 'translate(' + interpolatedTranslate + ')' + + ' scale(' + scale + ')'; + return ( + + + {edgeElements} + + + {nodeElements} + + + ); + }} + ); }, @@ -370,7 +389,8 @@ const NodesChart = React.createClass({ zoomed: function() { this.setState({ hasZoomed: true, - translate: d3.event.translate, + panTranslate: d3.event.translate.slice(), + translate: d3.event.translate.slice(), scale: d3.event.scale }); }