This repository has been archived by the owner on Sep 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 79
Adding mixin with animation support #41
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
fbe5a86
added mixin and example
krawaller 2afe696
removed bundle, used utils
krawaller 7a8c5b1
documentation
krawaller 4ccf62e
conformed to standardjs
krawaller 0bdb9cd
got started on mixin tests
krawaller 64321b2
added animation test
krawaller fcabe05
added animation test
krawaller efb0d69
stop animation testing
krawaller 393347e
added tests for remaining mixin funcs
krawaller 09a75f1
unlicensed example
krawaller 4fde2e9
refactored gitignore
krawaller f6d6cf4
README tweaks
krawaller cab8f95
split mixin into core and anim
krawaller 57bb5d2
moved componentWillUnmount from core to anim
krawaller df75803
updated docs with split mixins
krawaller fce075d
fixed embarrasing mixin def bug
krawaller File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
node_modules/ | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
bundle.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width"> | ||
<title>Mixin example</title> | ||
<style type="text/css"> | ||
|
||
button { | ||
position: absolute; | ||
margin: 5em; | ||
} | ||
|
||
/* styles below are from Mike Bostock's D3 chart used in the example | ||
https://bl.ocks.org/mbostock/3943967 */ | ||
|
||
body { | ||
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; | ||
margin: auto; | ||
position: relative; | ||
} | ||
|
||
text { | ||
font: 10px sans-serif; | ||
} | ||
|
||
.axis path, | ||
.axis line { | ||
fill: none; | ||
stroke: #000; | ||
shape-rendering: crispEdges; | ||
} | ||
|
||
form { | ||
position: absolute; | ||
right: 10px; | ||
top: 10px; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<div id="container"></div> | ||
<script type="text/javascript" src="./bundle.js"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
var React = require('react') | ||
var ReactDOM = require('react-dom') | ||
var Faux = require('../../lib/ReactFauxDOM') | ||
var d3 = require('d3') | ||
|
||
var Chart = React.createClass({ | ||
mixins: [Faux.mixins.core, Faux.mixins.anim], | ||
getInitialState: function () { | ||
return { look: 'stacked' } | ||
}, | ||
render: function () { | ||
return <div> | ||
<button onClick={this.toggle}>Toggle</button> | ||
{this.state.chart} | ||
</div> | ||
}, | ||
toggle: function () { | ||
if (this.state.look === 'stacked') { | ||
this.setState({ look: 'grouped' }) | ||
this.transitionGrouped() | ||
} else { | ||
this.setState({ look: 'stacked' }) | ||
this.transitionStacked() | ||
} | ||
}, | ||
componentDidMount: function () { | ||
// This will create a faux div and store its virtual DOM | ||
// in state.chart | ||
var faux = this.connectFauxDOM('div', 'chart') | ||
|
||
var component = this | ||
|
||
/* | ||
D3 code below by Mike Bostock, https://bl.ocks.org/mbostock/3943967 | ||
The only changes made for this example are... | ||
|
||
1) feeding D3 the faux node created above | ||
2) calling this.animateFauxDOM(duration) after each animation kickoff | ||
3) attaching the radio button callbacks to the component | ||
4) deleting the radio button (as we do the toggling through the react button) | ||
|
||
*/ | ||
|
||
var n = 4 // number of layers | ||
var m = 58 // number of samples per layer | ||
var stack = d3.layout.stack() | ||
var layers = stack(d3.range(n).map(function () { return bumpLayer(m, 0.1) })) | ||
var yGroupMax = d3.max(layers, function (layer) { return d3.max(layer, function (d) { return d.y }) }) | ||
var yStackMax = d3.max(layers, function (layer) { return d3.max(layer, function (d) { return d.y0 + d.y }) }) | ||
|
||
var margin = {top: 40, right: 10, bottom: 20, left: 10} | ||
var width = 960 - margin.left - margin.right | ||
var height = 500 - margin.top - margin.bottom | ||
|
||
var x = d3.scale.ordinal() | ||
.domain(d3.range(m)) | ||
.rangeRoundBands([0, width], 0.08) | ||
|
||
var y = d3.scale.linear() | ||
.domain([0, yStackMax]) | ||
.range([height, 0]) | ||
|
||
var color = d3.scale.linear() | ||
.domain([0, n - 1]) | ||
.range(['#aad', '#556']) | ||
|
||
var xAxis = d3.svg.axis() | ||
.scale(x) | ||
.tickSize(0) | ||
.tickPadding(6) | ||
.orient('bottom') | ||
|
||
var svg = d3.select(faux).append('svg') | ||
.attr('width', width + margin.left + margin.right) | ||
.attr('height', height + margin.top + margin.bottom) | ||
.append('g') | ||
.attr('transform', 'translate(' + margin.left + ',' + margin.top + ')') | ||
|
||
var layer = svg.selectAll('.layer') | ||
.data(layers) | ||
.enter().append('g') | ||
.attr('class', 'layer') | ||
.style('fill', function (d, i) { return color(i) }) | ||
|
||
var rect = layer.selectAll('rect') | ||
.data(function (d) { return d }) | ||
.enter().append('rect') | ||
.attr('x', function (d) { return x(d.x) }) | ||
.attr('y', height) | ||
.attr('width', x.rangeBand()) | ||
.attr('height', 0) | ||
|
||
rect.transition() | ||
.delay(function (d, i) { return i * 10 }) | ||
.attr('y', function (d) { return y(d.y0 + d.y) }) | ||
.attr('height', function (d) { return y(d.y0) - y(d.y0 + d.y) }) | ||
|
||
this.animateFauxDOM(800) | ||
|
||
svg.append('g') | ||
.attr('class', 'x axis') | ||
.attr('transform', 'translate(0,' + height + ')') | ||
.call(xAxis) | ||
|
||
this.transitionGrouped = function () { | ||
y.domain([0, yGroupMax]) | ||
|
||
rect.transition() | ||
.duration(500) | ||
.delay(function (d, i) { return i * 10 }) | ||
.attr('x', function (d, i, j) { return x(d.x) + x.rangeBand() / n * j }) | ||
.attr('width', x.rangeBand() / n) | ||
.transition() | ||
.attr('y', function (d) { return y(d.y) }) | ||
.attr('height', function (d) { return height - y(d.y) }) | ||
|
||
component.animateFauxDOM(2000) | ||
} | ||
|
||
this.transitionStacked = function () { | ||
y.domain([0, yStackMax]) | ||
|
||
rect.transition() | ||
.duration(500) | ||
.delay(function (d, i) { return i * 10 }) | ||
.attr('y', function (d) { return y(d.y0 + d.y) }) | ||
.attr('height', function (d) { return y(d.y0) - y(d.y0 + d.y) }) | ||
.transition() | ||
.attr('x', function (d) { return x(d.x) }) | ||
.attr('width', x.rangeBand()) | ||
|
||
component.animateFauxDOM(2000) | ||
} | ||
|
||
// Inspired by Lee Byron's test data generator. | ||
function bumpLayer (n, o) { | ||
function bump (a) { | ||
var x = 1 / (0.1 + Math.random()) | ||
var y = 2 * Math.random() - 0.5 | ||
var z = 10 / (0.1 + Math.random()) | ||
for (var i = 0; i < n; i++) { | ||
var w = (i / n - y) * z | ||
a[i] += x * Math.exp(-w * w) | ||
} | ||
} | ||
|
||
var a = [] | ||
var i | ||
for (i = 0; i < n; ++i) a[i] = o + o * Math.random() | ||
for (i = 0; i < 5; ++i) bump(a) | ||
return a.map(function (d, i) { return {x: i, y: Math.max(0, d)} }) | ||
} | ||
} | ||
}) | ||
|
||
ReactDOM.render(<Chart/>, document.getElementById('container')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "react-faux-dom-example-mixin", | ||
"version": "0.0.1", | ||
"description": "An example of how to use the React-Faux-DOM mixin to animate D3", | ||
"main": "index.js", | ||
"devDependencies": { | ||
"browserify": "^13.0.0", | ||
"d3": "^3.5.16", | ||
"react": "^0.14.6", | ||
"react-dom": "^0.14.6", | ||
"reactify": "^1.1.1" | ||
}, | ||
"scripts": { | ||
"build": "browserify -t reactify index.js -o bundle.js" | ||
}, | ||
"author": "David Waller", | ||
"license": "Unlicense" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
var anim = { | ||
animateFauxDOM: function (duration) { | ||
this.animateFauxDOMUntil = Math.max(Date.now() + duration, this.animateFauxDOMUntil) | ||
if (!this.fauxDOMAnimationInterval) { | ||
this.fauxDOMAnimationInterval = setInterval(function () { | ||
if (Date.now() < this.animateFauxDOMUntil) { | ||
this.drawFauxDOM() | ||
} else { | ||
this.stopAnimatingFauxDOM() | ||
} | ||
}.bind(this), 16) | ||
} | ||
}, | ||
stopAnimatingFauxDOM: function () { | ||
this.fauxDOMAnimationInterval = clearInterval(this.fauxDOMAnimationInterval) | ||
this.animateFauxDOMUntil = 0 | ||
}, | ||
isAnimatingFauxDOM: function () { | ||
return !!this.fauxDOMAnimationInterval | ||
}, | ||
componentWillUnmount: function () { | ||
this.stopAnimatingFauxDOM() | ||
} | ||
} | ||
|
||
module.exports = anim |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
var Element = require('../Element') | ||
var mapValues = require('../utils/mapValues') | ||
|
||
var mixin = { | ||
componentWillMount: function () { | ||
this.connectedFauxDOM = {} | ||
this.animateFauxDOMUntil = 0 | ||
}, | ||
connectFauxDOM: function (node, name) { | ||
this.connectedFauxDOM[name] = typeof node !== 'string' ? node : new Element(node) | ||
setTimeout(this.drawFauxDOM) | ||
return this.connectedFauxDOM[name] | ||
}, | ||
drawFauxDOM: function () { | ||
var virtualDOM = mapValues(this.connectedFauxDOM, function (n) { | ||
return n.toReact() | ||
}) | ||
this.setState(virtualDOM) | ||
} | ||
} | ||
|
||
module.exports = mixin |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By virtual DOM, you mean faux DOM, right? So we store the faux DOM so D3 has access to the nodes the whole way through the animation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unless I'm confusing myself, I do mean virtual DOM. Although the faux nodes are stored in the
this.connectedFauxDOM
object, only thefauxnode.toReact()
result is ever stored in state (that is, sent tothis.setState)
. Or perhaps I'm misusing the label here? In my mind virtual DOM is the sketetal representative object structure mirroring the DOM, which is whatfauxnode.toReact
outputs, right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, you're right. If you're storing the
toReact
result, it's virtual DOM. I didn't spot that.