forked from BuddyMeme/Meme.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
meme.js
187 lines (142 loc) · 4.88 KB
/
meme.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
/*
Meme.js
=======
Use one function to generate a meme.
You can call it all with strings:
Meme('dog.jpg', 'canvasID', 'Buy pizza, 'Pay in snakes');
Or with a selected canvas element:
var canvas = document.getElementById('canvasID');
Meme('wolf.jpg', canvas, 'The time is now', 'to take what\'s yours');
Or with a jQuery/Zepto selection:
Meme('spidey.jpg', $('#canvasID'), 'Did someone say', 'Spiderman JS?');
You can also pass in an image:
var img = new Image();
img.src = 'insanity.jpg';
var can = document.getElementById('canvasID');
Meme(img, can, 'you ignore my calls', 'I ignore your screams of mercy');
********************************************************************************
Copyright (c) 2012 BuddyMeme
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
window.Meme = function(image, canvas, top, bottom) {
/*
Default top and bottom
*/
top = top || '';
bottom = bottom || '';
/*
Deal with the canvas
*/
// If it's nothing, set it to a dummy value to trigger error
if (!canvas)
canvas = 0;
// If it's a string, conver it
if (canvas.toUpperCase)
canvas = document.getElementById(canvas);
// If it's jQuery or Zepto, convert it
if (($) && (canvas instanceof $))
canvas = canvas[0];
// Throw error
if (!(canvas instanceof HTMLCanvasElement))
throw new Error('No canvas selected');
// Get context
var context = canvas.getContext('2d');
/*
Deal with the image
*/
// If there's no image, set it to a dummy value to trigger an error
if (!image)
image = 0;
// Convert it from a string
if (image.toUpperCase) {
var src = image;
image = new Image();
image.src = src;
}
// Set the proper width and height of the canvas
var setCanvasDimensions = function(w, h) {
canvas.width = w;
canvas.height = h;
};
setCanvasDimensions(image.width, image.height);
/*
Draw a centered meme string
*/
var drawText = function(text, topOrBottom, y) {
// Variable setup
topOrBottom = topOrBottom || 'top';
var fontSize = (canvas.height / 8);
var x = canvas.width / 2;
if (typeof y === 'undefined') {
y = fontSize;
if (topOrBottom === 'bottom')
y = canvas.height - 10;
}
// Should we split it into multiple lines?
if (context.measureText(text).width > (canvas.width * 1.1)) {
// Split word by word
var words = text.split(' ');
var wordsLength = words.length;
// Start with the entire string, removing one word at a time. If
// that removal lets us make a line, place the line and recurse with
// the rest. Removes words from the back if placing at the top;
// removes words at the front if placing at the bottom.
if (topOrBottom === 'top') {
var i = wordsLength;
while (i --) {
var justThis = words.slice(0, i).join(' ');
if (context.measureText(justThis).width < (canvas.width * 1.0)) {
drawText(justThis, topOrBottom, y);
drawText(words.slice(i, wordsLength).join(' '), topOrBottom, y + fontSize);
return;
}
}
}
else if (topOrBottom === 'bottom') {
for (var i = 0; i < wordsLength; i ++) {
var justThis = words.slice(i, wordsLength).join(' ');
if (context.measureText(justThis).width < (canvas.width * 1.0)) {
drawText(justThis, topOrBottom, y);
drawText(words.slice(0, i).join(' '), topOrBottom, y - fontSize);
return;
}
}
}
}
// Draw!
context.fillText(text, x, y, canvas.width * .9);
context.strokeText(text, x, y, canvas.width * .9);
};
/*
Do everything else after image loads
*/
image.onload = function() {
// Set dimensions
setCanvasDimensions(this.width, this.height);
// Draw the image
context.drawImage(image, 0, 0);
// Set up text variables
context.fillStyle = 'white';
context.strokeStyle = 'black';
context.lineWidth = 2;
var fontSize = (canvas.height / 8);
context.font = fontSize + 'px Impact';
context.textAlign = 'center';
// Draw them!
drawText(top, 'top');
drawText(bottom, 'bottom');
};
};