GithubHelp home page GithubHelp logo

vaadincanvas's Introduction

What's this?

This is a server-side Vaadin add-on for the HTML5 canvas. You add the Canvas component to your Vaadin UI, and can then draw on it using the Java API.

Downloads

Sources are available here, and you can download the add-on package from the Vaadin add-on Directory.

Compatibility

This add-on is compatible with Vaadin 7 RC2 and up. A canvas add-on for Vaadin 6 is available here.

Installation

To install the add-on to your Vaadin project just copy the JAR file from Vaadin Directory, and compile your widgetset (more details here).

Alternatively you can add the add-on to your project using Maven (see the Maven POM snippet required from Vaadin Directory).

Basic Usage

Here is a trivial Vaadin application that uses the add-on:

public class TestUI extends UI {
    private Canvas canvas;

    @Override
    protected void init(VaadinRequest request) {
        VerticalLayout content = new VerticalLayout();
        setContent(content);

        // Instantiate the component and add it to your UI
        content.addComponent(canvas = new Canvas());

        // Draw a 20x20 filled rectangle with the upper left corner
        // in coordinate 10,10. It will be filled with the default
        // color which is black.
        canvas.fillRect(10,  10,  20,  20);
    }
}

Mouse events

The Canvas component supports listening to mouse clicks within the canvas area. It works simply by adding a CanvasClickListener to the component. Unfortunately the HTML5 Canvas API doesn't support objects, so you can only find out the coordinates where the click happened.

You can find out other details related to the click event using the MouseEventDetails instance you receive from the listener.

Working with images

The HTML5 Canvas image support contains a few pitfalls that complicate drawing images to the canvas. In practice, you should ensure that the image has been preloaded to the browser cache before trying to draw it.

To make sure that your images are preloaded before drawing you should first tell the component to load your images using the loadImages(String[] urls) method, and add a CanvasImageLoadListener to the Canvas. The listener will notify you when the images have been loaded, and you can start issuing drawImage() that work with the preloaded images.

vaadincanvas's People

Watchers

Mikko Taivainen avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.