Guest User

MemeForSanders

a guest
Feb 24th, 2016
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. <script type="text/javascript">// <![CDATA[
  3.     // Original inspiration from https://mobiforge.com/design-development/html5-canvas-meme-generator
  4.  
  5.     // Canvas where the pictures are drawn
  6.     var canvas;
  7.     var canvasWidth;
  8.     var ctx;
  9.  
  10.     // Canvas where the text is drawn
  11.     // Credit for double canvas idea to http://stackoverflow.com/a/26255416/1629241
  12.     var textCanvas;
  13.     var textCtx;
  14.  
  15.     // Dimensions of margins and objects within the canvas
  16.     var topMargin;
  17.     var leftMargin;
  18.     var rightMargin;
  19.     var middleMargin;
  20.     var bottomMargin;
  21.     var destinationWidth;
  22.     var destinationHeight;
  23.     var proportions;
  24.     var allowedHorizontalForImg;
  25.     var cutOnEachSideForImg;
  26.     var allowedHorizontalForImg2;
  27.     var cutOnEachSideForImg2;
  28.  
  29.  
  30.     // Initial setup of the meme
  31.     window.onload = function() {
  32.  
  33.         // We have a canvas where the keep the pictures, and another
  34.         // canvas on top of it where we keep the text. The text canvas
  35.         // is cleared on each keystroke. The pictures are merged into
  36.         // the text canvas whenever the user wants to save.
  37.  
  38.  
  39.         canvas = document.getElementById('memecanvas');
  40.         ctx = canvas.getContext('2d');
  41.         var deviceWidth = window.innerWidth;
  42.  
  43.         // Draw the canvas
  44.         // TODO: Larger maximum size?
  45.         canvasWidth = Math.min(600, deviceWidth - 20);
  46.         canvasHeight = Math.min(480, deviceWidth - 20);
  47.         canvas.width = canvasWidth;
  48.         canvas.height = canvasHeight;
  49.  
  50.         // - Compute margins around the pictures -
  51.  
  52.         // Opening question goes in here
  53.         topMargin = canvas.height * 0.15;
  54.  
  55.         // Space at the side of the pictures
  56.         leftMargin = canvas.width * 0.05;
  57.         rightMargin = canvas.width * 0.05;
  58.         middleMargin = canvas.width * 0.10;
  59.  
  60.         // Call to action goes here
  61.         bottomMargin = canvas.height * 0.15;
  62.  
  63.  
  64.         // - Set up the text customization boxes -
  65.         var textSetters = document.getElementById("text-customization");
  66.         textSetters.width=canvas.width;
  67.         textSetters.style.position = "absolute";
  68.         textSetters.style.left = leftMargin + "px";
  69.         textSetters.style.top = (canvas.height + 30) + "px";
  70.  
  71.  
  72.         // - Crop the pictures and draw them -
  73.         drawImages(canvas);
  74.  
  75.         // - Be responsive to text update in real time -
  76.         var textBoxesIds = ["custom-text-1", "custom-text-2", "custom-text-3", "custom-text-4", "custom-text-5"];
  77.         for (var i = 0; i < textBoxesIds.length; i++) {
  78.             element = document.getElementById(textBoxesIds[i]);
  79.             element.addEventListener('keyup', updateText, false);
  80.         }
  81.         element = document.getElementById('language-select');
  82.         element.addEventListener('change', updateText, false);
  83.         element = document.getElementById('background-select');
  84.         element.addEventListener('change', backgroundDraw, false);
  85.  
  86.         // - Set up the call to action and the background color -
  87.         initialTextDraw();
  88.         backgroundDraw();
  89.  
  90.  
  91.         // - Set up downloading -
  92.  
  93.         function handleCanvasMerge(e) {
  94.             // We draw the images into the text canvas
  95.             textCanvas.getContext('2d').fillStyle = canvas.style.backgroundColor;
  96.             textCanvas.getContext('2d').fillRect(0, 0, textCanvas.width, textCanvas.height);
  97.             drawImages(textCanvas);
  98.             updateText(true);
  99.         }
  100.  
  101.         // Saving by right click
  102.         textCanvas.addEventListener('contextmenu', handleCanvasMerge);
  103.  
  104.         // Saving with download button
  105.         // Credit for buttom styling to http://stackoverflow.com/questions/710089/how-do-i-make-an-html-link-look-like-a-button
  106.         element = document.getElementById('button-download');
  107.         element.addEventListener('click', function(e) {
  108.             handleCanvasMerge(e);
  109.             var dataURL = textCanvas.toDataURL('image/png');
  110.             document.getElementById('button-download').href = dataURL;
  111.         })
  112.  
  113.         // TODO: Sharing buttons?
  114.         // TODO: Improve design?
  115.  
  116.     }
  117.  
  118.  
  119.     // Draw images on canvas
  120.     function drawImages(canvasToDraw) {
  121.  
  122.         canvasToDrawCtx = canvasToDraw.getContext('2d');
  123.  
  124.         img = document.getElementById('bern-image');
  125.         img2 = document.getElementById('hill-image');
  126.  
  127.         // Proportions for the pictures within the canvas, horizontal divided by vertical
  128.         destinationWidth = (canvasToDraw.width - leftMargin - rightMargin - middleMargin) * 0.5;
  129.         destinationHeight = canvasToDraw.height - topMargin - bottomMargin;
  130.         proportions = ( destinationWidth / (canvasToDraw.height - topMargin - bottomMargin));
  131.  
  132.         // TODO: Also crop vertically if using different pictures?
  133.         // Determine how do we crop the original pictures, and draw them
  134.         allowedHorizontalForImg = img.height * proportions;
  135.         cutOnEachSideForImg = (img.width - allowedHorizontalForImg) / 2.0;
  136.         canvasToDrawCtx.drawImage(img, cutOnEachSideForImg, 0, img.width - 2.0*cutOnEachSideForImg, img.height,
  137.                     leftMargin, topMargin, destinationWidth, destinationHeight);
  138.  
  139.         allowedHorizontalForImg2 = img2.height * proportions;
  140.         cutOnEachSideForImg2 = (img2.width - allowedHorizontalForImg2) / 2.0;
  141.         canvasToDrawCtx.drawImage(img2, cutOnEachSideForImg2, 0, img2.width - 2.0*cutOnEachSideForImg2, img2.height,
  142.                     leftMargin + middleMargin + destinationWidth, topMargin, destinationWidth, destinationHeight);
  143.  
  144.     }
  145.  
  146.  
  147.     // Set up the background color and the call to action text
  148.     function backgroundDraw() {
  149.  
  150.         // This resets the background drawn in the text canvas when we export the image
  151.         updateText();
  152.  
  153.         var canvasForBackground = document.getElementById('memecanvas');
  154.         var backgroundColor = document.getElementById('background-select');
  155.         if ( (backgroundColor.color[0].checked && backgroundColor.color[0].value == "black") ||
  156.                 (backgroundColor.color[1].checked && backgroundColor.color[1].value == "black")){
  157.             canvasForBackground.style.background = "black";
  158.         } else {
  159.             canvasForBackground.style.background = "white";
  160.         }
  161.  
  162.     }
  163.  
  164.  
  165.     // Set up the call to action text at the bottom
  166.     function initialTextDraw(doNotClearPrevious) {
  167.  
  168.         // Set up the text canvas
  169.         textCanvas = document.getElementById('textcanvas');
  170.         textCxt = textCanvas.getContext('2d');
  171.  
  172.  
  173.         // We clear previous text on a text update, but not on preparation for a save
  174.         if (doNotClearPrevious != true) {
  175.             textCanvas.height = canvas.height;
  176.             textCanvas.width = canvas.width;
  177.             textCxt.clearRect(0, 0, textCanvas.width, textCanvas.height);
  178.             textCanvas.style.background = "transparent";
  179.         }
  180.  
  181.  
  182.         // We draw the call to action
  183.         textCxt.lineWidth = 3;
  184.         textCxt.strokeStyle = 'black';
  185.         textCxt.fillStyle = 'white';
  186.         textCxt.textAlign = 'center';
  187.         textCxt.font = '30pt sans-serif';
  188.  
  189.  
  190.         // Figure out the location and language for the text
  191.         var horizontalEdgeForText = textCanvas.width * 0.5;
  192.         YForText = destinationHeight + topMargin + 0.6 * bottomMargin;
  193.         var callToAction = document.getElementById('language-select');
  194.         var callToActionText = "";
  195.         if ( (callToAction.language[0].checked && callToAction.language[0].value == "English") ||
  196.                 (callToAction.language[1].checked && callToAction.language[1].value == "English")){
  197.             callToActionText = "VOTE BERNIE";
  198.         } else {
  199.             callToActionText = "SANDERS PRESIDENTE";
  200.         }
  201.  
  202.  
  203.         textCxt.strokeText(callToActionText, horizontalEdgeForText, YForText);
  204.         textCxt.fillText(callToActionText, horizontalEdgeForText, YForText);
  205.     }
  206.  
  207.  
  208.     // Set up the question and answers in the meme
  209.     function updateText(doNotClearPrevious) {
  210.  
  211.         // - Compute the question and answers, with nothing shown if the default text is still set -
  212.  
  213.         // Sets up the canvas and the call to action
  214.  
  215.         initialTextDraw(doNotClearPrevious);
  216.  
  217.         // Determine question we show candidates answers to
  218.         var question = document.getElementById('custom-text-1').value
  219.         if (question == "Question") {
  220.             question = "";
  221.         }
  222.  
  223.         // Determine Bernie answers
  224.         var answerTop1 = document.getElementById('custom-text-2').value;
  225.         if (answerTop1 == "Bernie answer, line 1") {
  226.             answerTop1 = "";
  227.         }
  228.         var answerBottom1 = document.getElementById('custom-text-4').value;
  229.         if (answerBottom1 == "Bernie answer, line 2 (optional)") {
  230.             answerBottom1 = "";
  231.         }
  232.  
  233.         // Determine Hillary answers
  234.         var answerTop2 = document.getElementById('custom-text-3').value;
  235.         if (answerTop2 == "Hillary answer, line 1") {
  236.             answerTop2 = "";
  237.         }
  238.         var answerBottom2 = document.getElementById('custom-text-5').value;
  239.         if (answerBottom2 == "Hillary answer, line 2 (optional)") {
  240.             answerBottom2 = "";
  241.         }
  242.  
  243.         // - Display the question and answers -
  244.  
  245.         // Style
  246.         textCxt.lineWidth = 3;
  247.         textCxt.font = '30pt sans-serif';
  248.         textCxt.strokeStyle = 'black';
  249.         textCxt.fillStyle = 'white';
  250.         textCxt.textAlign = 'center';
  251.  
  252.         // Question
  253.         var horizontalEdgeForText = textCanvas.width/2;
  254.         if (question.length > 30) {
  255.             textCxt.font = '25pt sans-serif';
  256.         }
  257.         textCxt.strokeText(question, horizontalEdgeForText, topMargin * 0.5);
  258.         textCxt.fillText(question, horizontalEdgeForText, topMargin * 0.5);
  259.  
  260.  
  261.         // Answers
  262.         textCxt.font = '20pt sans-serif';
  263.         horizontalEdgeForText = leftMargin + destinationWidth * 0.5;
  264.         // Answers in different position depending on whether there are one or two of them for
  265.         // each candidate
  266.         if (answerBottom1 != "") {
  267.             var highestYForText = destinationHeight * 0.33 + topMargin;
  268.             textCxt.strokeText(answerTop1, horizontalEdgeForText, highestYForText);
  269.             textCxt.fillText(answerTop1, horizontalEdgeForText, highestYForText);
  270.  
  271.             var lowestYForText = destinationHeight*0.67 + topMargin;
  272.             textCxt.strokeText(answerBottom1, horizontalEdgeForText, lowestYForText);
  273.             textCxt.fillText(answerBottom1, horizontalEdgeForText, lowestYForText);
  274.         } else {
  275.             var YForText = (destinationHeight * 0.5) + topMargin;
  276.             textCxt.strokeText(answerTop1, horizontalEdgeForText, YForText);
  277.             textCxt.fillText(answerTop1, horizontalEdgeForText, YForText);
  278.         }
  279.  
  280.         horizontalEdgeForText = leftMargin + destinationWidth + middleMargin + destinationWidth * 0.5;
  281.         if (answerBottom2 != "") {
  282.             var highestYForText = destinationHeight * 0.33 + topMargin;
  283.             textCxt.strokeText(answerTop2, horizontalEdgeForText, highestYForText);
  284.             textCxt.fillText(answerTop2, horizontalEdgeForText, highestYForText);
  285.  
  286.             var lowestYForText = destinationHeight*0.67 + topMargin;
  287.             textCxt.strokeText(answerBottom2, horizontalEdgeForText, lowestYForText);
  288.             textCxt.fillText(answerBottom2, horizontalEdgeForText, lowestYForText);
  289.         } else {
  290.             var YForText = destinationHeight * 0.5 + topMargin;
  291.             textCxt.strokeText(answerTop2, horizontalEdgeForText, YForText);
  292.             textCxt.fillText(answerTop2, horizontalEdgeForText, YForText);
  293.         }
  294.  
  295.     };
  296.  
  297.  
  298. // ]]&gt;</script>
  299.  
  300.  
  301.  
  302.  
  303. <img id="bern-image" style="display: none;" src="http://i.imgur.com/G86fBCV.jpg" crossorigin="anonymous" alt =""  />
  304.  
  305.  
  306. <img id="hill-image" style="display: none;" src="http://i.imgur.com/bGYckyw.jpg" crossorigin="anonymous"  alt =""  />
  307.  
  308. <div style="position: relative;">
  309.  
  310. <canvas id="memecanvas" style="position: absolute; left: 0; top: 0; z-index:0;">
  311.     It doesn't seem possible to display this on your device. Sorry!
  312. </canvas>
  313.  
  314. <canvas id="textcanvas" style="position: absolute; left: 0; top: 0; z-index:1;">
  315. </canvas>
  316. </div>
  317.  
  318. <br>
  319.  
  320. <div id="text-customization" style="font-family: 'Century Gothic', CenturyGothic, AppleGothic, sans-serif; font-size: 14px">
  321.  
  322.     <input id="custom-text-1" type="text" value="Question" style="position: static;"> </br>
  323.     <input id="custom-text-2" type="text" value="Bernie answer, line 1" style="width: 300px;" maxlength="20">
  324.     <input id="custom-text-3" type="text" value="Hillary answer, line 1" style="width: 300px;" maxlength="20">
  325.  
  326.     </br>
  327.  
  328.     <input id="custom-text-4" width="300" type="text" value="Bernie answer, line 2 (optional)" style="width: 300px;" maxlength="20">
  329.     <input id="custom-text-5" width="300" type="text" value="Hillary answer, line 2 (optional)" style="width: 300px;" maxlength="20">  </br>
  330.  
  331.  
  332.     <form id="language-select">
  333.         <input type="radio" name="language" value="English" checked="1"> English
  334.         <input type="radio" name="language" value="Spanish"> Espa&#241;ol
  335.     </form>
  336.  
  337.     <form id="background-select">
  338.         <input type="radio" name="color" value="black" checked="1"> Black background
  339.         <input type="radio" name="color" value="white"> White background
  340.     </form>
  341.  
  342.     <a href="#" download="bernie-meme.png" id="button-download" style="text-decoration: none; background-color: #EEEEEE; color: #333333;padding: 0px 6px 0px 6px; border-top: 1px solid #CCCCCC; border-right: 1px solid #333333; border-bottom: 1px solid #333333; border-left: 1px solid #CCCCCC">
  343.         Download
  344.     </a>
  345.  
  346.  
  347.     <p>
  348.         Spread the word around! And remember, many people outside heavy Internet users haven't even heard of Bernie yet.
  349.     </p>
  350.  
  351.  
  352.     <p>
  353.         Turn device sideways for better use in mobile device. If downloading doesn't work in the Android default browser, trying in Firefox or Chrome might fix it.
  354.     </p>
  355.  
  356.  
  357. </div>
Advertisement
Add Comment
Please, Sign In to add comment