Browse Source

Basic functionality for drawing and updating the label.

parisminton 8 years ago
parent
commit
98df23e08f
6 changed files with 142 additions and 51 deletions
  1. 6 6
      client/index.js
  2. 7 0
      client/preview.js
  3. 6 1
      renderer/index.js
  4. 112 40
      renderer/text-wrapper.js
  5. 1 1
      settings/labels.json
  6. 10 3
      settings/themes.json

+ 6 - 6
client/index.js View File

151
 }
151
 }
152
 
152
 
153
 // Once images are downloaded, set up listeners
153
 // Once images are downloaded, set up listeners
154
-function initialize(err, results) {
154
+function initialize(err, themesWithImages) {
155
 
155
 
156
   // Populate themes menu
156
   // Populate themes menu
157
   d3.select("#input-theme")
157
   d3.select("#input-theme")
158
     .on("change", updateTheme)
158
     .on("change", updateTheme)
159
     .selectAll("option")
159
     .selectAll("option")
160
-    .data(results) // themes with images
160
+    .data(themesWithImages)
161
     .enter()
161
     .enter()
162
     .append("option")
162
     .append("option")
163
       .text(function(d){
163
       .text(function(d){
239
   preview.caption(this.value);
239
   preview.caption(this.value);
240
 }
240
 }
241
 
241
 
242
-function updateTheme() {
243
-  preview.theme(d3.select(this.options[this.selectedIndex]).datum());
242
+function updateLabel() {
243
+  preview.label(this.value);
244
 }
244
 }
245
 
245
 
246
-function updateLabel() {
247
-  // updating logic
246
+function updateTheme() {
247
+  preview.theme(d3.select(this.options[this.selectedIndex]).datum());
248
 }
248
 }
249
 
249
 
250
 function preloadImages(themes) {
250
 function preloadImages(themes) {

+ 7 - 0
client/preview.js View File

10
 
10
 
11
 var theme,
11
 var theme,
12
     caption,
12
     caption,
13
+    label,
13
     file,
14
     file,
14
     selection;
15
     selection;
15
 
16
 
25
   return arguments.length ? (caption = _, redraw()) : caption;
26
   return arguments.length ? (caption = _, redraw()) : caption;
26
 }
27
 }
27
 
28
 
29
+function _label(_) {
30
+  return arguments.length ? (label = _, redraw()) : theme;
31
+}
32
+
28
 function _selection(_) {
33
 function _selection(_) {
29
   return arguments.length ? (selection = _) : selection;
34
   return arguments.length ? (selection = _) : selection;
30
 }
35
 }
80
 
85
 
81
   renderer.drawFrame(context, {
86
   renderer.drawFrame(context, {
82
     caption: caption,
87
     caption: caption,
88
+    label: label,
83
     waveform: sampleWave,
89
     waveform: sampleWave,
84
     frame: 0
90
     frame: 0
85
   });
91
   });
108
 
114
 
109
 module.exports = {
115
 module.exports = {
110
   caption: _caption,
116
   caption: _caption,
117
+  label: _label,
111
   theme: _theme,
118
   theme: _theme,
112
   file: _file,
119
   file: _file,
113
   selection: _selection,
120
   selection: _selection,

+ 6 - 1
renderer/index.js View File

55
 
55
 
56
     // Write the caption
56
     // Write the caption
57
     if (options.caption) {
57
     if (options.caption) {
58
-      wrapText(context, options.caption);
58
+      wrapText(context, options.caption, 'caption');
59
+    }
60
+
61
+    // Write the label
62
+    if (options.label) {
63
+      wrapText(context, options.label, 'label');
59
     }
64
     }
60
 
65
 
61
     return this;
66
     return this;

+ 112 - 40
renderer/text-wrapper.js View File

6
   var left = ifNumeric(theme.captionLeft, 0),
6
   var left = ifNumeric(theme.captionLeft, 0),
7
       right = ifNumeric(theme.captionRight, theme.width),
7
       right = ifNumeric(theme.captionRight, theme.width),
8
       bottom = ifNumeric(theme.captionBottom, null),
8
       bottom = ifNumeric(theme.captionBottom, null),
9
-      top = ifNumeric(theme.captionTop, null);
9
+      top = ifNumeric(theme.captionTop, null),
10
+      labelLeft = ifNumeric(theme.labelLeft, 0),
11
+      labelRight = ifNumeric(theme.labelRight, theme.width),
12
+      labelBottom = ifNumeric(theme.labelBottom, null),
13
+      labelTop = ifNumeric(theme.labelTop, null),
14
+
15
+      renderfunctions ;
10
 
16
 
11
   if (bottom === null && top === null) {
17
   if (bottom === null && top === null) {
12
     top = 0;
18
     top = 0;
13
   }
19
   }
14
 
20
 
15
-  var captionWidth = right - left;
21
+  if (labelBottom === null && labelTop === null) {
22
+    labelTop = 0;
23
+  }
24
+
25
+  var captionWidth = right - left,
26
+      labelWidth = labelRight - labelLeft;
16
 
27
 
17
-  return function(context, caption) {
28
+  return function(context, caption, type) {
18
 
29
 
19
     if (!caption) {
30
     if (!caption) {
20
       return;
31
       return;
21
     }
32
     }
22
 
33
 
23
-    var lines = [[]],
24
-        maxWidth = 0,
25
-        words = smartquotes(caption + "").trim().replace(/\s\s+/g, " \n").split(/ /g);
34
+    if (type === 'caption') {
35
+      var lines = [[]],
36
+          maxWidth = 0,
37
+          words = smartquotes(caption + "").trim().replace(/\s\s+/g, " \n").split(/ /g);
26
 
38
 
27
-    context.font = theme.captionFont;
28
-    context.textBaseline = "top";
29
-    context.textAlign = theme.captionAlign || "center";
39
+      context.font = theme.captionFont;
40
+      context.textBaseline = "top";
41
+      context.textAlign = theme.captionAlign || "center";
30
 
42
 
31
-    // Check whether each word exceeds the width limit
32
-    // Wrap onto next line as needed
33
-    words.forEach(function(word,i){
43
+      // Check whether each word exceeds the width limit
44
+      // Wrap onto next line as needed
45
+      words.forEach(function(word,i){
34
 
46
 
35
-      var width = context.measureText(lines[lines.length - 1].concat([word]).join(" ")).width;
47
+        var width = context.measureText(lines[lines.length - 1].concat([word]).join(" ")).width;
36
 
48
 
37
-      if (word[0] === "\n" || (lines[lines.length - 1].length && width > captionWidth)) {
49
+        if (word[0] === "\n" || (lines[lines.length - 1].length && width > captionWidth)) {
38
 
50
 
39
-        word = word.trim();
40
-        lines.push([word]);
41
-        width = context.measureText(word).width;
51
+          word = word.trim();
52
+          lines.push([word]);
53
+          width = context.measureText(word).width;
42
 
54
 
43
-      } else {
55
+        } else {
56
+
57
+          lines[lines.length - 1].push(word);
58
+
59
+        }
60
+
61
+        maxWidth = Math.max(maxWidth,width);
44
 
62
 
45
-        lines[lines.length - 1].push(word);
63
+      });
46
 
64
 
65
+      var totalHeight = lines.length * theme.captionLineHeight + (lines.length - 1) * theme.captionLineSpacing;
66
+
67
+      // horizontal alignment
68
+      var x = theme.captionAlign === "left" ? left : theme.captionAlign === "right" ? right : (left + right) / 2;
69
+
70
+      // Vertical alignment
71
+      var y;
72
+
73
+      if (top !== null && bottom !== null) {
74
+        // Vertical center
75
+        y = (bottom + top - totalHeight) / 2;
76
+      } else if (bottom !== null) {
77
+        // Vertical align bottom
78
+        y = bottom - totalHeight;
79
+      } else {
80
+        // Vertical align top
81
+        y = top;
47
       }
82
       }
48
 
83
 
49
-      maxWidth = Math.max(maxWidth,width);
84
+      // draw caption
85
+      context.fillStyle = theme.captionColor;
86
+      lines.forEach(function(line, i){
87
+        context.fillText(line.join(" "), x, y + i * (theme.captionLineHeight + theme.captionLineSpacing));
88
+      });
89
+    } // end if caption
50
 
90
 
51
-    });
52
 
91
 
53
-    var totalHeight = lines.length * theme.captionLineHeight + (lines.length - 1) * theme.captionLineSpacing;
92
+    if (type === 'label') {
93
+      var lines = [[]],
94
+          maxWidth = 0,
95
+          words = smartquotes(caption + "").trim().replace(/\s\s+/g, " \n").split(/ /g);
54
 
96
 
55
-    // horizontal alignment
56
-    var x = theme.captionAlign === "left" ? left : theme.captionAlign === "right" ? right : (left + right) / 2;
97
+      context.font = theme.labelFont;
98
+      context.textBaseline = "top";
99
+      context.textAlign = theme.labelAlign || "center";
57
 
100
 
58
-    // Vertical alignment
59
-    var y;
101
+      // Check whether each word exceeds the width limit
102
+      // Wrap onto next line as needed
103
+      words.forEach(function(word,i){
60
 
104
 
61
-    if (top !== null && bottom !== null) {
62
-      // Vertical center
63
-      y = (bottom + top - totalHeight) / 2;
64
-    } else if (bottom !== null) {
65
-      // Vertical align bottom
66
-      y = bottom - totalHeight;
67
-    } else {
68
-      // Vertical align top
69
-      y = top;
70
-    }
105
+        var width = context.measureText(lines[lines.length - 1].concat([word]).join(" ")).width;
106
+
107
+        if (word[0] === "\n" || (lines[lines.length - 1].length && width > labelWidth)) {
71
 
108
 
72
-    context.fillStyle = theme.captionColor;
73
-    lines.forEach(function(line, i){
74
-      context.fillText(line.join(" "), x, y + i * (theme.captionLineHeight + theme.captionLineSpacing));
75
-    });
109
+          word = word.trim();
110
+          lines.push([word]);
111
+          width = context.measureText(word).width;
112
+
113
+        } else {
114
+
115
+          lines[lines.length - 1].push(word);
116
+
117
+        }
118
+
119
+        maxWidth = Math.max(maxWidth,width);
120
+
121
+      });
122
+
123
+      var totalHeight = lines.length * theme.labelLineHeight + (lines.length - 1) * theme.labelLineSpacing;
124
+
125
+      // horizontal alignment
126
+      var x = theme.labelAlign === "left" ? left : theme.labelAlign === "right" ? right : (left + right) / 2;
127
+
128
+      // Vertical alignment
129
+      var y;
130
+
131
+      if (top !== null && bottom !== null) {
132
+        // Vertical center
133
+        y = (bottom + top - totalHeight) / 2;
134
+      } else if (bottom !== null) {
135
+        // Vertical align bottom
136
+        y = bottom - totalHeight;
137
+      } else {
138
+        // Vertical align top
139
+        y = top;
140
+      }
141
+
142
+      // draw label
143
+      context.fillStyle = theme.labelColor;
144
+      lines.forEach(function(line, i){
145
+        context.fillText(line.join(" "), x, y + i * (theme.labelLineHeight + theme.labelLineSpacing));
146
+      });
147
+    }
76
 
148
 
77
  };
149
  };
78
 
150
 

+ 1 - 1
settings/labels.json View File

1
 {
1
 {
2
   "podcasts" : [
2
   "podcasts" : [
3
-    "Generic",
3
+    "None",
4
     "The Daily",
4
     "The Daily",
5
     "Still Processing",
5
     "Still Processing",
6
     "Popcast",
6
     "Popcast",

+ 10 - 3
settings/themes.json View File

13
     "captionLineHeight": 52,
13
     "captionLineHeight": 52,
14
     "captionLineSpacing": 7,
14
     "captionLineSpacing": 7,
15
     "captionLeft": 200,
15
     "captionLeft": 200,
16
-    "captionRight": 1080
16
+    "captionRight": 1080,
17
+    "labelTop": 200,
18
+    "labelLeft": 60,
19
+    "labelLineHeight": 20,
20
+    "labelLineSpacing": 4
17
   },
21
   },
18
   "NYT Light": {
22
   "NYT Light": {
19
     "width": 1000,
23
     "width": 1000,
28
     "captionTop": 70,
32
     "captionTop": 70,
29
     "captionLeft": 50,
33
     "captionLeft": 50,
30
     "captionRight": 860,
34
     "captionRight": 860,
31
-    "backgroundImage": "t_logo_light.png"
35
+    "backgroundImage": "t_logo_light.png",
36
+    "labelFont": "300 30px 'NYT Franklin Light'",
37
+    "labelColor": "#000"
32
   },
38
   },
33
   "NYT Dark": {
39
   "NYT Dark": {
34
     "width": 1000,
40
     "width": 1000,
44
     "captionTop": 70,
50
     "captionTop": 70,
45
     "captionLeft": 50,
51
     "captionLeft": 50,
46
     "captionRight": 860,
52
     "captionRight": 860,
47
-    "backgroundImage": "t_logo_dark.png"
53
+    "backgroundImage": "t_logo_dark.png",
54
+    "labelColor": "#000"
48
   }
55
   }
49
 }
56
 }