Browse Source

Working line/curve

Noah 7 years ago
parent
commit
53db33f662
2 changed files with 47 additions and 7 deletions
  1. 1 1
      client/sample-wave.js
  2. 46 6
      renderer/patterns.js

File diff suppressed because it is too large
+ 1 - 1
client/sample-wave.js


+ 46 - 6
renderer/patterns.js View File

@@ -1,15 +1,17 @@
1 1
 var d3 = require("d3");
2 2
 
3 3
 module.exports = {
4
-  wave: curve(d3.curveCardinal.tension(0.1)),
5
-  pixel: curve(d3.curveStep),
4
+  wave: filledPath(d3.curveCardinal.tension(0.1)),
5
+  pixel: filledPath(d3.curveStep),
6 6
   roundBars: bars(true),
7 7
   bars: bars(),
8 8
   bricks: bricks(),
9
-  equalizer: bricks(true)
9
+  equalizer: bricks(true),
10
+  line: strokedPath(),
11
+  curve: strokedPath(d3.curveCardinal.tension(0.1))
10 12
 };
11 13
 
12
-function curve(interpolator) {
14
+function filledPath(interpolator) {
13 15
 
14 16
   return function drawCurve(context, data, options) {
15 17
 
@@ -18,9 +20,12 @@ function curve(interpolator) {
18 20
     context.lineWidth = 3;
19 21
 
20 22
     var line = d3.line()
21
-      .curve(interpolator)
22 23
       .context(context);
23 24
 
25
+    if (interpolator) {
26
+      line.curve(interpolator);
27
+    }
28
+
24 29
     var waveHeight = options.waveBottom - options.waveTop;
25 30
 
26 31
     var baseline = options.waveTop + waveHeight / 2;
@@ -110,7 +115,7 @@ function bars(round) {
110 115
 }
111 116
 
112 117
 function bricks(rainbow) {
113
-  return function (context, data, options) {
118
+  return function(context, data, options) {
114 119
 
115 120
     context.fillStyle = options.waveColor;
116 121
 
@@ -147,3 +152,38 @@ function bricks(rainbow) {
147 152
 
148 153
   };
149 154
 }
155
+
156
+function strokedPath(interpolator) {
157
+  return function(context, data, options) {
158
+
159
+    context.fillStyle = options.waveColor;
160
+    context.strokeStyle = options.waveColor;
161
+    context.lineWidth = 3;
162
+
163
+    var line = d3.line()
164
+      .context(context);
165
+
166
+    if (interpolator) {
167
+      line.curve(interpolator);
168
+    }
169
+
170
+    var x = d3.scalePoint()
171
+      .padding(0.1)
172
+      .domain(d3.range(data.length))
173
+      .range([options.waveLeft, options.waveRight]);
174
+
175
+    var y = d3.scaleLinear()
176
+      .domain([-1, 1])
177
+      .range([options.waveBottom, options.waveTop]);
178
+
179
+    var points = data.map(function(d, i){
180
+      return [x(i), y(d[1])];
181
+    });
182
+
183
+    // Fill waveform
184
+    context.beginPath();
185
+    line(points);
186
+    context.stroke();
187
+
188
+  }
189
+}