Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit e2ca610
Showing
4 changed files
with
127 additions
and
0 deletions.
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
|
||
var Stream = require('stream') | ||
|
||
var d3 = require('d3') | ||
var through = require('through') | ||
var parse = require('event-stream').parse | ||
|
||
var livebezier = function(opts) { | ||
|
||
var stream = new Stream | ||
stream.writable = true | ||
|
||
var svg = d3 | ||
.select('body') | ||
.append('div') | ||
.attr('id', opts.id) | ||
.append('svg') | ||
|
||
svg | ||
.attr('id', 'background') | ||
.attr('width', opts.clientWidth) | ||
.attr('height', opts.clientHeight) | ||
|
||
var p = svg.append('path') | ||
p.style('fill', 'none') | ||
p.style('stroke', opts.lineColor) | ||
p.style('stroke-width', opts.lineWidth) | ||
|
||
stream.vectors = [] | ||
|
||
function write(data) { | ||
|
||
if (typeof data[0] !== 'number') { | ||
for (var i = 0, l = data.length; i < l; i++) { | ||
stream.vectors.push(data[i]) | ||
} | ||
} | ||
else { | ||
stream.vectors.push(data) | ||
} | ||
|
||
var x = d3 | ||
.scale | ||
.linear() | ||
.domain([0, opts.domainWidth]) | ||
.range([0, opts.clientWidth]) | ||
; | ||
|
||
var y = d3 | ||
.scale | ||
.linear() | ||
.domain([0, opts.domainHeight]) | ||
.range([opts.clientHeight, 0]) | ||
; | ||
|
||
p | ||
.datum(stream.vectors) | ||
.attr('d', d3 | ||
.svg | ||
.line() | ||
.interpolate('basis') | ||
.x(function(d) { return x(d[0]); }) | ||
.y(function(d) { return y(d[1]); }) | ||
) | ||
; | ||
|
||
this.queue(d3.select('body').node().innerHTML) | ||
} | ||
|
||
return stream | ||
.pipe(parse()) | ||
.pipe(through(write)) | ||
} | ||
|
||
module.exports = livebezier |
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,16 @@ | ||
{ | ||
"name": "live-bezier", | ||
"version": "0.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "", | ||
"license": "BSD", | ||
"dependencies": { | ||
"through": "~2.3.4", | ||
"event-stream": "~3.0.16", | ||
"d3": "~3.2.7" | ||
} | ||
} |
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,35 @@ | ||
|
||
var livebez = require('./index') | ||
var count = 0 | ||
|
||
|
||
var opts = { | ||
id: 'test', | ||
lineColor: '#68bef3', | ||
lineWidth: '2px', | ||
domainWidth: 50, | ||
domainHeight: 1, | ||
clientWidth: 500, | ||
clientHeight: 100, | ||
} | ||
|
||
var s = livebez(opts) | ||
|
||
s.on('data', function(d) { | ||
count++ | ||
|
||
// | ||
// TODO: currently throwing into browser, | ||
// should set up a test here to measure correctness | ||
// | ||
console.log(count, d) | ||
}) | ||
|
||
s.write([[0, 0.1], [1, 0.4], [2, 0.6], [3, 0.7]]) | ||
s.write([4, 0.8]) | ||
s.write([5, 0.9]) | ||
s.write([6, 0.5]) | ||
s.write([10, 1]) | ||
s.write([10, 0.1]) | ||
|
||
s.write([[11, 0.1], [12, 0.6], [40, 0.1]]) |