1 package swingjs.api.js;
3 import java.awt.image.BufferedImage;
5 public interface HTML5Canvas extends DOMNode {
7 HTML5CanvasContext2D getContext(String str2d);
10 * Retrieves the byte[] data buffer from an HTML5 CANVAS element, optionally
11 * first setting its contents to a source IMG, CANVAS, or VIDEO element.
14 static byte[] getDataBufferBytes(HTML5Canvas canvas, DOMNode sourceNode, int w, int h) {
15 if (sourceNode != null) {
16 DOMNode.setAttrInt(canvas, "width", w);
17 DOMNode.setAttrInt(canvas, "height", h);
19 HTML5CanvasContext2D ctx = canvas.getContext("2d");
20 if (sourceNode != null) {
21 ctx.drawImage(sourceNode, 0, 0, w, h);
23 // Coerse int[] to byte[]
24 return (byte[]) (Object) ctx.getImageData(0, 0, w, h).data;
28 * Install a source image (img, video, or canvas) into a matching BufferedImage
33 static void setImageNode(DOMNode sourceNode, BufferedImage image) {
37 * image._setImageNode$O$Z(sourceNode, false);
40 // image._setImageNode(sourceNode, false);
46 static HTML5Canvas createCanvas(int width, int height, String id) {
47 HTML5Canvas canvas = (HTML5Canvas) DOMNode.createElement("canvas", (id == null ? "img" + Math.random() : id + ""));
48 DOMNode.setStyles(canvas, "width", width + "px", "height", height + "px");
52 * canvas.width = width;
53 * canvas.height = height;