Untitled


SUBMITTED BY: Guest

DATE: Feb. 26, 2014, 7:39 p.m.

FORMAT: JavaScript

SIZE: 5.6 kB

HITS: 834

  1. /*
  2. Meme.js
  3. =======
  4. Use one function to generate a meme.
  5. You can call it all with strings:
  6. Meme('dog.jpg', 'canvasID', 'Buy pizza, 'Pay in snakes');
  7. Or with a selected canvas element:
  8. var canvas = document.getElementById('canvasID');
  9. Meme('wolf.jpg', canvas, 'The time is now', 'to take what\'s yours');
  10. Or with a jQuery/Zepto selection:
  11. Meme('spidey.jpg', $('#canvasID'), 'Did someone say', 'Spiderman JS?');
  12. You can also pass in an image:
  13. var img = new Image();
  14. img.src = 'insanity.jpg';
  15. var can = document.getElementById('canvasID');
  16. Meme(img, can, 'you ignore my calls', 'I ignore your screams of mercy');
  17. ********************************************************************************
  18. Copyright (c) 2012 BuddyMeme
  19. Permission is hereby granted, free of charge, to any person obtaining a copy of
  20. this software and associated documentation files (the "Software"), to deal in
  21. the Software without restriction, including without limitation the rights to
  22. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  23. the Software, and to permit persons to whom the Software is furnished to do so,
  24. subject to the following conditions:
  25. The above copyright notice and this permission notice shall be included in all
  26. copies or substantial portions of the Software.
  27. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  28. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  29. FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  30. COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  31. IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  32. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  33. */
  34. window.Meme = function(image, canvas, top, bottom) {
  35. /*
  36. Default top and bottom
  37. */
  38. top = top || '';
  39. bottom = bottom || '';
  40. /*
  41. Deal with the canvas
  42. */
  43. // If it's nothing, set it to a dummy value to trigger error
  44. if (!canvas)
  45. canvas = 0;
  46. // If it's a string, conver it
  47. if (canvas.toUpperCase)
  48. canvas = document.getElementById(canvas);
  49. // If it's jQuery or Zepto, convert it
  50. if (($) && (canvas instanceof $))
  51. canvas = canvas[0];
  52. // Throw error
  53. if (!(canvas instanceof HTMLCanvasElement))
  54. throw new Error('No canvas selected');
  55. // Get context
  56. var context = canvas.getContext('2d');
  57. /*
  58. Deal with the image
  59. */
  60. // If there's no image, set it to a dummy value to trigger an error
  61. if (!image)
  62. image = 0;
  63. // Convert it from a string
  64. if (image.toUpperCase) {
  65. var src = image;
  66. image = new Image();
  67. image.src = src;
  68. }
  69. // Set the proper width and height of the canvas
  70. var setCanvasDimensions = function(w, h) {
  71. canvas.width = w;
  72. canvas.height = h;
  73. };
  74. setCanvasDimensions(image.width, image.height);
  75. /*
  76. Draw a centered meme string
  77. */
  78. var drawText = function(text, topOrBottom, y) {
  79. // Variable setup
  80. topOrBottom = topOrBottom || 'top';
  81. var fontSize = (canvas.height / 8);
  82. var x = canvas.width / 2;
  83. if (typeof y === 'undefined') {
  84. y = fontSize;
  85. if (topOrBottom === 'bottom')
  86. y = canvas.height - 10;
  87. }
  88. // Should we split it into multiple lines?
  89. if (context.measureText(text).width > (canvas.width * 1.1)) {
  90. // Split word by word
  91. var words = text.split(' ');
  92. var wordsLength = words.length;
  93. // Start with the entire string, removing one word at a time. If
  94. // that removal lets us make a line, place the line and recurse with
  95. // the rest. Removes words from the back if placing at the top;
  96. // removes words at the front if placing at the bottom.
  97. if (topOrBottom === 'top') {
  98. var i = wordsLength;
  99. while (i --) {
  100. var justThis = words.slice(0, i).join(' ');
  101. if (context.measureText(justThis).width < (canvas.width * 1.0)) {
  102. drawText(justThis, topOrBottom, y);
  103. drawText(words.slice(i, wordsLength).join(' '), topOrBottom, y + fontSize);
  104. return;
  105. }
  106. }
  107. }
  108. else if (topOrBottom === 'bottom') {
  109. for (var i = 0; i < wordsLength; i ++) {
  110. var justThis = words.slice(i, wordsLength).join(' ');
  111. if (context.measureText(justThis).width < (canvas.width * 1.0)) {
  112. drawText(justThis, topOrBottom, y);
  113. drawText(words.slice(0, i).join(' '), topOrBottom, y - fontSize);
  114. return;
  115. }
  116. }
  117. }
  118. }
  119. // Draw!
  120. context.fillText(text, x, y, canvas.width * .9);
  121. context.strokeText(text, x, y, canvas.width * .9);
  122. };
  123. /*
  124. Do everything else after image loads
  125. */
  126. image.onload = function() {
  127. // Set dimensions
  128. setCanvasDimensions(this.width, this.height);
  129. // Draw the image
  130. context.drawImage(image, 0, 0);
  131. // Set up text variables
  132. context.fillStyle = 'white';
  133. context.strokeStyle = 'black';
  134. context.lineWidth = 2;
  135. var fontSize = (canvas.height / 8);
  136. context.font = fontSize + 'px Impact';
  137. context.textAlign = 'center';
  138. // Draw them!
  139. drawText(top, 'top');
  140. drawText(bottom, 'bottom');
  141. };
  142. };

comments powered by Disqus