List of usage examples for com.google.gwt.canvas.dom.client Context2d setGlobalAlpha
public final native void setGlobalAlpha(double alpha) ;
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 ww w . ja v a2 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:com.sencha.gxt.chart.client.draw.engine.Canvas2d.java
License:sencha.com license
protected void appendPath(Context2d ctx, PathSprite sprite) { ctx.beginPath();//from w w w . ja v a 2 s . c o m sprite.toAbsolute(); // sprite = sprite.copy().toCurve(); PrecisePoint currentPoint = new PrecisePoint(); PrecisePoint movePoint = new PrecisePoint(); PrecisePoint curvePoint = new PrecisePoint(); PrecisePoint quadraticPoint = new PrecisePoint(); appendPathCommands(ctx, sprite.getCommands(), currentPoint, movePoint, curvePoint, quadraticPoint); double opacity = Double.isNaN(sprite.getOpacity()) ? 1.0 : sprite.getOpacity(); 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.setLineCap(sprite.getStrokeLineCap() == null ? LineCap.BUTT : sprite.getStrokeLineCap()); ctx.setLineJoin(sprite.getStrokeLineJoin() == null ? LineJoin.MITER : sprite.getStrokeLineJoin()); ctx.setMiterLimit(sprite.getMiterLimit() == Double.NaN ? 4 : sprite.getMiterLimit()); ctx.setGlobalAlpha( Double.isNaN(sprite.getStrokeOpacity()) ? opacity : opacity * sprite.getStrokeOpacity()); ctx.stroke(); } }
From source file:de.ga_ap.cirrus.newcloud.client.TheCloud.java
License:Open Source License
public TheCloud(final Element cloudContainer) { final NodeList<Element> aElements = cloudContainer.getElementsByTagName("A"); final int height = cloudContainer.getOffsetHeight(); final int width = cloudContainer.getOffsetWidth(); final Canvas canvas = Canvas.createIfSupported(); canvas.setCoordinateSpaceHeight(height); canvas.setCoordinateSpaceWidth(width); final Context2d context2d = canvas.getContext2d(); final double radius = canvas.getCanvasElement().getHeight() / 3; final int refreshrate = 30; final List<CloudItem> itemList = new ArrayList<CloudItem>(aElements.getLength()); for (int i = 0; i < aElements.getLength(); i++) { final Element el = aElements.getItem(i); final CloudItem ci = new CloudItem(); ci.text = el.getInnerText();/* ww w .j av a 2 s . co m*/ itemList.add(ci); } cloudContainer.setInnerText(""); final int itemListSize = itemList.size(); for (int i = 1; i <= itemListSize; i++) { final CloudItem ci = itemList.get(i - 1); ci.theta = Math.acos(-1.0 + (2.0 * (double) i - 1.0) / itemListSize); ci.phi = Math.sqrt(itemListSize * Math.PI) * ci.theta; ci.z = radius * Math.cos(ci.theta); ci.y = radius * Math.sin(ci.theta) * Math.cos(ci.phi); ci.x = radius * Math.sin(ci.theta) * Math.sin(ci.phi); // TODO heightOfAllItems ?! // TODO font size context2d.fillText(ci.text, ci.y, ci.z); System.out.println(ci.x + " " + ci.y + " " + ci.z + " - " + ci.theta + " " + ci.phi); } Scheduler.get().scheduleFixedPeriod(new RepeatingCommand() { @Override public boolean execute() { canvas.setCoordinateSpaceHeight(height); canvas.setCoordinateSpaceWidth(width); final double cosPsi = Math.cos(psi); final double sinPsi = Math.sin(psi); final double cosTheta = Math.cos(theta); final double sinTheta = Math.sin(theta); final double sinThetaCosPsi = sinTheta * cosPsi; final double sinThetaSinPsi = sinTheta * sinPsi; for (final CloudItem ci : itemList) { final double x, y, z; x = ci.x * cosTheta * cosPsi + ci.y * cosTheta * sinPsi - ci.z * sinTheta; y = ci.x * (-sinPsi) + ci.y * cosPsi; z = ci.x * sinThetaCosPsi + ci.y * sinThetaSinPsi + ci.z * cosTheta; ci.x = x; ci.y = y; ci.z = z; context2d.setGlobalAlpha(0.7 + ci.x / radius / 3.0); context2d.setFont("20pt Arial"); context2d.fillText(ci.text, radius + ci.y, radius + ci.z); // System.out.println(ci.x + " " + ci.y + " " + ci.z + " - " // + ci.theta + " " + ci.phi + " " + ci.text); } theta += ySteps; psi += xSteps; // System.out.println(theta); if (theta > Math.PI * 2.0) { theta = 0.0; } if (psi > Math.PI * 2.0) { psi = 0.0; } return true; } }, refreshrate); // final EventListener listener = new EventListener() { // // @Override // public void onBrowserEvent(final Event event) { // if (event.getTypeInt() == Event.ONMOUSEMOVE) { // moveSteps(event); // } else if (event.getTypeInt() == Event.ONTOUCHMOVE) { // event.preventDefault(); // prevents the default behavior of // // a page when touching // moveSteps(event); // } // } // // private void moveSteps(final Event event) { // ySteps = -((event.getClientY() + Window.getScrollTop()) // / canvas.getCoordinateSpaceHeight() * 0.000002 - 0.1) / 2.0; // xSteps = ((event.getClientX() + Window.getScrollLeft()) // / canvas.getCoordinateSpaceWidth() * 0.000002 - 0.1) / 2.0; // } // }; // // DOM.setEventListener( // (com.google.gwt.user.client.Element) cloudContainer, listener); // DOM.sinkEvents((com.google.gwt.user.client.Element) cloudContainer, // Event.ONMOUSEMOVE + Event.ONTOUCHMOVE); System.out.println(radius); cloudContainer.appendChild(canvas.getCanvasElement()); }
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 a2 s.c om 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:examples.geometry.demos.RegionOutlineExample.java
License:Open Source License
@Override protected ControllableShape[] createShapes(Canvas canvas, EventBus eventBus) { return new ControllableShape[] { new ControllableShape(canvas, eventBus) { {//from w w w .j a v a 2 s . com addControlPoints(new Point(100, 50), new Point(300, 100)); addControlPoints(new Point(250, 200), new Point(350, 330)); addControlPoints(new Point(100, 200), new Point(190, 325)); addControlPoints(new Point(150, 300), new Point(280, 380)); } @Override public Region getShape() { Point[] cp = getPoints(); Rectangle[] rectangles = new Rectangle[cp.length / 2]; for (int i = 0; i < rectangles.length; i++) { rectangles[i] = new Rectangle(cp[2 * i], cp[2 * i + 1]); } return new Region(rectangles); } @Override public void onDraw(Canvas canvas) { Context2d context = canvas.getContext2d(); Region region = getShape(); context.setFillStyle("rgba(0, 0, 255, 0.5)"); context.setGlobalAlpha(0.5); context.beginPath(); for (Rectangle r : region.getShapes()) { context.fillRect(r.getX(), r.getY(), r.getWidth(), r.getHeight()); } context.closePath(); // gc.setAlpha(255); context.setFillStyle("rgba(255, 255, 255, 1)"); context.setGlobalAlpha(1); // gc.setForeground(Display.getCurrent().getSystemColor( // SWT.COLOR_RED)); // for (Rectangle r : region.getShapes()) { // gc.drawRectangle(r.toSWTRectangle()); // } // gc.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_BLACK)); context.beginPath(); for (Line l : region.getOutlineSegments()) { Point p1 = l.getP1(); Point p2 = l.getP2(); context.moveTo(p1.x, p1.y); context.lineTo(p2.x, p2.y); context.stroke(); } context.closePath(); } } }; }
From source file:org.catrobat.html5player.client.Scene.java
License:Open Source License
/** * * @param imageElement/* w w w . j a v a 2 s. com*/ * @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/* w w w .ja 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 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//from ww w . j a va 2 s . com * @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:playn.html.HtmlCanvasLayerCanvas.java
License:Apache License
@Override void paint(Context2d ctx, float parentAlpha) { ctx.save();/*from w w w.j a va 2s . c om*/ transform(ctx); ctx.setGlobalAlpha(parentAlpha * alpha); ctx.drawImage(canvas.canvas(), 0, 0); ctx.restore(); }
From source file:playn.html.HtmlImageLayerCanvas.java
License:Apache License
@Override public void paint(Context2d ctx, float parentAlpha) { if (!visible() || !img.isReady()) return;//from w w w . j a va 2 s . co m ctx.save(); transform(ctx); ctx.setGlobalAlpha(parentAlpha * alpha); float width = width(); float height = height(); if (repeatX || repeatY) { updatePattern(ctx); ctx.setFillStyle(pattern); ctx.beginPath(); ctx.rect(0, 0, width, height); ctx.scale(repeatX ? 1 : width / img.width(), repeatY ? 1 : height / img.height()); ctx.fill(); } else { ((HtmlCanvas.Drawable) img).draw(ctx, 0, 0, width, height); } ctx.restore(); }