Last active
August 29, 2015 14:07
-
-
Save torgeir/7dda67dbe0aba606b82c to your computer and use it in GitHub Desktop.
Testing omniscient with requestAnimationFrame.
This file contains hidden or 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
| 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); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://public.wa.gd/omniscient-svg/