-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathscript.js
317 lines (277 loc) · 9.94 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
var quote = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
var name = "Amy Gutmann";
var title = "Penn President";
var showAttribution = true;
var showAttributionTitle = true;
var includeLogo = true;
var centerElements = false;
var inverseColors = false;
var useWordmark = false;
var useSports = false;
var includePhoto = false;
var photoURL = "";
// colors by publication
const primary = ['#aa1e22', '#44bfbf', '#456CB3'];
const logo = ['logos/dp.svg', 'logos/street.svg', 'logos/utb.svg'];
const inverse = ['logos/inverse-dp.svg', 'logos/inverse-street.svg', 'logos/inverse-utb.svg'];
var wrapText = function(context, text, x, y, maxWidth, lineHeight) {
var words = text.split(' ');
var line = '';
for (var n = 0; n < words.length; n++) {
var testLine = line + words[n] + ' ';
var metrics = context.measureText(testLine);
var testWidth = metrics.width;
if (testWidth > maxWidth && n > 0) {
context.fillText(line, x, y);
line = words[n] + ' ';
y += lineHeight;
} else {
line = testLine;
}
}
context.fillText(line, x, y);
return y;
}
var renderContent = function() {
var canvas = document.getElementById("canvas");
canvas.width = 1000;
canvas.height = 500;
var pub = document.getElementById("publicationSelect").selectedIndex;
// QUOTE TEXT + BG + IMG
var quoteCtx = canvas.getContext("2d");
quoteCtx.fillStyle = primary[pub];
quoteCtx.fillRect(0, 0, canvas.width, canvas.height);
if (inverseColors) {
quoteCtx.fillStyle = "#ffffff";
quoteCtx.fillRect(10, 10, canvas.width - 20, canvas.height - 20);
}
quoteCtx.font = "200 38px lora";
if (inverseColors) {
quoteCtx.fillStyle = primary[pub];
} else {
quoteCtx.fillStyle = "#ffffff";
}
if (centerElements) {
quoteCtx.textAlign = "center";
}
/* * * * * * * * * * * * * * * * * * * * *
* RENDER CANVAS WITH PHOTO
* * * * * * * * * * * * * * * * * * * * */
if (includePhoto && photoURL != "") {
var attribY = wrapText(quoteCtx, "\“" + quote + "\”", centerElements ? 750 : 520,
canvas.height / 2 - (showAttribution ? 100 : 70), 460, 48);
// load logo
if (includeLogo) {
var image = new Image();
image.onload = function() {
var aspect = image.width / image.height;
if (useWordmark) {
var width = 40 * aspect;
quoteCtx.drawImage(image, canvas.width - width - 40, 40, width, 40);
} else {
var width = 50 * aspect;
quoteCtx.drawImage(image, canvas.width - width - 40, 40, width, 50);
}
}
}
// load photo
var photo = new Image();
photo.onload = function() {
quoteCtx.drawImage(photo, 30, 30, 440, 440);
}
photo.src = photoURL;
if (showAttribution) {
quoteCtx.textAlign = "left"; // makes below calculations work\
var nameCtx = canvas.getContext("2d");
var titleCtx = canvas.getContext("2d");
// NAME TEXT
nameCtx.font = (showAttributionTitle) ? "38px neuzeit-grotesk" : "100 38px neuzeit-grotesk";
if (inverseColors) {
nameCtx.fillStyle = primary[pub];
} else {
nameCtx.fillStyle = "#ffffff";
}
var nameY = (showAttributionTitle) ?
attribY + 100 < (canvas.height - 70) ? attribY + 100 : canvas.height - 70 :
attribY + 135 < (canvas.height - 35) ? attribY + 135 : canvas.height - 35;
nameCtx.fillText(name, 520, nameY);
// TITLE TEXT
if (showAttributionTitle) {
titleCtx.font = "100 30px neuzeit-grotesk";
titleCtx.fillText(title, 520, attribY + 140 < (canvas.height - 30) ? attribY + 140 : canvas.height - 30);
}
}
/* * * * * * * * * * * * * * * * * * * * *
* RENDER CANVAS WITHOUT PHOTO
* * * * * * * * * * * * * * * * * * * * */
} else {
var quoteBottomSpacing = (showAttribution ? 0 : -20) + (includeLogo ? 50 : 120)
+ (!showAttribution && !includeLogo ? 40 : 0);
wrapText(quoteCtx, "\“" + quote + "\”", centerElements ? 500 : 50,
canvas.height / 2 - quoteBottomSpacing, 800, 48);
// load logo
if (includeLogo) {
var image = new Image();
image.onload = function() {
var aspect = image.width / image.height;
var width = 50 * aspect;
quoteCtx.drawImage(image, (centerElements ? (canvas.width - width) / 2 + 10 : canvas.width - width - 50), 50,
width, 50);
}
}
if (showAttribution) {
quoteCtx.textAlign = "left"; // makes below calculations work
var nameCtx = canvas.getContext("2d");
var titleCtx = canvas.getContext("2d");
// set the nameCtx font to get correct width measurement
nameCtx.font = (showAttributionTitle) ? "38px neuzeit-grotesk" : "100 38px neuzeit-grotesk";
var nameLength = (showAttributionTitle) ? nameCtx.measureText(name + " | ").width : nameCtx.measureText(name).width;
var titleLength = titleCtx.measureText(title).width;
var centerPos = (showAttributionTitle) ? canvas.width / 2 - nameLength / 2 - titleLength / 2 : canvas.width / 2 - nameLength / 2;
var nameCtxX = centerElements ? centerPos : 50;
var titleCtxX = nameLength + nameCtxX;
// NAME TEXT
nameCtx.fillStyle = inverseColors ? primary[pub] : "#ffffff";
nameCtx.fillText((showAttributionTitle) ? name + " | " : name, nameCtxX,
canvas.height - (includeLogo ? 70 : 120) + (!includeLogo && showAttribution ? 50 : 0));
// TITLE TEXT
if (showAttributionTitle) {
titleCtx.font = "100 38px neuzeit-grotesk";
titleCtx.fillText(title, titleCtxX, canvas.height - (includeLogo ? 70 : 120)
+ (!includeLogo && showAttribution ? 50 : 0));
}
}
}
// add logo
if (useSports) {
if (inverseColors) {
image.src = "logos/inverse-sports.svg";
} else {
image.src = "logos/sports.svg";
}
} else if (useWordmark) {
if (inverseColors) {
image.src = "logos/inverse-dp-wordmark.svg";
} else {
image.src = "logos/dp-wordmark.svg";
}
} else if (includeLogo) {
if (inverseColors) {
image.src = inverse[pub];
} else {
image.src = logo[pub];
}
}
}
window.setTimeout(function() {
renderContent();
}, 700);
document.getElementById('quoteBox').oninput = function() {
quote = this.value;
// Convert all quotes to curly quotes
quote = quote.replace(/\b'/g, "\’");
quote = quote.replace(/'(?=\d)/g, "\’");
quote = quote.replace(/'(?=\b|$)/g, "\‘");
quote = quote.replace(/\b"/g, "\”");
quote = quote.replace(/"(?=\w|$)/g, "\“");
renderContent();
}
document.getElementById('quoteAttr').oninput = function() {
name = this.value;
renderContent();
}
document.getElementById('quoteTitle').oninput = function() {
title = this.value;
renderContent();
}
document.getElementById('saveButton').addEventListener('click', function() {
var dataURL = canvas.toDataURL("image/png");
var data = atob(dataURL.substring("data:image/png;base64,".length)),
asArray = new Uint8Array(data.length);
for (var i = 0, len = data.length; i < len; ++i) {
asArray[i] = data.charCodeAt(i);
}
var blob = new Blob([asArray.buffer], {
type: "image/png"
});
saveAs(blob, "quote.png");
});
// EVENT HANDLERS
// Toggle Attribution
var toggleAttrCheckbox = document.getElementById('toggleAttribution');
toggleAttrCheckbox.addEventListener('click', function() {
showAttribution = !showAttribution;
renderContent();
});
// Toggle Attribution Title
var toggleAttrTitleCheckbox = document.getElementById('toggleAttributionTitle');
toggleAttrTitleCheckbox.addEventListener('click', function() {
showAttributionTitle = !showAttributionTitle;
name = document.getElementById('quoteAttr').value || "Amy Gutmann";
renderContent();
});
// Toggle Center Elements
var toggleCenterCheckbox = document.getElementById('centerElements');
toggleCenterCheckbox.addEventListener('click', function() {
centerElements = !centerElements;
renderContent();
});
// Toggle Inverse Colors
var toggleColorsCheckbox = document.getElementById('inverseColors');
toggleColorsCheckbox.addEventListener('click', function() {
inverseColors = !inverseColors;
renderContent();
});
// Change Selected Publication
var publicationSelect = document.getElementById('publicationSelect');
publicationSelect.addEventListener('change', function() {
// wordmark and sports are only available for DP
if (document.getElementById('publicationSelect').selectedIndex != 0) {
document.getElementById('useWordmark').disabled = true;
document.getElementById('useWordmark').checked = false;
useWordmark = false;
document.getElementById('useSports').disabled = true;
document.getElementById('useSports').checked = false;
useSports = false;
} else {
document.getElementById('useWordmark').disabled = false;
document.getElementById('useSports').disabled = false;
}
renderContent();
});
// Toggle Wordmark
var useWordmarkCheckbox = document.getElementById('useWordmark');
useWordmarkCheckbox.addEventListener('click', function() {
useWordmark = !useWordmark;
renderContent();
});
// Toggle Sports Logo
var useSportsCheckbox = document.getElementById('useSports');
useSportsCheckbox.addEventListener('click', function() {
useSports = !useSports;
renderContent();
});
// Include Photo
var togglePictureCheckbox = document.getElementById('togglePicture');
togglePictureCheckbox.addEventListener('click', function() {
includePhoto = !includePhoto;
renderContent();
});
// Upload Picture
var uploadPicture = document.getElementById('uploadPicture');
var fileInput = document.getElementById('fileInput');
uploadPicture.addEventListener('click', function() {
fileInput.click();
});
fileInput.addEventListener('change', function() {
if (this.files && this.files[0]) {
photoURL = URL.createObjectURL(this.files[0]);
}
renderContent();
});
// Include logo
var toggleLogoCheckbox = document.getElementById('toggleLogo');
toggleLogoCheckbox.addEventListener('click', function() {
includeLogo = !includeLogo;
renderContent();
});