Edit HTML5 Canvas in Java

What is HTML Canvas?

The HTML Canvas element is an HTML5 element in an HTML document that is added using the <canvas> tag. This element is part of HTML5 and provides scriptable dynamic rendering of 2D shapes and bitmaps. This is a low-level procedural model that updates a bitmap. HTML5 Canvas, which is used for rendering graphs, game graphics, art, or other visual images on the fly, is in the list of built-in formats of the Aspose.HTML class library.

The <canvas> element must have:

The HTML Canvas element also has other attributes that you can set to configure its behavior and appearance. For example, if you want to to add a border, use a style attribute. The <canvas> element also supports the Global Attributes and in HTML.

HTML5 Canvas – How to Edit in Java

To render canvas as a part of an HTML document, you do not need any particular manipulation. Just render the document as you usually do. The following code snippet demonstrates how to work with a document that contains an HTML5 Canvas element: the HTML <canvas> element has an id, width, height, and style attributes; inside the <canvas> element, there is a script block that contains JavaScript code to draw “Hello World” on the canvas.

  1. Prepare an HTML code with HTML5 Canvas inside and use the java.io.FileWriter class to write the HTML code to a file.
  2. Initialize an HTML document from the file using the HTMLDocument class.
  3. Call the convertHTML(document, options, outputPath) method to convert HTML to PDF with the default save options.
 1// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.HTML-for-Java
 2// Prepare a document with HTML5 Canvas inside and save it to the file 'document.html'
 3String code = "<canvas id=myCanvas width='200' height='100' style='border:1px solid #d3d3d3;'></canvas>" +
 4                "<script>" +
 5                "var c = document.getElementById('myCanvas');" +
 6                "var context = c.getContext('2d');" +
 7                "context.font = '20px Arial';" +
 8                "context.fillStyle = 'red';" +
 9                "context.fillText('Hello World', 40, 50);" +
10                "</script>";
11java.io.FileWriter fileWriter = new java.io.FileWriter("document.html");
12fileWriter.write(code);
13fileWriter.close();
14
15// Initialize an HTML document from the html file
16com.aspose.html.HTMLDocument document = new com.aspose.html.HTMLDocument("document.html");
17
18// Convert HTML to PDF
19com.aspose.html.converters.Converter.convertHTML(document, new com.aspose.html.saving.PdfSaveOptions(), "output.pdf");

Canvas Rendering Context 2D

Besides processing HTML5 Canvas as a part of an HTML document, you can work with canvas directly inside your Java code. Aspose.HTML provides ICanvasRenderingContext2D interface for these operations, which is fully based on the official standard. Following code snippet shows how to use the drawing feature of HTML5 Canvas to render a custom text filled in with the gradient brush.

 1// Create an empty HTML document
 2com.aspose.html.HTMLDocument document = new com.aspose.html.HTMLDocument();
 3
 4// Create the Canvas element
 5com.aspose.html.HTMLCanvasElement canvas = (com.aspose.html.HTMLCanvasElement) document.createElement("canvas");
 6
 7// with a specified size
 8canvas.setWidth(300);
 9canvas.setHeight(150);
10
11// and append it to the document body
12document.getBody().appendChild(canvas);
13
14// Get the canvas rendering context to draw
15com.aspose.html.dom.canvas.ICanvasRenderingContext2D context = (com.aspose.html.dom.canvas.ICanvasRenderingContext2D) canvas.getContext("2d");
16
17// Prepare the gradient brush
18com.aspose.html.dom.canvas.ICanvasGradient gradient = context.createLinearGradient(0, 0, canvas.getWidth(), 0);
19gradient.addColorStop(0, "magenta");
20gradient.addColorStop(0.5, "blue");
21gradient.addColorStop(1.0, "red");
22
23// Assign brush to the content
24context.setFillStyle(gradient);
25context.setStrokeStyle(gradient);
26
27// Write the Text
28context.fillText("Hello World!", 10, 90, 500);
29
30// Fill the Rectangle
31context.fillRect(0, 95, 300, 20);
32
33// Create the PDF output device
34com.aspose.html.rendering.pdf.PdfDevice device = new com.aspose.html.rendering.pdf.PdfDevice("canvas.pdf");
35
36// Render HTML5 Canvas to PDF
37document.renderTo(device);

To learn more about how to use HTML5 Canvas, you can read the popular public HTML Canvas Tutorial.

You can download the complete examples and data files from GitHub.

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.