/
photo.js
223 lines (194 loc) · 6.04 KB
/
photo.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
import Brush from "./brush.js";
import { getThemed } from "../defs.js";
class PhotoBrush extends Brush {
static template = `
<style>
:host {
display: block;
margin: var(--spacing) 0;
border-bottom: 1px dotted var(--light-gray);
--show-tint: none;
}
.container > * {
margin: 4px 0;
}
[as=credit] {
display: block;
width: 100%;
}
.warnings {
display: none;
}
.warnings.shown {
display: block;
padding: 10px;
background: var(--peach);
}
.tint-controls {
display: var(--show-tint);
}
:host([tintable]) {
--show-tint: block;
}
</style>
<div class="container">
<label for="photo-upload">Photo block:</label>
<input type="file" id="photo-upload" as="file" accept="image/*">
<label for="photo-gravity">Alignment</label>
<select as="gravity" id="photo-gravity">
<option selected value="center">Center</option>
<option value="n">North</option>
<option value="s">South</option>
<option value="e">East</option>
<option value="w">West</option>
</select>
<input as="credit" placeholder="Photo credit">
<div class="tint-controls">
<input id="tint-image" as="tinted" type=checkbox>
<label for="tint-image">Tint image?</label>
</div>
<div class="warnings" as="warning"></div>
</div>
`;
static boundMethods = ["onUpload", "invalidate"];
constructor() {
super();
this.elements.file.addEventListener("change", this.onUpload);
this.elements.tinted.addEventListener("change", this.invalidate);
this.elements.credit.addEventListener("input", this.invalidate);
this.elements.gravity.addEventListener("input", this.invalidate);
this.image = null;
this.x = 0;
this.y = 0;
this.width = 1;
this.height = 1;
this.tint = "backgroundAlt";
this.buffer = document.createElement("canvas");
this.context = this.buffer.getContext("2d");
}
persist() {
if (!this.image) return null;
var image = this.image;
var tinted = this.elements.tinted.checked;
return { image, tinted };
}
restore(state) {
if (!state) return;
this.image = state.image;
this.elements.tinted.checked = state.tinted;
}
onUpload() {
var [file] = this.elements.file.files;
if (!file) return;
this.image = new Image();
var url = URL.createObjectURL(file);
this.image.src = url;
this.image.onload = this.invalidate;
}
static observedAttributes = ["x", "y", "width", "height", "tint"];
attributeChangedCallback(attr, was, value) {
switch (attr) {
case "tint":
this[attr] = value;
break;
default:
this[attr] = Number(value);
}
this.invalidate();
}
getLayout(context, config) {
var [x, y] = this.denormalize(context.canvas, [this.x, this.y]);
var [width, height] = this.denormalize(context.canvas, [
this.width,
this.height,
]);
return new DOMRect(x, y, width, height);
}
draw(context, config) {
var layout = this.getLayout(context, config);
context.fillStyle = getThemed(config.theme, this.tint);
context.fillRect(layout.x, layout.y, layout.width, layout.height);
if (this.image) {
// check for being too small
if (
this.image.naturalWidth < layout.width ||
this.image.naturalHeight < layout.height
) {
this.elements.warning.innerHTML = `
This image is smaller than the display area,
and may appear blurry when exported.`;
} else {
this.elements.warning.innerHTML = `
Please make sure we have the rights to publish
this photo (i.e., it has been purchased on Getty
or is a courtesy photo from a source).`;
}
this.elements.warning.classList.add("shown");
// find photo positioning (centered in display area, scaled)
var imageAspect = this.image.naturalWidth / this.image.naturalHeight;
var layoutAspect = layout.width / layout.height;
// try horizontal cover
var width = layout.width;
var height = width / imageAspect;
// if it can't work, try vertical cover
if (height < layout.height) {
height = layout.height;
width = height * imageAspect;
}
// assume we're anchoring in the center for now
var gravity = this.elements.gravity.value;
var xOffset = (width - layout.width) / 2;
var yOffset = (height - layout.height) / 2;
switch (gravity) {
case "n":
yOffset = 0;
break;
case "s":
yOffset = height - layout.height;
break;
case "e":
xOffset = 0;
break;
case "w":
xOffset = width - layout.width;
}
var x = layout.x - xOffset;
var y = layout.y - yOffset;
// manually clip using the internal canvas
this.buffer.width = layout.width;
this.buffer.height = layout.height;
this.context.drawImage(this.image, -xOffset, -yOffset, width, height);
// set tint or not
var blend = this.elements.tinted.checked ? "luminosity" : "source-over";
context.globalCompositeOperation = blend;
// blit to the main canvas
context.drawImage(this.buffer, layout.x, layout.y);
// clean up tint settings
context.globalCompositeOperation = "source-over";
// is there a credit line?
var credit = this.elements.credit.value.trim().toUpperCase();
if (credit) {
context.textAlign = "right";
context.textBaseline = "bottom";
context.font = `italic 20px "Barlow Condensed"`;
context.fillStyle = "white";
context.strokeStyle = "black";
context.lineWidth = 6;
context.strokeText(credit, layout.right - 20, layout.bottom - 10);
context.fillText(credit, layout.right - 20, layout.bottom - 10);
}
} else {
// add placeholder label
context.fillStyle = getThemed(config.theme, "background");
context.textAlign = "center";
context.textBaseline = "middle";
context.font = `120px "IBM Plex Serif"`;
context.fillText(
"PHOTO",
layout.x + layout.width * 0.5,
layout.y + layout.height * 0.5
);
}
}
}
PhotoBrush.define("photo-brush");