Creating Server-side Images with Jaxer

Introduction

Jaxer is built on the same technology as Firefox which means we can take advantage of XUL-like functionality on the server. In this article, we show a small snippet of JavaScript that creates a simple chart and then injects that into an img element as a data URL. Once the page is received by the client, they'll see an inlined chart that was created completely on the server using only Jaxer and JavaScript.

The Code

The following snippet of JavaScript is the high-level view of the creation of an image and the injection of the result into an image tag.

The Result

Below is the image that is created when running this sample. If you view the source of the generated page, you will see the data url that was generated on the server.

Conclusion

Jaxer makes it easy to inline server-side generate images due to the rich functionality provided by the Mozilla code-base. As seen here, developers can make use of the canvas element and its associate 2D drawing context to create images on the fly before a page is sent to the client. There are many interesting examples of the canvas element in use online. Now JavaScript developers can take advantage of that on the server without having to resort to other server-side languages or executables.