-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
139 lines (91 loc) · 2.96 KB
/
app.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
// Register A service worker
if ('serviceWorker' in navigator) {
window.addEventListener('load', function() {
const prefix = location.pathname.replace(/\/(index\.html)?$/, '');
navigator.serviceWorker.register(`${prefix}/sw.js`)
.then(function(registration) {
// Registration was successful
console.log('[success] scope: ', registration.scope);
}, function(err) {
// registration failed :(
console.log('[fail]: ', err);
});
});
}
console.log('index.js modified');
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#blah').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
$("#imgInp").change(function () {
readURL(this);
});
var memeSize = 300;
var canvas = document.getElementById('memecanvas');
ctx = canvas.getContext('2d');
// Set the text style to that to which we are accustomed
canvas.width = memeSize;
canvas.height = memeSize;
// Grab the nodes
var img = document.getElementById('blah');
var topText = document.getElementById('top-text');
var bottomText = document.getElementById('bottom-text');
// When the image has loaded...
img.onload = function() {
drawMeme()
}
topText.addEventListener('keydown', drawMeme)
topText.addEventListener('keyup', drawMeme)
topText.addEventListener('change', drawMeme)
bottomText.addEventListener('keydown', drawMeme)
bottomText.addEventListener('keyup', drawMeme)
bottomText.addEventListener('change', drawMeme)
function drawMeme() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(img, 0, 0, memeSize, memeSize);
ctx.lineWidth = 4;
ctx.font = '20pt sans-serif';
ctx.strokeStyle = 'black';
ctx.fillStyle = 'white';
ctx.textAlign = 'center';
ctx.textBaseline = 'top';
var text1 = document.getElementById('top-text').value;
text1 = text1.toUpperCase();
x = memeSize / 2;
y = 0;
wrapText(ctx, text1, x, y, 300, 28, false);
ctx.textBaseline = 'bottom';
var text2 = document.getElementById('bottom-text').value;
text2 = text2.toUpperCase();
y = memeSize;
wrapText(ctx, text2, x, y, 300, 28, true);
}
function wrapText(context, text, x, y, maxWidth, lineHeight, fromBottom) {
var pushMethod = (fromBottom)?'unshift':'push';
lineHeight = (fromBottom)?-lineHeight:lineHeight;
var lines = [];
var y = y;
var line = '';
var words = text.split(' ');
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) {
lines[pushMethod](line);
line = words[n] + ' ';
} else {
line = testLine;
}
}
lines[pushMethod](line);
for (var k in lines) {
context.strokeText(lines[k], x, y + lineHeight * k);
context.fillText(lines[k], x, y + lineHeight * k);
}
}