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:anagram.client.Logo.java

License:Apache License

void draw(Context2d context) {
    context.save();/*from w w w.  jav a2  s  . c  o  m*/
    context.translate(this.pos.x, this.pos.y);
    context.rotate(rot);
    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();//from w  ww. j  a va2s  .co  m
        context.translate(getX(), getY());
        context.rotate(getAngle());
        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();/*  w ww . j  av  a  2s.c o m*/
    /*context.setShadowOffsetY(1);
    context.setShadowOffsetX(4);
    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);/*from w  w  w . j  ava2  s .  co m*/
    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.lambourg.webgallery.client.widgets.TitleBarIcon.java

License:GNU General Public License

private void draw() {
    Context2d ctx;
    Canvas buffer = Canvas.createIfSupported();
    Image img;/*from  w  w w .  j a v a 2s . com*/
    int size = Style.toPixelRatio(Style.titleIconSize);

    this.canvas.setCoordinateSpaceWidth(size);
    this.canvas.setCoordinateSpaceHeight(size + Style.toPixelRatio(1));
    buffer.setCoordinateSpaceWidth(size);
    buffer.setCoordinateSpaceHeight(size);

    if (this.active) {
        img = this.imgActive;
    } else {
        img = this.imgNormal;
    }

    //  We need to draw the icon itself in a separate buffer, and only
    //  after apply the shadow.
    //  The reason is that in order to draw the icon using the color we
    //  want, we need to use composite operations, and this is not
    //  compatible with the shadow effect we want.
    ctx = buffer.getContext2d();
    if (this.over) {
        ctx.setFillStyle("#fff");
    } else {
        ctx.setFillStyle("#ddd");
    }
    ctx.fillRect(0, 0, size, size);
    ctx.setGlobalCompositeOperation(Composite.DESTINATION_ATOP);
    ctx.drawImage(ImageElement.as(img.getElement()), 0, 0, size, size);
    ctx.restore();

    ctx = this.canvas.getContext2d();
    ctx.setShadowBlur(0.0);
    ctx.setShadowColor("#333");
    ctx.setShadowOffsetY(Style.toPixelRatio(1));

    ctx.drawImage(buffer.getCanvasElement(), 0, 0, size, size);
}

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

License:Apache License

@Override
public void draw(double time, Context2d context) {
    if (!visible) {
        return;/* w  ww  . j a v a2  s. com*/
    }
    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/* ww w.j  av a 2s. co 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 a  va2 s.c o  m*/
            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();/*from ww w. j a v  a2s  .c o m*/
    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 source file:org.catrobat.html5player.client.Scene.java

License:Open Source License

/**
 *
 * @param imageElement/*from   ww w. jav  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");
}