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

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

Introduction

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

Prototype

public Context2d getContext2d() 

Source Link

Document

Returns a 2D rendering context.

Usage

From source file:com.badlogic.gdx.backends.gwt.GwtApplicationCustom.java

License:Apache License

public PreloaderCallback getPreloaderCallback() {
    final Canvas canvas = Canvas.createIfSupported();
    canvas.setWidth("100%");
    canvas.setHeight("100%");
    getRootPanel().add(canvas);//from  w w  w.j av a2 s . co m
    final Context2d context = canvas.getContext2d();
    context.setTextAlign(TextAlign.CENTER);
    context.setTextBaseline(TextBaseline.MIDDLE);
    context.setFont("18pt Calibri");

    return new PreloaderCallback() {
        @Override
        public void done() {
            context.fillRect(0, 0, 300, 40);
        }

        @Override
        public void loaded(String file, int loaded, int total) {
            System.out.println("loaded " + file + "," + loaded + "/" + total);
            //            String color = Pixmap.make(30, 30, 30, 1);
            //            context.setFillStyle(color);
            //            context.setStrokeStyle(color);
            context.fillRect(0, 0, 300, 70);
            //            color = Pixmap.make(200, 200, 200, (((TimeUtils.nanoTime() - loadStart) % 1000000000) / 1000000000f));
            //            context.setFillStyle(color);
            //            context.setStrokeStyle(color);
            context.fillRect(0, 0, 300 * (loaded / (float) total) * 0.97f, 70);

            //            context.setFillStyle(Pixmap.make(50, 50, 50, 1));
            context.fillText("loading", 300 / 2, 70 / 2);
        }

        @Override
        public void error(String file) {
            System.out.println("error: " + file);
        }
    };
}

From source file:com.gambo.web.client.WebDisplay.java

License:Open Source License

public WebDisplay(Mmu mmu, Canvas canvas) {
    super(mmu);// w  w  w  .  j  av  a2 s .  c  o  m

    this.canvas = canvas;

    width = canvas.getCoordinateSpaceWidth();
    height = canvas.getCoordinateSpaceHeight();

    this.bufferImage = canvas.getContext2d().createImageData(width, height);
    this.cpa = bufferImage.getData();
}

From source file:com.google.gwt.sample.userwatcher.client.FileUploaderWidget_v2.java

private void drawThumbToScreen(Image image) {

    int size = 75;

    int width = image.getWidth();
    int height = image.getHeight();

    double scaleToRatio = 0.50D; // TODO oops, getting image loading problem. event loading, fix later...

    ImageData imageDataForThumb = ImageUtils.scaleImage(image, scaleToRatio);

    // for debugging
    Canvas canvasTmp = Canvas.createIfSupported();
    canvasTmp.setCoordinateSpaceHeight((int) imageDataForThumb.getHeight());
    canvasTmp.setCoordinateSpaceWidth((int) imageDataForThumb.getWidth());
    Context2d context = canvasTmp.getContext2d();
    context.putImageData(imageDataForThumb, 0, 0);
    pThumb.add(canvasTmp);/* w  w w. ja  va  2s . co m*/

    canvasTmp.addStyleName("test2");
}

From source file:com.google.gwt.sample.userwatcher.client.FileUploaderWidget_v2.java

private void upload(final Image image) {

    Canvas canvasTmp = Canvas.createIfSupported();
    canvasTmp.setCoordinateSpaceHeight((int) image.getHeight());
    canvasTmp.setCoordinateSpaceWidth((int) image.getWidth());
    Context2d context = canvasTmp.getContext2d();
    ImageElement imageElement = ImageElement.as(image.getElement());
    context.drawImage(imageElement, 0, 0);

    String contentType = "image/jpeg";

    String fileBase64 = canvasTmp.toDataUrl(contentType);

    UploadImage iu = new UploadImage(cp);
    iu.setFile(1234568759L, fileName, contentType, fileBase64);

    // TODO enable this if your testing it!!!!!!!!!!
    iu.upload();/* ww  w.j  a v  a  2  s.  c o  m*/

    iu.addChangeHandler(new ChangeHandler() {
        public void onChange(ChangeEvent event) {
            UploadImage u = (UploadImage) event.getSource();
            int e = u.getChangeEvent();
            fireChange(e);
        }
    });
}

From source file:com.google.gwt.sample.userwatcher.client.ImageUtils.java

public static ImageData cropImage(Image image, double sx, double sy, double sw, double sh) {

    Canvas canvasTmp = Canvas.createIfSupported();
    Context2d context = canvasTmp.getContext2d();

    canvasTmp.setCoordinateSpaceHeight((int) sh + 10);
    canvasTmp.setCoordinateSpaceWidth((int) sw + 10);

    ImageElement imageElement = ImageElement.as(image.getElement());

    double dx = 0;
    double dy = 0;
    double dw = sw;
    double dh = sh;

    // draw image to canvas
    context.drawImage(imageElement, sx, sy, sw, sh, dx, dy, dw, dh);

    // get image data
    double w = sw;
    double h = sh;
    ImageData imageData = context.getImageData(0, 0, w, h);

    canvasTmp.removeFromParent();/* w ww.  ja v  a  2 s  .c  o m*/

    return imageData;
}

From source file:com.google.gwt.sample.userwatcher.client.ImageUtils.java

public static ImageData scaleAndCropImage(Image image, double scaleToRatio, double sx, double sy, double sw,
        double sh) {

    Canvas canvasTmp = Canvas.createIfSupported();
    //RootPanel.get().add(canvasTmp);
    Context2d context = canvasTmp.getContext2d();

    double ch = (image.getHeight() * scaleToRatio) + 100;
    double cw = (image.getWidth() * scaleToRatio) + 100;

    canvasTmp.setCoordinateSpaceHeight((int) ch);
    canvasTmp.setCoordinateSpaceWidth((int) cw);

    ImageElement imageElement = ImageElement.as(image.getElement());

    // tell it to scale image
    context.scale(scaleToRatio, scaleToRatio);

    // draw image to canvas
    // s = source
    // d = destination     
    double dx = 0;
    double dy = 0;
    context.drawImage(imageElement, dx, dy);

    // get image data - if you go greater than the scaled image nothing will show up
    ImageData imageData = context.getImageData(sx, sy, sw, sh);

    return imageData;
}

From source file:com.google.gwt.sample.userwatcher.client.ImageUtils.java

/**
 * for debugging/*from www . jav a2  s.  com*/
 * 
 * @param imageData
 */
public static void addImageToScreen(ImageData imageData) {

    Canvas canvasTmp = Canvas.createIfSupported();
    canvasTmp.setCoordinateSpaceHeight((int) imageData.getHeight() + 10);
    canvasTmp.setCoordinateSpaceWidth((int) imageData.getWidth() + 10);
    Context2d context = canvasTmp.getContext2d();

    context.putImageData(imageData, 0, 0);

    //flexTable.setWidget(r, c, canvasTmp);
    RootPanel.get().add(canvasTmp);
}

From source file:com.google.gwt.sample.userwatcher.client.ImageUtils.java

/**
 * image - an ImageElement object/*from  www.j  av a 2s. c  o m*/
 * 
  sx - the x coordinate of the upper-left corner of the source rectangle
  sy - the y coordinate of the upper-left corner of the source rectangle
  sw - the width of the source rectangle
  sh - the width of the source rectangle
  dx - the x coordinate of the upper-left corner of the destination rectangle
  dy - the y coordinate of the upper-left corner of the destination rectangle
  dw - the width of the destination rectangle
  dh - the height of the destination rectangle
        
 */
public static ImageData scaleImage(Image image, double scaleToRatio) {

    //System.out.println("PanoTiler.scaleImag()e: scaleToRatio=" + scaleToRatio + " width=" + width + " x height=" + height);

    Canvas canvasTmp = Canvas.createIfSupported();
    Context2d context = canvasTmp.getContext2d();

    double ch = (image.getHeight() * scaleToRatio) + 100; // 100 is offset so it doesn't throw
    double cw = (image.getWidth() * scaleToRatio) + 100;

    canvasTmp.setCoordinateSpaceHeight((int) ch);
    canvasTmp.setCoordinateSpaceWidth((int) cw);

    ImageElement imageElement = ImageElement.as(image.getElement());

    // s = source
    // d = destination 
    double sx = 0;
    double sy = 0;
    double sw = imageElement.getWidth();
    double sh = imageElement.getHeight();

    double dx = 0;
    double dy = 0;
    double dw = imageElement.getWidth();
    double dh = imageElement.getHeight();

    // tell it to scale image
    context.scale(scaleToRatio, scaleToRatio);

    // draw image to canvas
    context.drawImage(imageElement, sx, sy, sw, sh, dx, dy, dw, dh);

    // get image data
    double w = dw * scaleToRatio;
    double h = dh * scaleToRatio;
    ImageData imageData = context.getImageData(0, 0, w, h); // this won't get the extra 100

    return imageData;
}

From source file:com.google.gwt.sample.userwatcher.client.ImageUtils.java

public static ImageData cropImage(ImageData imageData, double sx, double sy, double sw, double sh) {

    Canvas canvasTmp = Canvas.createIfSupported();
    canvasTmp.setStyleName("mainCanvas");
    Context2d context = canvasTmp.getContext2d();

    canvasTmp.setCoordinateSpaceHeight((int) imageData.getHeight() + 10);
    canvasTmp.setCoordinateSpaceWidth((int) imageData.getWidth() + 10);

    // draw image to canvas
    context.putImageData(imageData, 0, 0);

    // get image data
    //imageData = context.getImageData(0, 0, imageData.getWidth(), imageData.getHeight());
    ImageData newImageData = context.getImageData(sx, sy, sw, sh);

    return newImageData;
}

From source file:com.googlecode.mgwt.image.client.ImageConverter.java

License:Apache License

public void convert(final ImageResource resource, String color,
        final ImageConverterCallback imageConverterCallback) {

    if (color == null) {
        throw new IllegalArgumentException();
    }/*  w w  w  .j  a va2  s .com*/

    if (!color.startsWith("#")) {
        throw new IllegalArgumentException();
    }

    color = maybeExpandColor(color);

    final int hexColor = Integer.parseInt(color.substring(1), 16);

    final int red = hexColor >> 16 & 0xFF;
    final int green = hexColor >> 8 & 0xFF;
    final int blue = hexColor & 0xFF;

    final int height = resource.getHeight();
    final int width = resource.getWidth();

    loadImage(resource.getSafeUri().asString(), width, height, new LoadImageCallback() {

        @Override
        public void onSuccess(ImageElement imageElement) {

            Canvas canvas = Canvas.createIfSupported();
            canvas.getElement().setPropertyInt("height", height);
            canvas.getElement().setPropertyInt("width", width);

            Context2d context = canvas.getContext2d();
            context.drawImage(imageElement, 0, 0);
            ImageData imageData = context.getImageData(0, 0, width, height);

            CanvasPixelArray canvasPixelArray = imageData.getData();

            for (int i = 0; i < canvasPixelArray.getLength(); i += 4) {
                canvasPixelArray.set(i, red);
                canvasPixelArray.set(i + 1, green);
                canvasPixelArray.set(i + 2, blue);
                canvasPixelArray.set(i + 3, canvasPixelArray.get(i + 3));
            }
            context.putImageData(imageData, 0, 0);
            imageConverterCallback.onSuccess(new ConvertedImageResource(canvas.toDataUrl("image/png"),
                    resource.getWidth(), resource.getHeight()));
        }
    });
}