From e2ca61070accf460f0604a274a830d66cbc74ac8 Mon Sep 17 00:00:00 2001 From: hij1nx Date: Fri, 19 Jul 2013 17:44:57 -0400 Subject: [PATCH] first commit --- .gitignore | 1 + index.js | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 16 +++++++++++ test.js | 35 ++++++++++++++++++++++++ 4 files changed, 127 insertions(+) create mode 100644 .gitignore create mode 100644 index.js create mode 100644 package.json create mode 100644 test.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c2658d7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/index.js b/index.js new file mode 100644 index 0000000..33c8a76 --- /dev/null +++ b/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 diff --git a/package.json b/package.json new file mode 100644 index 0000000..2d55898 --- /dev/null +++ b/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" + } +} diff --git a/test.js b/test.js new file mode 100644 index 0000000..4f138d2 --- /dev/null +++ b/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]])