A few days ago while I was working on updating my resume I thought about ways to add my simple “CW” (short for Chris West of course) logo to it. At the time of writing this article I presented my logo on CWestify.com using CSS3:




Leveraging the Canvas

One great thing that Mozilla has promoted for years is the <canvas>. As long as the canvas is not tainted it can turn its contents into an image (PNG or JPG). The great thing about using PNGs is their support of transparency and partial transparency. So the question is, “how do you draw HTML on a <canvas>?” Without using a library you can actually do so by inlining the styles and then turning the HTML code into SVG:



  
    
CW

Next we can either get the outerHTML of this SVG element or we can store the value directly in JavaScript (I will choose the latter for this example):


var data = '\
  \
    \
      
\
CW
\
\
\
';

Now we need to turn this SVG into something the <canvas> will display. The <canvas> will display images so we can load the SVG code into an image’s source:


// Define an image and set the source of the image to the SVG
var img = new Image();
img.src = 'data:image/svg+xml;base64,' + window.btoa(data);

Once the image object loads, we can then draw it onto the <canvas>:


// Variable to store the data URI for the PNG
var strPNG;

// Define an image
var img = new Image();

// Once the image loads draw it onto a new canvas
img.onload = function() {
  // Create a canvas
  var canvas = document.createElement('canvas');
  canvas.style.height = 0;
  canvas.height = 300;
  canvas.width = 300;

  // Draw the image onto the canvas
  canvas.getContext('2d').drawImage(img, 0, 0);

  // Store the image's data URI in a variable for later
  strPNG = canvas.toDataURL();
};

// Set the source of the image to the SVG data
img.src = 'data:image/svg+xml;base64,' + window.btoa(data);

Of course, since we are creating a <canvas> without adding it to the DOM we wanted to immediately use it to get its data URI and store it off into a variable (strPNG) for later use. All together the code looks like this:


var data = '\
  \
    \
      
\
CW
\
\
\
'; // Define an image var img = new Image(); // Once the image loads draw it onto a new canvas img.onload = function() { // Create a canvas var canvas = document.createElement('canvas'); canvas.style.height = 0; canvas.height = 300; canvas.width = 300; // Draw the image onto the canvas canvas.getContext('2d').drawImage(img, 0, 0); // Do something useful with the PNG's URI doSomethingWithLogoURI(canvas.toDataURL()); }; // Set the source of the image to the SVG data img.src = 'data:image/svg+xml;base64,' + window.btoa(data);

As you can see in the code above, after drawing the image on the canvas we call the doSomethingWithLogoURI() function so that it can use it.

Resulting PNG Image

Using this method produced an image in Chrome which I then saved off and uploaded to this blog so that regardless of the browser’s CSS3 capabilities my logo will always look the same:

CWestify Logo

CWestify Logo

Conclusion

Now we know that as long as we don’t taint the canvas with some outside source, we can basically turn any HTML element into a PNG. If we wanted to we could even use the method I outlined a while back to make such an image downloadable upon clicking on it. Perhaps this may open up your mind to other cool applications. Have fun! šŸ˜Ž

Categories: BlogJavaScript

Leave a Reply

Your email address will not be published. Required fields are marked *