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

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

Introduction

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

Prototype

public static Canvas createIfSupported() 

Source Link

Document

Return a new Canvas if supported, and null otherwise.

Usage

From source file:anagram.client.GwtCanvasDemo.java

License:Apache License

public void onModuleLoad() {
    canvas = Canvas.createIfSupported();
    backBuffer = Canvas.createIfSupported();
    if (canvas == null) {
        RootPanel.get(holderId).add(new Label(upgradeMessage));
        return;/*from   w ww. j a v a 2 s . c o  m*/
    }

    // init the canvases
    canvas.setWidth(width + "px");
    canvas.setHeight(height + "px");
    canvas.setCoordinateSpaceWidth(width);
    canvas.setCoordinateSpaceHeight(height);
    backBuffer.setCoordinateSpaceWidth(width);
    backBuffer.setCoordinateSpaceHeight(height);
    RootPanel.get(holderId).add(canvas);
    context = canvas.getContext2d();
    backBufferContext = backBuffer.getContext2d();

    // init the objects
    logoGroup = new LogoGroup(width, height, 18, 165);
    ballGroup = new BallGroup(width, height);
    lens = new Lens(35, 15, width, height, new Vector(320, 150), new Vector(1, 1));

    // init handlers
    initHandlers();

    // setup timer
    final Timer timer = new Timer() {
        @Override
        public void run() {
            doUpdate();
        }
    };
    timer.scheduleRepeating(refreshRate);
}

From source file:annis.gui.widgets.gwt.client.ui.VSimpleCanvas.java

License:Apache License

/**
 * The constructor should first call super() to initialize the component and
 * then handle any initialization relevant to Vaadin.
 *//*from w w w. ja  v a  2s  .co m*/
public VSimpleCanvas() {
    super();

    canvas = Canvas.createIfSupported();

    if (canvas == null) {
        Label lblErrorMessage = new Label("Your browser does not support the Canvas element.");
        initWidget(lblErrorMessage);
    } else {
        initWidget(canvas);

        canvas.setHeight("" + height + "px");
        canvas.setWidth("" + width + "px");
        canvas.setCoordinateSpaceHeight(height);
        canvas.setCoordinateSpaceWidth(width);

        context = canvas.getContext2d();
    }

    // This method call of the Paintable interface sets the component
    // style name in DOM tree
    setStyleName(CLASSNAME);

}

From source file:ch.unifr.pai.twice.multipointer.client.widgets.MultiFocusTextBox.java

License:Apache License

public MultiFocusTextBox() {
    blinkTimer = new Timer() {

        @Override/*from   ww w .j av  a 2s. com*/
        public void run() {
            for (Cursor c : cursors.values()) {
                c.setVisible(cursorsVisible);
            }
            cursorsVisible = !cursorsVisible;
        }
    };
    blinkTimer.scheduleRepeating(cursorSpeed);
    p.getElement().getStyle().setDisplay(Display.INLINE_BLOCK);
    c = Canvas.createIfSupported();
    c.setCoordinateSpaceWidth(10000);
    c.addStyleName("multiFocusWidget");
    c.getElement().getStyle().setBorderWidth(0, Unit.PX);
    c.getElement().getStyle().setProperty("outline", "none");
    c.addKeyPressHandler(new KeyPressHandler() {

        @Override
        public void onKeyPress(KeyPressEvent event) {
            processInput(MultiCursorController.getUUID(event.getNativeEvent()), event.getCharCode());
        }
    });
    c.addKeyUpHandler(new KeyUpHandler() {

        @Override
        public void onKeyUp(KeyUpEvent event) {
            Cursor c = cursors.get(MultiCursorController.getUUID(event.getNativeEvent()));
            if (c != null) {
                switch (event.getNativeKeyCode()) {
                case KeyCodes.KEY_LEFT:
                    c.setPosition(Math.max(0, c.position - 1));
                    scrollIfNecessary();
                    break;
                case KeyCodes.KEY_RIGHT:
                    c.setPosition(Math.min(value.length(), c.position + 1));
                    scrollIfNecessary();
                    break;
                case KeyCodes.KEY_UP:
                    c.setPosition(0);
                    scrollIfNecessary();
                    break;
                case KeyCodes.KEY_DOWN:
                    c.setPosition(value != null ? value.length() : 0);
                    scrollIfNecessary();
                    break;
                case KeyCodes.KEY_DELETE:
                    if (value != null && c.position < value.length()) {
                        setValue(value.substring(0, c.position) + value.substring(c.position + 1));
                        for (Cursor cursor : cursors.values()) {
                            if (c.position < cursor.getPosition()) {
                                cursor.setPosition(cursor.getPosition() - 1);
                            }
                        }
                        scrollIfNecessary();
                    }
                    break;
                case KeyCodes.KEY_BACKSPACE:
                    if (value != null && c.position > 0 && c.position <= value.length()) {
                        setValue(value.substring(0, c.position - 1) + value.substring(c.position));
                        c.setPosition(c.position - 1);
                        for (Cursor cursor : cursors.values()) {
                            if (c.position < cursor.position) {
                                cursor.setPosition(cursor.getPosition() - 1);
                            }
                        }
                        scrollIfNecessary();
                    }
                    break;
                }
            }
        }
    });
    c.addClickHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {
            repositionCursor(MultiCursorController.getUUID(event.getNativeEvent()),
                    MultiCursorController.getColorNative(event.getNativeEvent()),
                    event.getRelativeX(c.getCanvasElement()), event.getRelativeY(c.getCanvasElement()));
        }
    });
    multiFocus.insert(c, 0, 0, 0);
    initWidget(multiFocus);
    context = c.getContext2d();
    context.setTextAlign(TextAlign.LEFT);
    context.setTextBaseline(TextBaseline.TOP);
    context.setFont("normal " + fontSize + "px sans-serif");
    c.getElement().getStyle().setPadding(padding, Unit.PX);
    setStyle();
    // TODO Auto-generated constructor stub
    // multiFocus.setVisible(false);

    multiFocus.setWidth("161px");
    multiFocus.setHeight("25px");

}

From source file:ch.unifr.pai.twice.multipointer.provider.client.widgets.MultiFocusTextBox.java

License:Apache License

public MultiFocusTextBox() {

    blinkTimer = new Timer() {

        @Override//from   w  ww  . ja va2 s .  c o m
        public void run() {
            for (Cursor c : cursors.values()) {
                c.setVisible(cursorsVisible);
            }
            cursorsVisible = !cursorsVisible;
        }
    };

    blinkTimer.scheduleRepeating(cursorSpeed);
    p.getElement().getStyle().setDisplay(Display.INLINE_BLOCK);
    c = Canvas.createIfSupported();
    c.setCoordinateSpaceWidth(10000);
    c.addStyleName("multiFocusWidget");
    c.getElement().getStyle().setBorderWidth(0, Unit.PX);
    c.getElement().getStyle().setProperty("outline", "none");
    c.addKeyPressHandler(new KeyPressHandler() {

        @Override
        public void onKeyPress(KeyPressEvent event) {
            processInput(NoMultiCursorController.getUUID(event.getNativeEvent()), event.getCharCode());
            String textcolor = NoMultiCursorController.getColorNative(event.getNativeEvent());
            setTextColor(textcolor);

        }
    });

    c.addKeyUpHandler(new KeyUpHandler() {

        @Override
        public void onKeyUp(KeyUpEvent event) {
            Cursor c = cursors.get(NoMultiCursorController.getUUID(event.getNativeEvent()));

            if (c != null) {

                switch (event.getNativeKeyCode()) {
                case KeyCodes.KEY_LEFT:
                    c.setPosition(Math.max(0, c.position - 1));
                    scrollIfNecessary();
                    break;
                case KeyCodes.KEY_RIGHT:
                    c.setPosition(Math.min(value.length(), c.position + 1));
                    scrollIfNecessary();
                    break;
                case KeyCodes.KEY_UP:
                    c.setPosition(0);
                    scrollIfNecessary();
                    break;
                case KeyCodes.KEY_DOWN:
                    c.setPosition(value != null ? value.length() : 0);
                    scrollIfNecessary();
                    break;
                case KeyCodes.KEY_DELETE:
                    if (value != null && c.position < value.length()) {
                        setValue(value.substring(0, c.position) + value.substring(c.position + 1));
                        for (Cursor cursor : cursors.values()) {
                            if (c.position < cursor.getPosition()) {
                                cursor.setPosition(cursor.getPosition() - 1);
                            }
                        }
                        scrollIfNecessary();
                    }
                    break;
                case KeyCodes.KEY_BACKSPACE:
                    if (value != null && c.position > 0 && c.position <= value.length()) {
                        setValue(value.substring(0, c.position - 1) + value.substring(c.position));
                        c.setPosition(c.position - 1);
                        for (Cursor cursor : cursors.values()) {
                            if (c.position < cursor.position) {
                                cursor.setPosition(cursor.getPosition() - 1);
                            }
                        }
                        scrollIfNecessary();
                    }
                    break;
                case KeyCodes.KEY_ENTER: //ICEExperiments-TextEdit
                    setValue("");
                    c.setPosition(0);

                    break;
                }
            }
        }
    });

    c.addClickHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {
            repositionCursor(NoMultiCursorController.getUUID(event.getNativeEvent()),
                    NoMultiCursorController.getColorNative(event.getNativeEvent()),
                    event.getRelativeX(c.getCanvasElement()), event.getRelativeY(c.getCanvasElement()));
        }
    });
    multiFocus.insert(c, 0, 0, 0);
    initWidget(multiFocus);
    context = c.getContext2d();
    context.setTextAlign(TextAlign.LEFT);
    context.setTextBaseline(TextBaseline.TOP);
    context.setFont("normal " + fontSize + "px sans-serif");
    c.getElement().getStyle().setPadding(padding, Unit.PX);
    setStyle();
    // TODO Auto-generated constructor stub
    // multiFocus.setVisible(false);

    multiFocus.setWidth("161px");
    multiFocus.setHeight("25px");

}

From source file:ch.unifr.pai.twice.widgets.client.MultiFocusTextBox.java

License:Apache License

public MultiFocusTextBox() {
    blinkTimer = new Timer() {

        @Override/*from   w  w  w  .  j a va2  s  .  c  om*/
        public void run() {
            for (Cursor c : cursors.values()) {
                c.setVisible(cursorsVisible);
            }
            cursorsVisible = !cursorsVisible;
        }
    };
    blinkTimer.scheduleRepeating(cursorSpeed);
    p.getElement().getStyle().setDisplay(Display.INLINE_BLOCK);
    c = Canvas.createIfSupported();
    c.getElement().getStyle().setBorderWidth(0, Unit.PX);
    c.getElement().getStyle().setProperty("outline", "none");
    c.addClickHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {
            //TODO if it is a new device, create a new cursor with the 
            repositionCursor(null, event.getRelativeX(c.getCanvasElement()),
                    event.getRelativeY(c.getCanvasElement()));
        }
    });
    multiFocus.insert(c, 0, 0, 0);
    initWidget(multiFocus);
    getElement().getStyle().setBorderStyle(BorderStyle.SOLID);
    getElement().getStyle().setBorderWidth(1, Unit.PX);
    c.getElement().getStyle().setMargin(5, Unit.PX);
    context = c.getContext2d();
    context.setTextAlign(TextAlign.LEFT);
    context.setTextBaseline(TextBaseline.TOP);
    context.setFont("13px sans-serif;");

    // TODO Auto-generated constructor stub
    //      multiFocus.setVisible(false);

    multiFocus.setWidth("161px");
    multiFocus.setHeight("28px");

}

From source file:cl.uai.client.page.MarkingPage.java

License:Open Source License

/**
 * /*from  www  .j a v  a2 s  . c o  m*/
 */
public MarkingPage(int pagenum, String image, int width, int height, List<Map<String, String>> pageMarks) {
    this.pageNumber = pagenum;
    this.pageImage = new Image(image);
    this.width = width;
    this.height = height;
    this.pageImage.setWidth(this.width + "px");
    this.pageImage.setHeight(this.height + "px");

    logger.fine("Adding page " + pagenum + " Url:" + image);

    this.marks = new HashMap<Integer, Mark>();

    for (Map<String, String> markMap : pageMarks) {

        try {
            int index = Integer.parseInt(markMap.get("format"));
            ButtonFormat format = index < 1000 ? MarkingButtons.getButtonFormatFromCode(index)
                    : ButtonFormat.BUTTON_CUSTOM;
            fixPositions(markMap, width, height);
            Mark mark = null;
            switch (format) {
            case BUTTON_COMMENT:
                mark = CommentMark.createFromMap(markMap);
                break;
            case BUTTON_RUBRIC:
                mark = RubricMark.createFromMap(markMap);
                break;
            case BUTTON_TICK:
                mark = CheckMark.createFromMap(markMap);
                break;
            case BUTTON_CROSS:
                mark = CrossMark.createFromMap(markMap);
                break;
            case BUTTON_PEN:
                mark = PathMark.createFromMap(markMap);
                break;
            case BUTTON_HIGHLIGHT:
                mark = HighlightMark.createFromMap(markMap);
                break;
            case BUTTON_QUESTION:
                mark = QuestionMark.createFromMap(markMap);
                break;
            case BUTTON_CUSTOM:
                mark = CustomMark.createFromMap(markMap);
                break;
            default:
                logger.severe("Invalid format for comment");
                mark = null;
                break;
            }

            if (mark != null) {
                this.marks.put(mark.getId(), mark);
            }

        } catch (Exception e) {
            e.printStackTrace();
            logger.severe(
                    "Exception creating mark from DB. " + markMap.toString() + " Error:" + e.getMessage());
        }
    }

    mainPanel = new FocusPanel();
    absolutePanel = new AbsolutePanel();
    absolutePanel.addStyleName(Resources.INSTANCE.css().absolutepage());
    drawingArea = new DrawingArea(width, height);

    mainPanel.addClickHandler(new MarkingPageClickHandler(this));

    dropController = new MarkingPageDropController(absolutePanel, this);
    EMarkingWeb.markingInterface.getDragController().registerDropController(dropController);

    // Initialize drag and drop controller, attached to the absolute panel
    dragController = new PickupDragController(absolutePanel, true);
    dragController.setBehaviorDragStartSensitivity(1);
    dragController.setBehaviorScrollIntoView(false);
    MarkingPageDragHandler dragHandler = new MarkingPageDragHandler(absolutePanel, this);
    dragController.addDragHandler(dragHandler);

    //Initialize Drawing controller for pen tool
    drawController = new DrawController();
    //The draw controller listens to the drawing area
    drawController.listenTo(mainPanel);
    //The drag handler listens to the draw controller
    MarkingPageDrawHandler drawHandler = new MarkingPageDrawHandler(absolutePanel, drawingArea, this);
    drawController.addListener(drawHandler);
    MarkingPageHighlightHandler highlightHandler = new MarkingPageHighlightHandler(absolutePanel, drawingArea,
            this);
    drawController.addListener(highlightHandler);
    absolutePanel.add(drawingArea, 0, 0);
    absolutePanel.add(pageImage);

    canvas = Canvas.createIfSupported();
    if (canvas != null) {
        canvas.setSize(width + "px", height + "px");
        canvas.setCoordinateSpaceWidth(width);
        canvas.setCoordinateSpaceHeight(height);
        canvas.addStyleName(Resources.INSTANCE.css().pagecanvas());
        absolutePanel.add(canvas, 0, 0);
    }

    mainPanel.add(absolutePanel);

    this.initWidget(mainPanel);

    addDomHandler(this, ContextMenuEvent.getType());
}

From source file:co.fxl.gui.canvas.gwt.GWTCanvas.java

License:Open Source License

@SuppressWarnings("unchecked")
static ICanvas create(IContainer c) {
    Canvas canvas = Canvas.createIfSupported();
    c.nativeElement(canvas);/*from  ww  w .java  2 s  .  co  m*/
    return new GWTCanvas((GWTContainer<Canvas>) c);
}

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

License:Open Source License

public CanVg(String svgContent, String width, String height) {
    surface = Canvas.createIfSupported();
    if (surface == null) {
        Window.alert(unsupportedBrowser);
        return;/*from  w w  w . j a v  a2 s . com*/
    }
    this.initWidget(surface);
    elementId = IdGenerator.generateId();
    html = svgContent;
    surface.getElement().setId(elementId);
    surface.setWidth(width);
    surface.setHeight(height);
    this.getElement().getStyle().setPosition(Position.ABSOLUTE);
    this.getElement().getStyle().setLeft(position, Unit.PX);
    this.getElement().getStyle().setRight(position, Unit.PX);
    RootPanel.get().add(this);
    createContent(elementId, html);
    getBase64();
}

From source file:com.akjava.gwt.html5.client.ColorPickWidget.java

License:Apache License

public ColorPickWidget() {
    initWidget(uiBinder.createAndBindUi(this));

    controls.setVisible(false);//from www  . jav a2 s.c  o m
    plus.setVisible(false);
    minus.setVisible(false);

    hex.setWidth("50px");
    range = InputRangeWidget.createInputRange(0, 15, 0);
    range.setWidth("160px");
    range.addMouseUpHandler(new MouseUpHandler() {
        @Override
        public void onMouseUp(MouseUpEvent event) {
            update();
        }
    });

    control.add(range);

    canvas = Canvas.createIfSupported();
    canvas.setSize("160px", "160px");
    canvas.setCoordinateSpaceHeight(160);
    canvas.setCoordinateSpaceWidth(160);
    canvas.addMouseWheelHandler(new MouseWheelHandler() {

        @Override
        public void onMouseWheel(MouseWheelEvent event) {
            int d = event.getDeltaY();
            if (d > 0) {
                plus();
            } else if (d < 0) {
                minus();
            }
        }
    });
    canvas.addMouseMoveHandler(new MouseMoveHandler() {

        @Override
        public void onMouseMove(MouseMoveEvent event) {
            int color = getColorAt(event.getX(), event.getY());
            current.setText(toCssColor(color).toUpperCase());
        }
    });
    canvas.addClickHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {

            int color = getColorAt(event.getX(), event.getY());
            setValue(color);

        }
    });
    main.add(canvas);
    draw();
    setValue(0xffffff);
}

From source file:com.akjava.gwt.threetest.client.CanvasDemo.java

License:Apache License

@Override
public void start(final WebGLRenderer renderer, final int width, final int height, FocusPanel panel) {
    super.start(renderer, width, height, panel);
    canvas = Canvas.createIfSupported();
    canvas.setCoordinateSpaceWidth(100);
    canvas.setCoordinateSpaceHeight(100);
    canvas.getContext2d().setFillStyle("#cccccc");
    canvas.getContext2d().fillRect(0, 0, canvas.getCoordinateSpaceWidth(), canvas.getCoordinateSpaceHeight());
    canvas.getContext2d().strokeText("Hello World", 25, 25);
    String url = canvas.toDataUrl();
    Image img = new Image(url);
    ImageElement imageElement = ImageElement.as(img.getElement());
    Texture texture = THREE.Texture(imageElement);
    texture.setNeedsUpdate(true);/*from  w  w  w.ja v  a  2s  .  c o  m*/

    final Scene scene = THREE.Scene();
    final Camera camera = THREE.PerspectiveCamera(35, (double) width / height, .1, 10000);
    scene.add(camera);
    cameraControle.setPositionZ(20);

    final Mesh mesh = THREE.Mesh(THREE.CubeGeometry(5, 5, 5), THREE.MeshLambertMaterial().map(texture).build());
    scene.add(mesh);

    final Light light = THREE.PointLight(0xffffff);
    light.setPosition(10, 0, 10);
    scene.add(light);

    Timer timer = new Timer() {
        public void run() {
            MainWidget.stats.update();
            camera.setPosition(cameraControle.getPositionX(), cameraControle.getPositionY(),
                    cameraControle.getPositionZ());

            mesh.setRotation(cameraControle.getRadiantRotationX(), cameraControle.getRadiantRotationY(),
                    cameraControle.getRadiantRotationZ());

            renderer.render(scene, camera);
        }
    };

    startTimer(timer);
}