Example usage for com.google.gwt.canvas.client Canvas toDataUrl

List of usage examples for com.google.gwt.canvas.client Canvas toDataUrl

Introduction

In this page you can find the example usage for com.google.gwt.canvas.client Canvas toDataUrl.

Prototype

public String toDataUrl() 

Source Link

Document

Returns a data URL for the current content of the canvas element.

Usage

From source file:com.ait.toolkit.clientio.canvg.client.CanVgParser.java

License:Open Source License

public static ByteArray getCanvas(Canvas element) {
    assert element != null : "Element cant be null";
    String data = element.toDataUrl();

    return Base64Util.get().decode(data);
}

From source file:com.vaadin.addon.spreadsheet.client.SheetWidget.java

private void createCellRangeRule() {
    DivElement tempDiv = Document.get().createDivElement();
    tempDiv.addClassName("cell-range-bg-color");
    tempDiv.getStyle().setWidth(0, Unit.PX);
    tempDiv.getStyle().setHeight(0, Unit.PX);
    sheet.appendChild(tempDiv);/*from   w  w w .  j  a  v a2 s.co m*/
    ComputedStyle cs = new ComputedStyle(tempDiv);
    String bgCol = cs.getProperty("backgroundColor");
    bgCol = bgCol.replace("!important", "");
    sheet.removeChild(tempDiv);

    if (bgCol != null && !bgCol.trim().isEmpty()) {
        Canvas c = Canvas.createIfSupported();
        c.setCoordinateSpaceHeight(1);
        c.setCoordinateSpaceWidth(1);
        c.getContext2d().setFillStyle(bgCol);
        c.getContext2d().fillRect(0, 0, 1, 1);
        String bgImage = "url(\"" + c.toDataUrl() + "\")";

        jsniUtil.insertRule(sheetStyle, "." + sheetId + " .sheet .cell.cell-range {" + "background-image: "
                + bgImage + " !important;" + "}");
    } else {
        // Fall back to the default color
        jsniUtil.insertRule(sheetStyle, "." + sheetId + " .sheet .cell.cell-range {"
                + "background-color: rgba(232, 242, 252, 0.8) !important;" + "}");
    }
}

From source file:org.cleanlogic.cesiumjs4gwt.showcase.examples.LoadImages.java

License:Apache License

@Override
public void buildPanel() {
    csVPanel = new ViewerPanel();

    AbsolutePanel aPanel = new AbsolutePanel();
    aPanel.add(csVPanel);/*ww w  .j  a v a  2 s.c o  m*/

    org.cesiumjs.cs.core.PinBuilder pinBuilder = new org.cesiumjs.cs.core.PinBuilder();
    pinBuilder.fromUrlPromise(GWT.getModuleBaseURL() + "images/Cesium_Logo_Color_Overlay.png",
            Color.WHITE().withAlpha(0.0f), 256).then(new Fulfill<CanvasElement>() {
                @Override
                public void onFulfilled(CanvasElement value) {
                    BillboardGraphicsOptions billboardOptions = new BillboardGraphicsOptions();
                    billboardOptions.image = new ConstantProperty<>(value.toDataUrl());
                    EntityOptions entityOptions = new EntityOptions();
                    entityOptions.name = "Pin billboard through fromUrl";
                    entityOptions.billboard = new BillboardGraphics(billboardOptions);
                    entityOptions.position = new ConstantPositionProperty(Cartesian3.fromDegrees(35, 35));
                    csVPanel.getViewer().entities().add(new Entity(entityOptions));
                }
            });

    Resource.fetchImage((ResourceImageOptions) ResourceImageOptions
            .create(GWT.getModuleBaseURL() + "images/Cesium_Logo_Color_Overlay.png"))
            .then(new Fulfill<JsImage>() {
                @Override
                public void onFulfilled(JsImage value) {
                    Canvas canvas = Canvas.createIfSupported();
                    canvas.setWidth(value.width + "px");
                    canvas.setHeight(value.height + "px");
                    Context2d context = canvas.getContext2d();
                    context.scale(0.1, 0.1);
                    context.drawImage((ImageElement) (Object) value, 0, 0);

                    BillboardGraphicsOptions billboardOptions = new BillboardGraphicsOptions();
                    billboardOptions.image = new ConstantProperty<>(canvas.toDataUrl());
                    EntityOptions entityOptions = new EntityOptions();
                    entityOptions.name = "Pin billboard through canvas";
                    entityOptions.billboard = new BillboardGraphics(billboardOptions);
                    entityOptions.position = new ConstantPositionProperty(Cartesian3.fromDegrees(45, 45));
                    csVPanel.getViewer().entities().add(new Entity(entityOptions));
                }
            });

    // CORS not loaded
    Resource.fetchImage((ResourceImageOptions) ResourceImageOptions
            .create("https://www.linux.org.ru/tango/img/games-logo.png")).then(new Fulfill<JsImage>() {
                @Override
                public void onFulfilled(JsImage value) {
                    Canvas canvas = Canvas.createIfSupported();
                    canvas.setWidth(value.width + "px");
                    canvas.setHeight(value.height + "px");
                    Context2d context = canvas.getContext2d();
                    context.scale(0.1, 0.1);
                    context.drawImage((ImageElement) (Object) value, 0, 0);

                    BillboardGraphicsOptions billboardOptions = new BillboardGraphicsOptions();
                    billboardOptions.image = new ConstantProperty<>(canvas.toDataUrl());
                    EntityOptions entityOptions = new EntityOptions();
                    entityOptions.name = "Pin billboard CORS";
                    entityOptions.billboard = new BillboardGraphics(billboardOptions);
                    entityOptions.position = new ConstantPositionProperty(Cartesian3.fromDegrees(55, 55));
                    csVPanel.getViewer().entities().add(new Entity(entityOptions));
                }
            });

    // Cors not loaded!
    final JsImage imageAmz = new JsImage();
    imageAmz.crossOrigin = "*";
    imageAmz.onload = new JsImage.Listener() {
        @Override
        public void function() {
            Cesium.log(imageAmz);
            /*Canvas canvas = Canvas.createIfSupported();
            canvas.setWidth(imageAmz.width + "px");
            canvas.setHeight(imageAmz.height + "px");
            Context2d context = canvas.getContext2d();
            context.scale(0.1, 0.1);
            context.drawImage((ImageElement) (Object) imageAmz, 0, 0);*/
            BillboardGraphicsOptions billboardOptions = new BillboardGraphicsOptions();
            billboardOptions.image = new ConstantProperty<>(imageAmz);
            //billboardOptions.image = new ConstantProperty<>(canvas.toDataUrl("image/png"));
            EntityOptions entityOptions = new EntityOptions();
            entityOptions.name = "Pin billboard CORS";
            entityOptions.billboard = new BillboardGraphics(billboardOptions);
            entityOptions.position = new ConstantPositionProperty(Cartesian3.fromDegrees(65, 65));
            csVPanel.getViewer().entities().add(new Entity(entityOptions));
        }
    };
    imageAmz.src = "https://d1.awsstatic.com/products/cloudfront/cloudfront-100_PoP_600x400.4a1edd6022833c54c41370ad9f615ae818350a23.png";

    // Worked, have Access-Control-Allow-Origin: *
    final JsImage imageWiki = new JsImage();
    imageWiki.crossOrigin = "*";
    imageWiki.onload = new JsImage.Listener() {
        @Override
        public void function() {
            Cesium.log(imageWiki);
            Canvas canvas = Canvas.createIfSupported();
            canvas.setWidth(imageWiki.width + "px");
            canvas.setHeight(imageWiki.height + "px");
            Context2d context = canvas.getContext2d();
            context.drawImage((ImageElement) (Object) imageWiki, 0, 0);
            BillboardGraphicsOptions billboardOptions = new BillboardGraphicsOptions();
            billboardOptions.image = new ConstantProperty<>(canvas.toDataUrl("image/png"));
            EntityOptions entityOptions = new EntityOptions();
            entityOptions.name = "Pin billboard CORS";
            entityOptions.billboard = new BillboardGraphics(billboardOptions);
            entityOptions.position = new ConstantPositionProperty(Cartesian3.fromDegrees(75, 75));
            csVPanel.getViewer().entities().add(new Entity(entityOptions));
        }
    };
    imageWiki.src = "https://ru.wikipedia.org/static/images/project-logos/ruwiki-2x.png";

    contentPanel.add(new HTML("<p>Cluster labels, billboards and points.</p>"));
    contentPanel.add(aPanel);

    initWidget(contentPanel);
}

From source file:org.rstudio.core.client.widget.FontDetector.java

License:Open Source License

public static boolean isFontSupported(String fontName) {
    SimplePanel panel = null;//w  w w  .  j  a  va  2 s. c  o m
    try {
        // default font name as a reference point
        final String defaultFontName = "Arial";
        if (defaultFontName.equals(fontName))
            return true;

        // make sure canvas is supported
        if (!Canvas.isSupported())
            return false;

        // add a temporary div to the dom
        panel = new SimplePanel();
        panel.setHeight("200px");
        panel.getElement().getStyle().setVisibility(Visibility.HIDDEN);
        panel.getElement().getStyle().setOverflow(Overflow.SCROLL);
        RootPanel.get().add(panel, -2000, -2000);

        // add a canvas element to the div and get the 2d drawing context
        final Canvas canvas = Canvas.createIfSupported();
        canvas.setWidth("512px");
        canvas.setHeight("64px");
        canvas.getElement().getStyle().setLeft(400, Unit.PX);
        canvas.getElement().getStyle().setBackgroundColor("#ffe");
        panel.add(canvas);
        final Context2d ctx = canvas.getContext2d();
        ctx.setFillStyle("#000000");

        // closure to generate a hash for a font
        class HashGenerator {
            public String getHash(String fontName) {
                ctx.setFont("57px " + fontName + ", " + defaultFontName);
                int width = canvas.getOffsetWidth();
                int height = canvas.getOffsetHeight();
                ctx.clearRect(0, 0, width, height);
                ctx.fillText("TheQuickBrownFox", 2, 50);
                return canvas.toDataUrl();
            }
        }
        ;

        // get hashes and compare them
        HashGenerator hashGenerator = new HashGenerator();
        String defaultHash = hashGenerator.getHash(defaultFontName);
        String fontHash = hashGenerator.getHash(fontName);
        return !defaultHash.equals(fontHash);
    } catch (Exception ex) {
        Debug.log(ex.toString());
        return false;
    } finally {
        if (panel != null)
            RootPanel.get().remove(panel);
    }
}