Example usage for com.google.gwt.canvas.dom.client Context2d restore

List of usage examples for com.google.gwt.canvas.dom.client Context2d restore

Introduction

In this page you can find the example usage for com.google.gwt.canvas.dom.client Context2d restore.

Prototype

public final native void restore() ;

Source Link

Document

Restores the context's state.

Usage

From source file:org.catrobat.html5player.client.Scene.java

License:Open Source License

/**
 *
 * @param imageElement/*from ww  w .j  av  a2 s  . c om*/
 * @param translateX
 * @param translateY
 * @param x
 * @param y
 * @param width
 * @param height
 * @param degrees
 * @param xSize
 * @param ySize
 */
public void drawImageElement(ImageElement imageElement, double translateX, double translateY, double x,
        double y, double width, double height, double degrees, double xSize, double ySize, double alpha) {

    long start = System.currentTimeMillis();

    Context2d context = sceneCanvas.getContext2d();

    context.save();
    context.setGlobalAlpha(alpha);
    context.translate(translateX, translateY);
    context.rotate(Math.toRadians(degrees));
    context.scale(xSize, ySize);
    context.drawImage(imageElement, x, y, width, height);

    //for testing - draws a rectangular around the sprite
    //      context.strokeRect(x, y, width, height);
    //

    context.restore();

    CatrobatDebug.debug("drawImageElement-execution took " + (System.currentTimeMillis() - start) + " ms");
}

From source file:org.catrobat.html5player.client.Scene.java

License:Open Source License

/**
 *
 * @param imageElement// w  ww .j  a  v  a  2s  . c o m
 * @param translateX
 * @param translateY
 * @param x
 * @param y
 * @param width
 * @param height
 * @param degrees
 * @param xSize
 * @param ySize
 */
public void drawImageElementBrightness(ImageElement imageElement, double translateX, double translateY,
        double x, double y, double width, double height, double degrees, double alpha, double brightness)
        throws JavaScriptException {
    Context2d context = sceneCanvas.getContext2d();
    context.save();
    context.setGlobalAlpha(alpha);
    context.translate(translateX, translateY);
    context.rotate(degrees * Math.PI / 180);

    try {
        Canvas adjustedImage = adjustImageBrightness(imageElement, brightness);
        context.drawImage(adjustedImage.getCanvasElement(), x, y, width, height);
        context.restore();
    } catch (JavaScriptException exception) {
        context.restore();
        throw exception;
    }
}

From source file:org.catrobat.html5player.client.Scene.java

License:Open Source License

/**
 *
 *///from  www  .  j a  v  a  2 s  .com
public void clearCanvas() {

    long start = System.currentTimeMillis();

    Context2d context = sceneCanvas.getContext2d();
    context.save();
    context.setFillStyle(fillColor);
    context.fillRect(0, 0, this.sceneWidth, this.sceneHeight);
    context.restore();

    CatrobatDebug.debug("clearCanvas-execution took " + (System.currentTimeMillis() - start) + " ms");
}

From source file:org.geomajas.gwt2.example.client.sample.rendering.CanvasImageElement.java

License:Open Source License

@Override
public void paint(Canvas canvas, Matrix matrix) {
    /*//from   ww  w  . j a  v a  2s .c o m
    * paint image on canvas based on the original and with the transformations of the matrix.
    * */
    Context2d context2d = canvas.getContext2d();
    context2d.save();
    boolean xReversal = matrix.getXx() < 0;
    boolean yReversal = matrix.getYy() < 0;
    context2d.scale(xReversal ? -1 : 1, yReversal ? -1 : 1);
    double xValue = xReversal ? box.getMaxX() * -1 : box.getX();
    double yValue = yReversal ? box.getMaxY() * -1 : box.getY();
    context2d.drawImage(hiddenImageCanvas.getCanvasElement(), xValue, yValue, box.getWidth(), box.getHeight());
    context2d.restore();
}

From source file:org.grailrtls.plunder.client.drawable.PassiveTiles.java

License:Open Source License

/**
 * Renders this objects tiles (if any) to the DrawableContext.
 * Since there are no images, uses drawing primitives to render
 * 2-d colored boxes.//from w  w w  .j  a  va 2 s  .c  om
 */
@Override
public void draw(Context2d context) {
    // FIXME: Proper solution for "out of date" data
    if (System.currentTimeMillis() - this.timestamp > 5000) {
        return;
    }

    context.save();
    context.setFillStyle("rgba(40,40,230,0.5)");

    for (Tile t : this.tiles) {
        float x1 = this.toCanvasX(t.x1);
        float x2 = this.toCanvasX(t.x2);
        float y1 = this.toCanvasY(t.y1);
        float y2 = this.toCanvasY(t.y2);

        context.fillRect(x1, y1, x2 - x1, y2 - y1);

    }

    context.restore();

}

From source file:org.peergreen.vaadin.diagram.client.ui.IntermediateConnectorUI.java

License:Apache License

@Override
public void draw() {
    // Draw a line from the source port to the current mouse coordinates
    Context2d canvas = getCanvas();
    IPoint coordinates = getModel().getMouseCoordinates();

    canvas.save();/*from   ww w .  j  a v a  2 s  .  c  o m*/
    canvas.beginPath();
    canvas.setStrokeStyle("#000");
    canvas.moveTo(sourcePort.getConnectorX(), sourcePort.getConnectorY());
    canvas.lineTo(coordinates.getX(), coordinates.getY());
    canvas.closePath();
    canvas.stroke();
    canvas.restore();
}

From source file:org.primordion.xholon.io.GridPanel.java

License:Open Source License

/**
 * Draw a polygon./*from   ww w  .  j a v a  2s  .c o m*/
 */
protected void drawPolygon(Context2d ctx, int x, int y, int xCoor[], int yCoor[], int sides) {
    ctx.save();
    ctx.translate(x, y);
    ctx.beginPath();
    ctx.moveTo(xCoor[0], yCoor[0]);
    for (int i = 1; i < sides; i++) {
        ctx.lineTo(xCoor[i], yCoor[i]);
    }
    ctx.lineTo(xCoor[0], yCoor[0]);
    ctx.fill();
    ctx.closePath();
    ctx.restore();
}

From source file:org.primordion.xholon.io.GridPanel.java

License:Open Source License

/**
 * Draw a regular polygon./*from  ww w. j a  v  a  2  s  .  c o m*/
 * Example:
<code>
context.beginPath();
polygon(context,350,125,100,6,-Math.PI/2);
context.fillStyle="rgba(51,128,255,0.75)";
context.fill();
context.stroke();
</code>
 * @param ctx A GWT Context2d object.
 * @param x X coordinate of the grid cell.
 * @param y Y coordinate of the grid cell.
 * @param radius 
 * @param sides The number of sides in the polygon. This value must be >= 3.
 * @param startAngle Angle in radians.
 * @param anticlockwise Draw clockwise (false) or anticlockwise (true).
 * @see http://www.storminthecastle.com/2013/07/24/how-you-can-draw-regular-polygons-with-the-html5-canvas-api/
 */
protected void drawPolygon(Context2d ctx, int x, int y, double radius, int sides, double startAngle,
        boolean anticlockwise) {
    if (sides < 3)
        return;
    double a = (Math.PI * 2) / sides;
    a = anticlockwise ? -a : a;
    ctx.save();
    ctx.translate(x, y);
    ctx.rotate(startAngle);
    ctx.moveTo(radius, 0);
    for (int i = 1; i < sides; i++) {
        //double xcoor = radius * Math.cos(a * i);
        //double ycoor = radius * Math.sin(a * i);
        //System.out.println("i:" + i + " xcoor:" + xcoor + " ycoor:" + ycoor);
        //ctx.lineTo(xcoor, ycoor);
        ctx.lineTo(radius * Math.cos(a * i), radius * Math.sin(a * i));
    }
    ctx.closePath();
    ctx.restore();
}

From source file:org.teavm.samples.benchmark.gwt.BenchmarkStarter.java

License:Apache License

private void render() {
    Context2d context = canvas.getContext2d();
    context.setFillStyle("white");
    context.setStrokeStyle("grey");
    context.fillRect(0, 0, 600, 600);//from   w ww  .  ja v a  2s  .c  o  m
    context.save();
    context.translate(0, 600);
    context.scale(1, -1);
    context.scale(100, 100);
    context.setLineWidth(0.01);
    for (Body body = scene.getWorld().getBodyList(); body != null; body = body.getNext()) {
        Vec2 center = body.getPosition();
        context.save();
        context.translate(center.x, center.y);
        context.rotate(body.getAngle());
        for (Fixture fixture = body.getFixtureList(); fixture != null; fixture = fixture.getNext()) {
            Shape shape = fixture.getShape();
            if (shape.getType() == ShapeType.CIRCLE) {
                CircleShape circle = (CircleShape) shape;
                context.beginPath();
                context.arc(circle.m_p.x, circle.m_p.y, circle.getRadius(), 0, Math.PI * 2, true);
                context.closePath();
                context.stroke();
            } else if (shape.getType() == ShapeType.POLYGON) {
                PolygonShape poly = (PolygonShape) shape;
                Vec2[] vertices = poly.getVertices();
                context.beginPath();
                context.moveTo(vertices[0].x, vertices[0].y);
                for (int i = 1; i < poly.getVertexCount(); ++i) {
                    context.lineTo(vertices[i].x, vertices[i].y);
                }
                context.closePath();
                context.stroke();
            }
        }
        context.restore();
    }
    context.restore();
}

From source file:playn.html.HtmlCanvasLayerCanvas.java

License:Apache License

@Override
void paint(Context2d ctx, float parentAlpha) {
    ctx.save();// w  w  w . j  a  v a  2s  .com
    transform(ctx);

    ctx.setGlobalAlpha(parentAlpha * alpha);
    ctx.drawImage(canvas.canvas(), 0, 0);

    ctx.restore();
}