X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fswingjs%2Fapi%2Fjs%2FHTML5Canvas.java;fp=src%2Fswingjs%2Fapi%2Fjs%2FHTML5Canvas.java;h=f6d06041319f709f8ecd78378d7a8ee4879be31c;hb=586ade46bdcd05ff028a1cff82c3c527326d28ec;hp=0000000000000000000000000000000000000000;hpb=adcef27f5747b4e70e89a56c3735bc3afb8ce9bf;p=jalview.git diff --git a/src/swingjs/api/js/HTML5Canvas.java b/src/swingjs/api/js/HTML5Canvas.java new file mode 100644 index 0000000..f6d0604 --- /dev/null +++ b/src/swingjs/api/js/HTML5Canvas.java @@ -0,0 +1,59 @@ +package swingjs.api.js; + +import java.awt.image.BufferedImage; + +public interface HTML5Canvas extends DOMNode { + + HTML5CanvasContext2D getContext(String str2d); + + /* + * Retrieves the byte[] data buffer from an HTML5 CANVAS element, optionally + * first setting its contents to a source IMG, CANVAS, or VIDEO element. + * + */ + static byte[] getDataBufferBytes(HTML5Canvas canvas, DOMNode sourceNode, int w, int h) { + if (sourceNode != null) { + DOMNode.setAttrInt(canvas, "width", w); + DOMNode.setAttrInt(canvas, "height", h); + } + HTML5CanvasContext2D ctx = canvas.getContext("2d"); + if (sourceNode != null) { + ctx.drawImage(sourceNode, 0, 0, w, h); + } + // Coerse int[] to byte[] + return (byte[]) (Object) ctx.getImageData(0, 0, w, h).data; + } + + /** + * Install a source image (img, video, or canvas) into a matching BufferedImage + * + * @param sourceNode + * @param image + */ + static void setImageNode(DOMNode sourceNode, BufferedImage image) { + /** + * @j2sNative + * + * image._setImageNode$O$Z(sourceNode, false); + * + */ { + // image._setImageNode(sourceNode, false); + } + } + + + + static HTML5Canvas createCanvas(int width, int height, String id) { + HTML5Canvas canvas = (HTML5Canvas) DOMNode.createElement("canvas", (id == null ? "img" + Math.random() : id + "")); + DOMNode.setStyles(canvas, "width", width + "px", "height", height + "px"); + /** + * @j2sNative + * + * canvas.width = width; + * canvas.height = height; + * + */ + return canvas; + } + +}