Java Code Examples for com.google.gwt.canvas.client.Canvas#createIfSupported()

The following examples show how to use com.google.gwt.canvas.client.Canvas#createIfSupported() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TexturedCube.java    From TGAReader with MIT License 6 votes vote down vote up
Canvas createImageCanvas(int [] pixels, int width, int height) {

	    Canvas canvas = Canvas.createIfSupported();
	    canvas.setCoordinateSpaceWidth(width);
	    canvas.setCoordinateSpaceHeight(height);

	    Context2d context = canvas.getContext2d();
	    ImageData data = context.createImageData(width, height);

	    CanvasPixelArray array = data.getData();
	    for(int i=0; i<width*height; i++) {
	        array.set(4*i+0, pixels[i] & 0xFF);
	        array.set(4*i+1, (pixels[i] >> 8) & 0xFF);
	        array.set(4*i+2, (pixels[i] >> 16) & 0xFF);
	        array.set(4*i+3, (pixels[i] >> 24) & 0xFF);
	    }
	    context.putImageData(data, 0, 0);

	    return canvas;

	}
 
Example 2
Source File: ImageCanvasTest.java    From TGAReader with MIT License 6 votes vote down vote up
private Canvas createImageCanvas(int [] pixels, int width, int height) {
	
	Canvas canvas = Canvas.createIfSupported();
	canvas.setCoordinateSpaceWidth(width);
	canvas.setCoordinateSpaceHeight(height);
	
	Context2d context = canvas.getContext2d();
	ImageData data = context.createImageData(width, height);

	CanvasPixelArray array = data.getData();
	for(int i=0; i<width*height; i++) { // ABGR
		array.set(4*i+0, pixels[i] & 0xFF);
		array.set(4*i+1, (pixels[i] >> 8) & 0xFF);
		array.set(4*i+2, (pixels[i] >> 16) & 0xFF);
		array.set(4*i+3, (pixels[i] >> 24) & 0xFF);
	}
	context.putImageData(data, 0, 0);
	
	canvas.getElement().getStyle().setMargin(4, Unit.PX);
	
	return canvas;
	
}
 
Example 3
Source File: DataStore.java    From djvu-html5 with GNU General Public License v2.0 5 votes vote down vote up
public static CanvasElement createImage(int width, int height) {
	Canvas canvas = Canvas.createIfSupported();
	canvas.setWidth(width + "px");
	canvas.setCoordinateSpaceWidth(width);
	canvas.setHeight(height + "px");
	canvas.setCoordinateSpaceHeight(height);
	return canvas.getCanvasElement();
}
 
Example 4
Source File: Djvu_html5.java    From djvu-html5 with GNU General Public License v2.0 5 votes vote down vote up
private Widget prepareCanvas() {
	canvas = Canvas.createIfSupported();
	if (canvas == null) {
		// TODO
		throw new RuntimeException("Canvas not supported!");
	}
	canvas.setTabIndex(0);

	final SimplePanel panel = new SimplePanel(canvas);
	panel.setStyleName("content");

	Window.addResizeHandler(e -> resizeCanvas());
	Scheduler.get().scheduleFinally(() -> resizeCanvas());
	return panel;
}
 
Example 5
Source File: CirSim.java    From circuitjs1 with GNU General Public License v2.0 4 votes vote down vote up
public Canvas getCircuitAsCanvas(boolean print) {
    	// create canvas to draw circuit into
    	Canvas cv = Canvas.createIfSupported();
    	Rectangle bounds = getCircuitBounds();
    	
	// add some space on edges because bounds calculation is not perfect
    	int wmargin = 140;
    	int hmargin = 100;
    	int w = (bounds.width+wmargin) ;
    	int h = (bounds.height+hmargin) ;
    	cv.setCoordinateSpaceWidth(w);
    	cv.setCoordinateSpaceHeight(h);
    	double oldTransform[] = Arrays.copyOf(transform, 6);
    
	Context2d context = cv.getContext2d();
	Graphics g = new Graphics(context);
	context.setTransform(1, 0, 0, 1, 0, 0);
        
        double scale = 1;
        
	// turn on white background, turn off current display
	boolean p = printableCheckItem.getState();
	boolean c = dotsCheckItem.getState();
	if (print)
	    printableCheckItem.setState(true);
        if (printableCheckItem.getState()) {
            CircuitElm.whiteColor = Color.black;
            CircuitElm.lightGrayColor = Color.black;
            g.setColor(Color.white);
        } else {
            CircuitElm.whiteColor = Color.white;
            CircuitElm.lightGrayColor = Color.lightGray;
            g.setColor(Color.black);
            g.fillRect(0, 0, g.context.getCanvas().getWidth(), g.context.getCanvas().getHeight());
        }
	dotsCheckItem.setState(false);

        if (bounds != null)
            scale = Math.min(w /(double)(bounds.width+wmargin),
                             h/(double)(bounds.height+hmargin));
        scale = Math.min(scale, 1.5); // Limit scale so we don't create enormous circuits in big windows
        
        // ScopeElms need the transform array to be updated
	transform[0] = transform[3] = scale;
	transform[4] = -(bounds.x-wmargin/2);
	transform[5] = -(bounds.y-hmargin/2);
	context.scale(scale, scale);
	context.translate(transform[4], transform[5]);
	
	// draw elements
	int i;
	for (i = 0; i != elmList.size(); i++) {
	    getElm(i).draw(g);
	}
	
	// restore everything
	printableCheckItem.setState(p);
	dotsCheckItem.setState(c);
	transform = oldTransform;
	return cv;
}
 
Example 6
Source File: TexturedCube.java    From TGAReader with MIT License 4 votes vote down vote up
public void onModuleLoad() {
	
	Canvas canvas = Canvas.createIfSupported();
	canvas.setStyleName("MyCanvas");
	canvas.setCoordinateSpaceWidth(400);
	canvas.setCoordinateSpaceHeight(400);
	RootLayoutPanel.get().add(canvas);
	
	gl = (WebGLRenderingContext)canvas.getContext("experimental-webgl");
	gl.viewport(0, 0, 400, 400);
	
	WebGLBuffer vertexBuffer = gl.createBuffer();
	gl.bindBuffer(ARRAY_BUFFER, vertexBuffer);
	gl.bufferData(ARRAY_BUFFER, Float32Array.create(VERTICES), STATIC_DRAW);

	WebGLShader vertexShader = gl.createShader(VERTEX_SHADER);
	gl.shaderSource(vertexShader, VERTEX_SHADER_SOURCE);
	gl.compileShader(vertexShader);
	
	WebGLShader fragmentShader = gl.createShader(FRAGMENT_SHADER);
	gl.shaderSource(fragmentShader, FRAGMENT_SHADER_SOURCE);
	gl.compileShader(fragmentShader);
	
	program = gl.createProgram();
	gl.attachShader(program, vertexShader);
	gl.attachShader(program, fragmentShader);
	gl.linkProgram(program);
	
	gl.useProgram(program);
	gl.bindBuffer(ARRAY_BUFFER, vertexBuffer);
	
	WebGLUniformLocation texture = gl.getUniformLocation(program, "texture");
	gl.uniform1i(texture, 0);
	
	int posAttr = gl.getAttribLocation(program, "position");
	gl.vertexAttribPointer(posAttr, 3, FLOAT, false, 5*4, 0);
	gl.enableVertexAttribArray(posAttr);
	
	int texAttr = gl.getAttribLocation(program, "texcoord");
	gl.vertexAttribPointer(texAttr, 2, FLOAT, false, 5*4, 3*4);
	gl.enableVertexAttribArray(texAttr);
	
	for(int i=0; i<TEXTURE_URLS.length; i++) {
		loadTexture(TEXTURE_URLS[i], i);
	}
	
}
 
Example 7
Source File: Util.java    From openchemlib-js with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
public static Canvas createScaledCanvas(int width, int height) {
  Canvas canvas = Canvas.createIfSupported();
  
  scaleCanvas(canvas, width, height);

  return canvas;
}