-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
executable file
·290 lines (249 loc) · 11.1 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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Quote Builder</title>
<style media="screen">
body, html {
margin: 0;
height: 100%;
}
a, a:visited {
color: inherit;
text-decoration: none;
}
body {
display: flex;
align-items: center;
justify-content: center;
}
*::selection {
background: rgba(0, 0, 0, 0.5);
color: rgba(0, 0, 0, 0.5);
}
.container {
position: relative;
}
#canvas {
background: #eee;
}
#quote, #author {
position: absolute;
font-size: 45px;
font-family: 'Impact';
letter-spacing: -1px;
line-height: 115%;
min-width: 20px;
max-width: 100%;
outline: none;
color: transparent;
overflow: hidden;
}
#quote {
left: 44px;
top: 30px;
}
#author {
width: 98%;
margin-left: -86%;
float: right;
text-align: right;
left: 855px;
top: 325px;
}
#scale {
display: block;
width: 100%;
margin-left: 0;
}
#file {
position: absolute;
visibility: hidden;
}
</style>
</head>
<body>
<div class="container">
<span id="quote" contenteditable spellcheck="false">Drop some image to start</span>
<span id="author" contenteditable spellcheck="false">Quote Builder</span>
<label for="file">
<canvas id="canvas" width="1000" height="400"></canvas>
</label>
<input id="scale" type="range" max="4" min=".01" step=".01" value="1">
<input id="file" type="file" name="pic" accept="image/*">
</div>
<script type="text/javascript">
var fontSize = '41px'
var fontFamily = 'Impact'
var canvas = document.getElementById('canvas')
var ctx = canvas.getContext('2d')
var quote = document.getElementById('quote')
var author = document.getElementById('author')
var scale = document.getElementById('scale')
var file = document.getElementById('file')
var image = new Image()
var overlay = new Image()
var imageX = 0
var imageY = 0
function render () {
ctx.save()
ctx.clearRect(0, 0, canvas.width, canvas.height)
// Scale on center
ctx.translate(canvas.width / 2, canvas.height / 2)
ctx.scale(scale.value, scale.value)
ctx.translate(-canvas.width / 2, -canvas.height / 2)
if (image.src) {
ctx.drawImage(image, imageX, imageY)
}
ctx.restore()
addThings()
}
function addImage (src) {
overlay.onload = function() {
image.onload = function () {
render()
}
}
overlay.src = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAA+gAAAGQCAMAAAAKk7pTAAABqlBMVEUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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6n5/uW8O+R36P34efjp3tgw8sfT4lP6HF66f3+Hwbv00rnN4ZOnx737L0vUvT7n1K/zl/D/S5fh/zDFmggAAAABJRU5ErkJggg=='
image.src = src
}
function addThings () {
// Use innerText if possible, because it contains line break
var text = quote.innerText ? 'innerText' : 'textContent'
// Enlarge whitespace
var quoteText = quote[text].replace(/ /g, '\u2005')
var authorText = author[text].replace(/ /g, '\u2005')
if (overlay.src) {
ctx.drawImage(overlay, 0, 0)
}
addQuote(quoteText)
addAuthor(authorText)
}
function loadImage (src) {
if (!src.type.match(/image.*/)) return false
var reader = new FileReader()
reader.onload = function (e) {
addImage(e.target.result)
}
reader.readAsDataURL(src)
return true
}
function drawText (text, x, y) {
ctx.font = fontSize + ' ' + fontFamily
ctx.fillStyle = '#fafafa'
// Shadow
ctx.shadowColor = '#262626'
ctx.shadowOffsetX = 1
ctx.shadowOffsetY = 1
ctx.shadowBlur = 6
// Stroke
ctx.lineJoin = 'miter'
ctx.miterLimit = 2
ctx.strokeStyle = '#0a0a0a'
ctx.lineWidth = 8
ctx.strokeText(text, x, y)
ctx.fillText(text, x, y)
ctx.shadowColor = 'transparent'
}
function addQuote (quote) {
var line = quote.split('\n')
var offsetX = 0
var offsetY = 0
ctx.textAlign = 'left'
if (line.length === 1) {
var text = '\u201C\u200A' + quote + '\u200A\u201D'
drawText(text, 30, 30)
} else {
line[0] = '\u201C\u200A' + line[0]
line[line.length - 2] = line[line.length - 2] + '\u200A\u201D'
line.pop()
line.forEach(function (text) {
if (line.indexOf(text) === 1) {
offsetX += 35
} else if (line.indexOf(text) > 1) {
offsetX += 20
}
drawText(text, 30 + offsetX, 30 + offsetY)
offsetY += 50
})
}
}
function addAuthor (author) {
ctx.textAlign = 'right'
var text = '-\u200A' + author
drawText(text, 970, 325)
}
function mouseDown (e) {
if (e.which === 1) {
canvas.addEventListener('mousemove', mouseMove)
}
}
function mouseUp (e) {
canvas.removeEventListener('mousemove', mouseMove)
}
function mouseMove (e) {
imageX += e.movementX / scale.value
imageY += e.movementY / scale.value
render()
}
function handleEvents() {
var editable = document.querySelectorAll('span[contenteditable]')
var clicked = {}
// Handle image move
canvas.addEventListener('mousedown', mouseDown)
canvas.addEventListener('mouseup', mouseUp)
canvas.addEventListener('mouseout', mouseUp)
// Handle input
for (var i = 0; i < editable.length; i++) {
editable[i].addEventListener('click', function () {
if (clicked[this.id] || window.location.hash) return
this.innerHTML = ''
clicked[this.id] = true
render()
})
editable[i].addEventListener('input', render)
editable[i].addEventListener('dblclick', function () {
this.innerHTML = ''
render()
})
}
scale.addEventListener('input', render)
quote.addEventListener('input', function () {
var child = this.children
var margin = 0
for (var i = 0; i < child.length; i++) {
margin += 15 + (i*2.5)
child[i].style.marginLeft = margin + 'px'
}
})
}
function drop (e) {
e.preventDefault()
var load = loadImage(e.dataTransfer.files[0])
if (load) {
scale.style.visibility = 'visible'
quote.textContent = 'Click text to edit'
author.textContent = 'Anonymous'
file.disabled = true
handleEvents()
} else {
quote.textContent = 'Invalid image'
render()
}
}
function init () {
ctx.textBaseline = 'top'
scale.style.visibility = 'hidden'
render()
// Handle image drop
document.addEventListener('dragover', function (e) {e.preventDefault()})
document.addEventListener('drop', drop)
file.addEventListener('change', function (e) {
e.dataTransfer = {files: this.files}
drop(e)
})
}
init()
</script>
</body>
</html>