Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
heapwolf committed Jul 19, 2013
0 parents commit e2ca610
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules/
75 changes: 75 additions & 0 deletions index.js
@@ -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
16 changes: 16 additions & 0 deletions package.json
@@ -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"
}
}
35 changes: 35 additions & 0 deletions test.js
@@ -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]])

0 comments on commit e2ca610

Please sign in to comment.