Skip to content

Instantly share code, notes, and snippets.

@torgeir
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save torgeir/7dda67dbe0aba606b82c to your computer and use it in GitHub Desktop.

Select an option

Save torgeir/7dda67dbe0aba606b82c to your computer and use it in GitHub Desktop.
Testing omniscient with requestAnimationFrame.
var React = require('react'),
component = require('../'),
immstruct = require('immstruct'),
Immutable = require('immutable'),
d3 = require('d3');
var d = React.DOM;
var w = Math.max(document.documentElement.clientWidth, window.innerWidth || 0),
h = Math.max(document.documentElement.clientHeight, window.innerHeight || 0),
n = 100,
colors = d3.scale.category20().range();
var RandWidth = {
getInitialState: function () {
return { w: Math.random() * w };
}
};
var Rect = component(RandWidth, function (cursor, statics) {
var width = cursor.get('x') * this.state.w / w;
return d.rect({ x: 0, y: statics.i * h / n,
width: width, height: h / n,
fill: colors[statics.i % 20]});
});
var svgProperties = { viewBox: '0 0 ' + w + ' ' + h };
var Rects = component(function (cursor) {
var rects = [svgProperties].concat(d3.range(0, n).map(function (i) {
return Rect(cursor, { i: i });
}));
return d.div({ style: { position: 'absolute', top: 0, right: 0, bottom: 0, left: 0 } },
d.svg.apply(d, rects));
});
var data = immstruct({ pos: { x: 0, y: 0 } });
var pos = data.cursor('pos');
document.body.addEventListener('mousemove', move);
document.body.addEventListener('touchmove', move);
function move (e) {
e.preventDefault();
var x = e.pageX,
y = e.pageY;
pos = pos.update(function () {
return Immutable.Map({ x: x, y: y });
});
}
var queued = false;
function update () {
if (queued) return;
queued = true;
requestAnimationFrame(render);
}
function render () {
queued = false;
React.renderComponent(Rects(pos), document.body);
}
render();
data.on('swap', update);
@torgeir
Copy link
Author

torgeir commented Oct 19, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment