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

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

Introduction

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

Prototype

public final native void save() ;

Source Link

Document

Saves the context's state.

Usage

From source file:anagram.client.Logo.java

License:Apache License

void draw(Context2d context) {
    context.save();
    context.translate(this.pos.x, this.pos.y);
    context.rotate(rot);/*from  ww w.  ja  va2  s  .  c  o  m*/
    context.drawImage(image, 0, 0);
    context.restore();
}

From source file:com.google.gwt.maeglin89273.game.ashinyballonthecross.client.tutorial.component.BlueMark.java

@Override
public void draw(Context2d context) {
    if (enabled) {
        context.save();
        context.translate(getX(), getY());
        context.rotate(getAngle());/*from  w  w w .j  a  v a  2s.com*/
        context.drawImage(spriteBlock.getSheetImage(), spriteBlock.getX(), spriteBlock.getY(),
                spriteBlock.getWidth(), spriteBlock.getHeight(), -getWidth() / 2, -getHeight() / 2, getWidth(),
                getHeight());
        context.restore();
    }
}

From source file:com.google.gwt.maeglin89273.shared.test.volcanogame.component.Volcano.java

@Override
public void draw(Context2d context) {
    Point p = getPositionAt(PositionType.NORTHWEST);

    context.save();
    /*context.setShadowOffsetY(1);
    context.setShadowOffsetX(4);/*from  w ww  .jav a2  s  . c o m*/
    context.setShadowBlur(25);
    context.setShadowColor("hsla(22.5,50%,5%,0.3)");
    context.setShadowColor("rgba(0,0,0,0.2)");*/
    context.drawImage(image.getImage(), p.getX(), p.getY() - 17, width, height + 17);
    context.restore();

}

From source file:com.google.gwt.maeglin89273.shared.test.volcanogame.component.VolcanoWorld.java

@Override
public void draw(Context2d context) {
    context.drawImage(background.getImage(), 0, 0);
    volcano.draw(context);//  w w w  .  j a  v a2s .c om
    context.save();
    //context.setShadowBlur(12);
    for (Physical fireball : fireballs) {
        fireball.draw(context);
    }

    /*context.setShadowBlur(3);
    context.setShadowOffsetX(4);
    context.setShadowOffsetY(4);
    context.setShadowColor("rgba(65,65,65,0.3)");*/
    context.drawImage(clouds.getImage(), 20, 30);
    context.restore();
}

From source file:com.philbeaudoin.quebec.client.scene.SceneNodeImpl.java

License:Apache License

@Override
public void draw(double time, Context2d context) {
    if (!visible) {
        return;/*from w ww . ja  va  2s.  co m*/
    }
    context.save();
    try {
        getTransform().applies(time, context);
        drawUntransformed(time, context);
        if (isAnimationCompleted(time)) {
            animationCompletedCallbacks.executeAll();
        }
    } finally {
        context.restore();
    }
}

From source file:com.sencha.gxt.chart.client.draw.engine.Canvas2d.java

License:sencha.com license

/**
 * In the Canvas2d class, this method does more or less what renderSprite does in SVG and VML - it
 * actually renders the sprite to the dom.
 * @param sprite the sprite to draw/*from   w ww. ja  v a  2 s.  c o m*/
 */
protected void append(Sprite sprite) {
    if (sprite.isHidden() || sprite.getOpacity() == 0) {
        return;
    }
    Context2d ctx = getContext();
    ctx.save();
    //set global stuff, fill, stroke, clip, etc

    //clip - deal with translation or normal rectangle
    if (sprite.getClipRectangle() != null) {
        PreciseRectangle clip = sprite.getClipRectangle();
        if (sprite.getScaling() != null || sprite.getTranslation() != null || sprite.getRotation() != null) {
            PathSprite transPath = new PathSprite(new RectangleSprite(clip));
            transPath = transPath.map(sprite.transformMatrix());
            appendPath(ctx, transPath);
        } else {
            ctx.beginPath();
            ctx.rect(clip.getX(), clip.getY(), clip.getWidth(), clip.getHeight());
            ctx.closePath();
        }
        ctx.clip();
    }

    if (sprite.getScaling() != null || sprite.getTranslation() != null || sprite.getRotation() != null
            || (component.isViewBox() && viewbox != null)) {
        Matrix matrix = sprite.transformMatrix();
        if (matrix != null) {
            //TODO consider replacing this transform call with three distinct calls to translate/scale/rotate if cheaper
            ctx.transform(matrix.get(0, 0), matrix.get(1, 0), matrix.get(0, 1), matrix.get(1, 1),
                    matrix.get(0, 2), matrix.get(1, 2));
        }
        if (component.isViewBox() && viewbox != null) {
            double size = Math.min(getWidth() / viewbox.getWidth(), getHeight() / viewbox.getHeight());

            ctx.scale(size, size);
            ctx.translate(-viewbox.getX(), -viewbox.getY());
        }
    }

    //TODO see about caching colors via the dirty flag? If we don't use a color/gradient for a pass or three, dump it
    double opacity = Double.isNaN(sprite.getOpacity()) ? 1.0 : sprite.getOpacity();
    PreciseRectangle untransformedBbox = sprite.getPathSprite().dimensions();
    if (sprite.getStroke() != null && sprite.getStroke() != Color.NONE && sprite.getStrokeWidth() != 0) {
        ctx.setLineWidth(Double.isNaN(sprite.getStrokeWidth()) ? 1.0 : sprite.getStrokeWidth());
        ctx.setStrokeStyle(getColor(sprite.getStroke(), untransformedBbox));//TODO read bbox from cache
    }
    if (sprite.getFill() != null && sprite.getFill() != Color.NONE) {
        ctx.setFillStyle(getColor(sprite.getFill(), untransformedBbox));//TODO read bbox from cache
    }

    if (sprite instanceof PathSprite) {
        appendPath(ctx, (PathSprite) sprite);
    } else if (sprite instanceof TextSprite) {
        TextSprite text = (TextSprite) sprite;
        //TODO style and weight
        ctx.setFont(text.getFontSize() + "px " + text.getFont());
        ctx.setTextAlign(getTextAlign(text.getTextAnchor()));
        ctx.setTextBaseline(getTextBaseline(text.getTextBaseline()));
        ctx.fillText(text.getText(), text.getX(), text.getY());
    } else if (sprite instanceof RectangleSprite) {
        RectangleSprite rect = (RectangleSprite) sprite;
        if (Double.isNaN(rect.getRadius()) || rect.getRadius() == 0) {
            if (sprite.getFill() != null && sprite.getFill() != Color.NONE) {
                ctx.setGlobalAlpha(
                        Double.isNaN(sprite.getFillOpacity()) ? opacity : opacity * sprite.getFillOpacity());
                ctx.fillRect(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
            }
            if (sprite.getStroke() != null && sprite.getStroke() != Color.NONE
                    && sprite.getStrokeWidth() != 0) {
                ctx.setGlobalAlpha(Double.isNaN(sprite.getStrokeOpacity()) ? opacity
                        : opacity * sprite.getStrokeOpacity());
                ctx.strokeRect(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight());
            }
        } else {
            appendPath(ctx, rect.getPathSprite());
        }
    } else if (sprite instanceof CircleSprite) {
        CircleSprite circle = (CircleSprite) sprite;
        ctx.beginPath();
        ctx.arc(circle.getCenterX(), circle.getCenterY(), circle.getRadius(), 0, 2 * Math.PI);
        ctx.closePath();
        if (sprite.getFill() != null && sprite.getFill() != Color.NONE) {
            ctx.setGlobalAlpha(
                    Double.isNaN(sprite.getFillOpacity()) ? opacity : opacity * sprite.getFillOpacity());
            ctx.fill();
        }
        if (sprite.getStroke() != null && sprite.getStroke() != Color.NONE && sprite.getStrokeWidth() != 0) {
            ctx.setGlobalAlpha(
                    Double.isNaN(sprite.getStrokeOpacity()) ? opacity : opacity * sprite.getStrokeOpacity());
            ctx.stroke();
        }
    } else if (sprite instanceof EllipseSprite) {
        appendPath(ctx, sprite.getPathSprite());
    } else if (sprite instanceof ImageSprite) {
        ImageSprite image = (ImageSprite) sprite;
        ImageElement elt = Document.get().createImageElement();
        elt.setSrc(image.getResource().getSafeUri().asString());
        ctx.drawImage(elt, image.getX(), image.getY(), image.getWidth(), image.getHeight());
    }

    ctx.restore();

    if (!REDRAW_ALL) {
        renderedBbox.put(sprite, getBBox(sprite));
    }

    sprite.clearDirtyFlags();
}

From source file:examples.geometry.demos.RegionClippingExample.java

License:Open Source License

@Override
protected ControllableShape[] createShapes(Canvas canvas, EventBus eventBus) {
    return new ControllableShape[] { new ControllableShape(canvas, eventBus) {
        {//from  ww w .j av  a 2 s  .com
            addControlPoints(new Point(100, 100), new Point(200, 200));
            addControlPoints(new Point(150, 150), new Point(250, 250));
        }

        @Override
        public Region getShape() {
            Point[] cp = getPoints();
            Region region = new Region(new Rectangle(cp[0], cp[1]), new Rectangle(cp[2], cp[3]));
            return region;
        }

        @Override
        public void onDraw(Canvas canvas) {
            Context2d context = canvas.getContext2d();
            Region region = getShape();

            context.save();
            context.beginPath();
            Rectangle rr = region.getBounds();
            context.rect(rr.getX(), rr.getY(), rr.getWidth(), rr.getHeight());
            //            context.fill();
            context.clip();

            for (int y = 0; y < 800; y += 20) {
                context.fillText(
                        "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz",
                        20, y);
            }

            context.restore();

            context.setFillStyle("blue");
            context.setGlobalAlpha(0.5);
            context.beginPath();
            for (Rectangle r : region.getShapes()) {
                context.fillRect(r.getX(), r.getY(), r.getWidth(), r.getHeight());
            }
            context.closePath();
            context.setFillStyle("black");
            context.setGlobalAlpha(1);
        }
    } };
}

From source file:gwt.g2d.client.graphics.visitor.EllipseVisitor.java

License:Apache License

@Override
public void visit(Surface surface) {
    Context2d context = surface.getContext();
    context.save();
    context.translate(x + width / 2, y + height / 2);
    context.scale(width / 2, height / 2);
    context.arc(0, 0, 1, 0, MathHelper.TWO_PI, true);
    context.restore();/*from  w w w . j  ava 2 s .co  m*/
}

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

License:Open Source License

/**
 *
 * @param imageElement/*from   w  ww .j av a2  s  .  c o m*/
 * @param x
 * @param y
 * @param width
 * @param height
 */
public void drawImageElement(ImageElement imageElement, double x, double y, double width, double height,
        double alpha) {

    long start = System.currentTimeMillis();

    Context2d context = sceneCanvas.getContext2d();
    context.save();
    context.setGlobalAlpha(alpha);
    context.drawImage(imageElement, 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//from  w w  w .j a v a 2 s . co  m
 * @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");
}