Adding SwingJS interface and utility classes
[jalview.git] / src / swingjs / api / js / HTML5Canvas.java
1 package swingjs.api.js;
2
3 import java.awt.image.BufferedImage;
4
5 public interface HTML5Canvas extends DOMNode {
6
7         HTML5CanvasContext2D getContext(String str2d);
8
9         /*
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.
12          * 
13          */
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);
18                 }
19                 HTML5CanvasContext2D ctx = canvas.getContext("2d");
20                 if (sourceNode != null) {
21                         ctx.drawImage(sourceNode, 0, 0, w, h);
22                 }
23                 // Coerse int[] to byte[]
24                 return (byte[]) (Object) ctx.getImageData(0, 0, w, h).data;
25         }
26
27         /**
28          * Install a source image (img, video, or canvas) into a matching BufferedImage 
29          * 
30          * @param sourceNode
31          * @param image
32          */
33         static void setImageNode(DOMNode sourceNode, BufferedImage image) {
34                 /**
35                  * @j2sNative
36                  * 
37                  *                      image._setImageNode$O$Z(sourceNode, false);
38                  * 
39                  */             {
40                         // image._setImageNode(sourceNode, false);
41                  }
42         }
43         
44         
45
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");
49                 /**
50                  * @j2sNative
51                  * 
52                  * canvas.width = width;
53                  * canvas.height = height;
54                  * 
55                  */
56                 return canvas;
57         }
58
59 }