|
@@ -1,63 +1,71 @@
|
1
|
1
|
var d3 = require("d3"),
|
2
|
2
|
patterns = require("./patterns.js"),
|
3
|
|
- sample = require("./sample-wave.js"),
|
4
|
3
|
textWrapper = require("./text-wrapper.js");
|
5
|
4
|
|
6
|
|
-module.exports = function(context) {
|
|
5
|
+module.exports = function(t) {
|
7
|
6
|
|
8
|
|
- context.patternQuality = "best";
|
|
7
|
+ var renderer = {},
|
|
8
|
+ backgroundImage,
|
|
9
|
+ wrapText,
|
|
10
|
+ theme;
|
9
|
11
|
|
10
|
|
- var renderer = {};
|
|
12
|
+ renderer.backgroundImage = function(_) {
|
|
13
|
+ if (!arguments.length) return backgroundImage;
|
|
14
|
+ backgroundImage = _;
|
|
15
|
+ return this;
|
|
16
|
+ };
|
11
|
17
|
|
12
|
|
- renderer.context = context;
|
|
18
|
+ renderer.theme = function(_) {
|
|
19
|
+ if (!arguments.length) return theme;
|
13
|
20
|
|
14
|
|
- renderer.update = function(options) {
|
|
21
|
+ theme = _;
|
15
|
22
|
|
16
|
|
- // TODO cleaner defaults
|
17
|
|
- options.backgroundColor = options.backgroundColor || "#fff";
|
18
|
|
- options.waveColor = options.waveColor || options.foregroundColor || "#000";
|
19
|
|
- options.captionColor = options.captionColor || options.foregroundColor || "#000";
|
|
23
|
+ // Default colors
|
|
24
|
+ theme.backgroundColor = theme.backgroundColor || "#fff";
|
|
25
|
+ theme.waveColor = theme.waveColor || theme.foregroundColor || "#000";
|
|
26
|
+ theme.captionColor = theme.captionColor || theme.foregroundColor || "#000";
|
20
|
27
|
|
21
|
|
- if (typeof options.waveTop !== "number") options.waveTop = 0;
|
22
|
|
- if (typeof options.waveBottom !== "number") options.waveBottom = options.height;
|
23
|
|
- if (typeof options.waveLeft !== "number") options.waveLeft = 0;
|
24
|
|
- if (typeof options.waveRight !== "number") options.waveRight = options.width;
|
|
28
|
+ // Default wave dimensions
|
|
29
|
+ if (typeof theme.waveTop !== "number") theme.waveTop = 0;
|
|
30
|
+ if (typeof theme.waveBottom !== "number") theme.waveBottom = theme.height;
|
|
31
|
+ if (typeof theme.waveLeft !== "number") theme.waveLeft = 0;
|
|
32
|
+ if (typeof theme.waveRight !== "number") theme.waveRight = theme.width;
|
25
|
33
|
|
26
|
|
- this.wrapText = textWrapper(context, options);
|
27
|
|
- this.options = options;
|
28
|
|
- this.waveform = options.waveform || [sample.slice(0, options.samplesPerFrame)];
|
29
|
|
- return this;
|
30
|
|
- };
|
|
34
|
+ wrapText = textWrapper(theme);
|
31
|
35
|
|
32
|
|
- // Get the waveform data for this frame
|
33
|
|
- renderer.getWaveform = function(frameNumber) {
|
34
|
|
- return this.waveform[Math.min(this.waveform.length - 1, frameNumber)];
|
|
36
|
+ return this;
|
35
|
37
|
};
|
36
|
38
|
|
37
|
39
|
// Draw the frame
|
38
|
|
- renderer.drawFrame = function(frameNumber) {
|
|
40
|
+ renderer.drawFrame = function(context, options){
|
|
41
|
+
|
|
42
|
+ context.patternQuality = "best";
|
39
|
43
|
|
40
|
44
|
// Draw the background image and/or background color
|
41
|
|
- context.clearRect(0, 0, this.options.width, this.options.height);
|
|
45
|
+ context.clearRect(0, 0, theme.width, theme.height);
|
42
|
46
|
|
43
|
|
- context.fillStyle = this.options.backgroundColor;
|
44
|
|
- context.fillRect(0, 0, this.options.width, this.options.height);
|
|
47
|
+ context.fillStyle = theme.backgroundColor;
|
|
48
|
+ context.fillRect(0, 0, theme.width, theme.height);
|
45
|
49
|
|
46
|
|
- if (this.backgroundImage) {
|
47
|
|
- context.drawImage(this.backgroundImage, 0, 0, this.options.width, this.options.height);
|
|
50
|
+ if (backgroundImage) {
|
|
51
|
+ context.drawImage(backgroundImage, 0, 0, theme.width, theme.height);
|
48
|
52
|
}
|
49
|
53
|
|
50
|
|
- patterns[this.options.pattern || "wave"](context, this.getWaveform(frameNumber), this.options);
|
|
54
|
+ patterns[theme.pattern || "wave"](context, options.waveform, theme);
|
51
|
55
|
|
52
|
56
|
// Write the caption
|
53
|
|
- if (this.caption) {
|
54
|
|
- this.wrapText(this.caption);
|
|
57
|
+ if (options.caption) {
|
|
58
|
+ wrapText(context, options.caption);
|
55
|
59
|
}
|
56
|
60
|
|
57
|
61
|
return this;
|
58
|
62
|
|
59
|
63
|
};
|
60
|
64
|
|
|
65
|
+ if (t) {
|
|
66
|
+ renderer.theme(t);
|
|
67
|
+ }
|
|
68
|
+
|
61
|
69
|
return renderer;
|
62
|
70
|
|
63
|
71
|
}
|