ANEXIA Development and Managed Hosting

Meme Generator with jQuery and Canvas

Written on October 24, 2017 by Manuel Wutte

In this tutorial, I will show you how to create a meme generator with the HTML element canvas. You can see the result here in my demo script:


Anyone who doesn’t know what a meme is will find a brief explanation below:

Memes are pseudo images featuring a more or less meaningful text phrase. In terms of topics, there are virtually no limitations, and memes are generally created by chance or based on the author’s own creativity. Memes have become an Internet hype, which can likely be traced back to the fact that they are easy to create.



Canvas is an HTML element with freely definable height and width – like a real physical canvas – which can be “drawn on” with the help of Javascript.

And the functions aren’t restricted to lines and rectangles – circles, gradients, and texts can also be processed. But that’s not all: even Bézier curves and external graphics can be used (to a certain extent).  They can be rescaled or repositioned, or even cropped.

Canvas treats each inserted object/object group as a separate element that can be moved, scaled, or rotated individually.

Meme-Generator mit Canvas und jQuery

With our meme generator, we will be able to select any image or picture and then place two text areas on it using a few slider elements.

Since we will only work with Javascript, we do not need a server-side script or script language (such as PHP) for these basic functions.


Step 1: Creating HTML markup for the configurator

In principle, our meme generator will consist of three parts: an HTML page with configuration options, a minimalistic CSS file (since we will work with the CSS framework Bootstrap), and a Javascript file with our logic.

So, let’s begin with the first part, the HTML configuration page.

Basically, we are creating a Bootstrap grid layout here (as you can see in the example screenshot above), which will display the meme generated with canvas on the left and the configuration options on the right (with the help of slider elements).

As you can see here, all you have to do is define a “canvas” HTML tag for canvas. Optionally, you can also enter inline dimensions directly, instead of via CSS definitions. The text within the canvas tag is only shown in browsers that do not support this technology.

The slider elements are assigned their own ID we will later use via Javascript or jQuery. This not only gives us their set value, but also allows us to set their start and end range dynamically based on the image file’s metadata (its dimensions).


Step 2: Preparing the CSS file

As mentioned before, the CSS file style.css will be very minimalistic since we will use standard Bootstrap elements in this tutorial.

We already embedded the Boostrap framework via its CDN (including the corresponding Javascript resources).

The image with the ID “start-image” is simply the holder for our source image file for canvas rendering. For this reason, we will move it off screen for convenience.


Step 3: Embedding Javascript logic

To be able to embed the Javascript logic, we will first create the file named app.js (already linked in HTML) with the following jQuery framework.

All further steps will take place within this block.

First, we will begin by initializing the canvas element with the ID “meme”:

In the ctx variable, within the jQuery scope we now have the application context for our canvas element, which is available via the canvas variable.

Let’s begin bringing our application to life by creating the main function for generating memes. First, we will create an empty function named drawMeme().

The actual functional logic for this will follow a bit later.

We have already created the two text fields for the upper and lower text areas in our HTML markup as well as the slider input fields for the configuration options, so we will now register our drawMeme() function for all relevant events of these fields.

Primarily, these are changekeyup, and keydown for the fields with text input, as well as the change event for our slider input fields.

The additional input event for these fields ensures that these handlers are also triggered when the slider input field is in use (dragging back and forth using the mouse).

In addition, we will show the value set for these fields in the configuration form by transferring it to the corresponding element with the help of the text function and overwriting its content with new content.

Now, we will begin to gradually structure our drawMeme() function.

We will use the source image file we assigned the ID “start-image” in the img variable. The corresponding Javascript object is now available in this variable.

The reason why we are working with the blank Javascript object instead of the jQuery equivalent is that we need this Javascript object in this intact form for the canvas functions.

In addition, we take the desired size for our meme from the input field, and in the next step we will set the maximum values for our offset configuration options (for adjusting the vertical placement of the texts) to limit the slider elements accordingly.

Next, we will configure the canvas element by defining its dimensions. First, we will use the canvas function drawImage() to transfer the source image file that we saved in the img  variable to this element.

To prevent the source image from being skewed, we will use the cropping function available in canvas. We take the maximum value for this right out of our source file by comparing the height and width and determining a suitable size.

Memes are generally square (size defined in the memeSize variable), so the image file should also have a square ratio (size defined in the croppingDimension variable).

In this case, the source image file will be scaled down to the meme dimensions and cropped to a square image. To keep it simple, we will skip any other adjustments for this cropping process.

To be able to position the texts in our meme canvas, we will write a short helper function wrapText() (outside the drawMeme() function):

Basically, we will now transfer the canvas scope via the context parameter, a few parameters that affect the text to be inserted, as well as its position properties.

Here, we want to use the font size and spacing to calculate concrete X and Y coordinates and transfer them to the canvas scope (context variable). You can simply take this over for now.

Back in the drawMeme() function, we will now create the basic settings for the canvas text using our configuration fields from the HTML mask.

Next, we will change the text input to capital letters and enter the desired Y position. The X coordinate is exactly half of the meme width, since the text should be positioned centered.

With the maxTextAreaWidth variable, we will limit the maximum text width to 85%. This is primarily for visual reasons.

We will then repeat this for the bottom text, only that we will no longer redefine the basic font settings, but instead simply overwrite them for the corresponding properties.

So that you can choose your own pictures, we will now give our file input field the corresponding function:

When a new image is selected, the Javascript file reader reads the image in and sets its Base64 string as the source (“src” attribute) of our source image file (with the ID “start-image”).

Next, we will once again trigger the drawMeme() function so that it will read in and process the new image.

Now, something should be happening on the meme generator page if you change one of the slider elements or input fields, since at least one change event is triggered, calling the drawMeme() function.

We will call the drawMeme() function when loading the page so that we will see some changes right when opening the page:

You can download this functioning example here:



Step 4: Adding an optional download link

You can read out the current image as a Base64 string from your canvas element with the help of the toDataURL() function at any time.

One option would be to set this Base64 string as an “href” attribute for your download link and to specify the desired file name using the “download” attribute.

However, note that this is not supported by all browsers and therefore will not work everywhere.

Ideally, you should send this Base64 string via or jQuery.ajax to a server-side script that then returns the string with the corresponding “Content-Type” header (“image/png”).

This would ensure that the function works in all browsers.



Canvas is a powerful tool for drawing dynamic graphics in your browser using Javascript.

You will most frequently find canvas in conjunction with charting libraries, since this technology is well-established in the area of statistics.

Working with canvas is quite easy. The only thing that can get complicated is – as in our meme generator example – correctly determining/calculating the individual positions of the generated elements.