HTML5 Canvas, JavaScript and operations on images


Level: intermediate

Some time ago we got a small job. The subject: small research and creation of simple on-line editor for images, which should be editable from HTML5 Canvas and JavaScript level. Then the user should be able to save / send the final image. Today we present the basic elements and some examples related to this subject.

HTML5 Canvas, JavaScript and images

Basically we need (all the necessary libraries are included in our ready example):

– advanced JavaScript libraries: base64.js, canvas2image.js, jsmanipulate.js,

– PHP files, where we will process the data,

– images (jpg, png) to process.

Steps to do:

1. Load image from file JPG or PNG file to the HTML5 Canvas element (or draw a simple picture directly, using Canvas methods)

2. Perform some graphics operations on this picture

3. Get the modified contents of the Canvas, so that you can save it in a file

The 3rd point was most important in our case.

A whole magic was performed through one simple method – toDataURL() of the HTML5 Canvas object:

var strDataURI = oCanvas.toDataURL();
// returns "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgA..."

This method returns the Canvas content as data: URI. This allows for further operations, such as converting and saving the data in the form of the final file.

The data: URI string can be used directly as src attribute of image tag, and displayed in the browser:

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgA... " />

Let’s go to the practice and sample code.

Example – simple UI:

<div id="wrapper">
  <canvas id="cnv" width="200" height="200"></canvas>
  <br /><br />
  <div id="tb"><button onclick="testing()">Test</button></div>


var canvas = document.getElementById('cnv');

if (canvas.getContext) {
  var context = canvas.getContext('2d');

  // simple drawing on canvas
} else {
  alert('Error in canvas context loading');

// the handler
function testing() {

  // save data as PNG (default)
  var strDataPng = canvas.toDataURL();
  // alert(strDataPng);

  var my_url = "processor_png.php";

    type: "POST",
    url: my_url,
    data: { filecode: strDataPng }
  }).done(function( msg ) {
    alert("PNG file saved!");

  // ...

In this example, we draw a completely new picture, only with HTML5 Canvas methods. Then we get the data representing this image and we send it via AJAX to PHP script, which saves these data to the file.

A simple example of such PHP script:

if (isset($_POST["filecode"])) {
  // $imageData = $GLOBALS['HTTP_RAW_POST_DATA'];
  $imageData = $_POST["filecode"];

  // cleanups 
  $filteredData = substr($imageData, strpos($imageData, ",") + 1);

  // decode right data
  $unencodedData = base64_decode($filteredData);

  // output
  $fp = fopen("tests/file_png.png", "wb");
  fwrite($fp, $unencodedData);

This code fetches and decodes the data and then saves it to a file on the server.

On-line examples:

– JPG file test

– PNG file test

Examples for download:

The elements of this case study were inspired by, among others, the articles from the page.


That’s all for today. I hope the discussed topic was interesting and will help someone with issues related to processing, getting and saving the data from HTML5 Canvas element.

Thank you and have fun!

Web and Mobile Software Developer, with years of experience. My biggest passions are programming, new technologies, e-commerce, as well sports, cars, and of course my family.

Facebook Twitter LinkedIn Google+ Skype Xing 

Share: Share on Facebook10Tweet about this on TwitterShare on Google+1Share on LinkedIn8Share on Tumblr0Digg thisEmail this to someonePin on Pinterest1
You can leave a response, or trackback from your own site.