-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
252 lines (211 loc) · 10.2 KB
/
index.html
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
<!DOCTYPE html>
<head>
<title>instagraphic</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-KyZXEAg3QhqLMpG8r+8fhAXLRk2vvoC2f3B09zVXn8CA5QIVfZOJ3BCsw2P0p/We" crossorigin="anonymous">
<script src="https://unpkg.com/[email protected]/dist/FileSaver.min.js"></script>
</head>
<body>
<div style="display: flex;">
<div style="flex: 1;">
<canvas id="canvas" width="540" height="540" style="border:1px solid #d3d3d3;">
</div>
<div style="flex: 2; margin-left: 3%; margin-right: 3%;">
<h1 class="text-center">Instagraphic</h1>
<form>
<div class="row g-3">
<label for="cmsUrl" class="col-sm-3 col-form-label">WordPress Import</label>
<div class="col-sm-7">
<input type="text" class="form-control" id="cmsUrl" value="https://pittnews.com/article/166200/sports/pitt-to-host-fall-sports-on-campus-without-attendance-restrictions/">
</div>
<div class="col-sm-2">
<button class="btn btn-warning" id="cmsButton">Import</button>
</div>
</div>
<hr>
<div class="row g-3 mb-3">
<label for="headline" class="col-sm-2 col-form-label">Headline</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="headline" value="Pitt to host fall sports on campus without attendance restrictions">
</div>
</div>
<div class="row g-3 mb-3">
<label for="writerByline" class="col-sm-2 col-form-label">Writer Byline</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="writerByline" value="Stephen Thompson, Senior Staff Writer">
</div>
</div>
<div class="row g-3 mb-3">
<label for="photoByline" class="col-sm-2 col-form-label">Photo Byline</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="photoByline" value="TPN File Photo">
</div>
</div>
<div class="row g-3 mb-3">
<label for="storyText" class="col-sm-2 col-form-label">Text</label>
<div class="col-sm-10">
<textarea class="form-control" rows="5" id="storyText">Pitt athletics is hoping to take a big step back towards pre-pandemic operations this fall. According to a report from the Pittsburgh Post-Gazette on Wednesday afternoon, Pitt plans on open on-campus venues to full capacity for fall sports this year, with some regulations for spectators still in place.</textarea>
</div>
</div>
<div class="row g-3 mb-3">
<label for="imageUrl" class="col-sm-2 col-form-label">Image URL</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="imageUrl" value="https://pittnews.com/wp-content/uploads/2021/08/S_FB_JM_1.jpg">
</div>
</div>
<div class="row g-3 mb-3">
<label for="imageStyle" class="col-sm-2 col-form-label">Image Style</label>
<div class="col-sm-10">
<select class="form-select" id="imageStyle">
<option selected value="l">Landscape</option>
<option value="p-l">Portrait left</option>
<option value="p-r">Portrait right</option>
</select>
</div>
</div>
<button type="submit" class="btn btn-primary" id="regenerate">Regenerate</button>
<button class="btn btn-success" id="download">Download</button>
</form>
</div>
</div>
<script>
function paintCanvas(headline, writerByline, photoByline, storyText, imageurl, imageStyle) {
var c = document.getElementById("canvas");
c.width = 542;
c.height = 542;
var ctx = c.getContext("2d");
ctx.clearRect(0, 0, c.width, c.height);
ctx.fillStyle = "#ffffff";
ctx.fillRect(0, 0, c.width, c.height);
const canvasConstraints = {
headlineRows: ctx.measureText(headline).width >= 190 ? 2 : 1,
maxStoryTextWidth: imageStyle === "l" ? 465 : 215,
};
// ** LOGO
// 480 x 132 ratio
logo = new Image();
logo.src = 'https://pittnews.com/wp-content/uploads/2018/07/mini-header1.png';
logo.onload = function(){
ctx.drawImage(logo, (540 - (480/2.8))/2, 0, 480/2.8, 132/2.8);
}
// ** HEADLINE
// Set fill style back to black for text
ctx.fillStyle = "#000000";
ctx.font = '24px Aleo';
ctx.textAlign = 'center';
if (canvasConstraints.headlineRows === 1) {
ctx.fillText(headline, 270, (132/2.8) + 24);
} else if (canvasConstraints.headlineRows === 2) {
var splitHeadline = headline.split(' ');
var headline1 = splitHeadline.slice(0, (splitHeadline.length / 2)).join(' ');
ctx.fillText(headline1, 270, (132/2.8) + 24);
var headline2 = splitHeadline.slice((splitHeadline.length / 2), splitHeadline.length).join(' ');
ctx.fillText(headline2, 270, (132/2.8) + 48);
}
// ** WRITER BYLINE
ctx.font = '16px Minion Pro';
ctx.textAlign = 'center';
const writerBylineHeight = canvasConstraints.headlineRows === 1 ? (132/2.8) + 24 + 24 : (132/2.8) + 48 + 24;
ctx.fillText(`Story by ${writerByline}`, 270, writerBylineHeight);
// ** PHOTO BYLINE
const photoBylineHeight = writerBylineHeight + 24;
const photoBylineText = `${photoByline.toLowerCase().includes('file') ? '' : 'Photo by '}${photoByline}`;
ctx.fillText(photoBylineText, 270, photoBylineHeight);
// ** TEXT
var storyTextLines = [];
ctx.font = '14px Minion Pro';
ctx.textAlign = 'start';
if (ctx.measureText(storyText).width >= canvasConstraints.maxStoryTextWidth) {
storyTextLines.push([0, 0, 0]);
var storyTextBySpaces = storyText.split(' ');
var storyTextBySpacesWidths = storyTextBySpaces.map((t) => ctx.measureText(t).width);
for(var i = 0; i < storyTextBySpacesWidths.length; i++) {
var storyTextBySpaceWidth = storyTextBySpacesWidths[i];
var currentStoryTextLine = storyTextLines[storyTextLines.length - 1];
if (currentStoryTextLine[2] + storyTextBySpaceWidth + (currentStoryTextLine[1] - currentStoryTextLine[0]) <= canvasConstraints.maxStoryTextWidth) {
currentStoryTextLine[1] = i;
currentStoryTextLine[2] += storyTextBySpaceWidth;
} else {
storyTextLines.push([i - 1, i, storyTextBySpaceWidth]);
}
}
canvasConstraints.storyTextLines = storyTextLines.length;
} else {
canvasConstraints.storyTextLines = 1;
}
var storyTextLinesHeight = canvasConstraints.storyTextLines * 16;
var storyTextStartHeight = 190;
if (imageStyle === "l") {
storyTextStartHeight = photoBylineHeight + (540 - 601/1.9 - (photoBylineHeight + 2) - storyTextLinesHeight) / 2;
}
if (storyTextLines.length > 0) {
for(var lineNum = 0; lineNum < storyTextLines.length; lineNum++) {
const lastLineEl = lineNum === storyTextLines.length - 1 ? 1 : 0;
const text = storyTextBySpaces.slice(storyTextLines[lineNum][0], storyTextLines[lineNum][1] + lastLineEl).join(' ');
var storyTextStartWidth;
if (imageStyle === "l" || imageStyle === "p-r") storyTextStartWidth = 20;
if (imageStyle === "p-l") storyTextStartWidth = 280;
ctx.fillText(text, storyTextStartWidth, storyTextStartHeight + 16 + (16*lineNum));
}
} else {
ctx.fillText(storyText, 20, storyTextStartHeight + 16);
}
// ** IMAGE
// 900 x 601 ratio
img = new Image();
img.src = imageurl;
img.onload = function(){
if (img.height > img.width && imageStyle === "l") {
// Re-trigger function based on calculated image style, only happens when importing from WP
return paintCanvas(headline, writerByline, photoByline, storyText, imageurl, "p-l");
}
if (imageStyle === "l") {
ctx.drawImage(img, (540 - (900/1.9))/2, 230, 900/1.9, 601/1.9);
} else if (imageStyle === "p-l") {
ctx.drawImage(img, (540 - (900/1.9))/2, 162, 601/2.5, 900/2.5);
} else if (imageStyle === "p-r") {
ctx.drawImage(img, 280, 162, 601/2.5, 900/2.5);
}
};
}
function paintCanvasFromForm() {
paintCanvas(
document.getElementById('headline').value,
document.getElementById('writerByline').value,
document.getElementById('photoByline').value,
document.getElementById('storyText').value,
document.getElementById('imageUrl').value,
document.getElementById('imageStyle').value,
);
}
window.onload = function() {
paintCanvasFromForm();
document.getElementById('cmsButton').addEventListener('click', function(event) {
event.preventDefault();
var r = new RegExp(/pittnews.com\/article\/(\d+)\//);
var id = document.getElementById('cmsUrl').value.match(r)[1];
if (!id) return alert ('Invalid CMS url');
fetch(`https://pittnews.com/wp-json/wp/v2/posts/${id}?`)
.then((res) => res.json())
.then((res) => {
const image = res.yoast_head_json.og_image[0];
document.getElementById('headline').value = res.title.rendered;
document.getElementById('writerByline').value = `${res.writer.join(', ')}, ${res.jobtitle.join('')}`;
document.getElementById('photoByline').value = res.photographer.replace('|', ',');
document.getElementById('storyText').value = res.excerpt.rendered.replace("<p>", "").replace("</p>", "");
document.getElementById('imageUrl').value = image.url;
document.getElementById('imageStyle').value = image.width > image.height ? "l" : "p-l";
paintCanvasFromForm();
});
});
document.getElementById('regenerate').addEventListener('click', (event) => {
event.preventDefault();
paintCanvasFromForm();
});
document.getElementById('download').addEventListener('click', (event) => {
event.preventDefault();
const img = document.getElementById("canvas").toDataURL("image/png");
saveAs(img, "graphic.png");
});
};
</script>
</body>