Edit HTML5 Canvas in Java

What is HTML Canvas?

The HTML <canvas> element is an HTML5 element that enables developers to create dynamic, scriptable rendering of 2D shapes and bitmap images directly in a web browser. Aspose.HTML for Java supports this functionality, allowing seamless integration and manipulation of canvas elements in Java applications.

HTML Canvas Key Features

  1. Attributes: The <canvas> element must include:
  1. JavaScript Integration: To create dynamic images or work with graphics, the <canvas> element uses JavaScript for drawing and rendering operations. This allows developers to create highly customizable visual effects using the getContext() method to access the rendering context.

  2. Global Attributes: The <canvas> element supports HTML’s Global Attributes, allowing for greater flexibility in styling and interaction.

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// Prepare a document with HTML5 Canvas inside and save it to the file "document.html"
 2String code = "<canvas id=myCanvas width='200' height='100' style='border:1px solid #d3d3d3;'></canvas>" +
 3        "<script>" +
 4        "var c = document.getElementById('myCanvas');" +
 5        "var context = c.getContext('2d');" +
 6        "context.font = '20px Arial';" +
 7        "context.fillStyle = 'red';" +
 8        "context.fillText('Hello World', 40, 50);" +
 9        "</script>";
10try (java.io.FileWriter fileWriter = new java.io.FileWriter("document.html")) {
11    fileWriter.write(code);
12}
13
14// Initialize an HTML document from the HTML file
15HTMLDocument document = new HTMLDocument("document.html");
16
17// Convert HTML to PDF
18Converter.convertHTML(document, new 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
 2HTMLDocument document = new HTMLDocument();
 3
 4// Create a canvas element
 5HTMLCanvasElement canvas = (HTMLCanvasElement) document.createElement("canvas");
 6
 7// with a specified size
 8canvas.setWidth(300);
 9canvas.setHeight(150);
10
11// Append the canvas element to the document body
12document.getBody().appendChild(canvas);
13
14// Get the canvas rendering context to draw
15ICanvasRenderingContext2D context = (ICanvasRenderingContext2D) canvas.getContext("2d");
16
17// Prepare a gradient brush
18ICanvasGradient 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 the 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 a PDF output device
34PdfDevice device = new PdfDevice("canvas.pdf");
35
36// Render HTML5 Canvas to PDF
37document.renderTo(device);

Recommendations

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.