How to use a canvas as a cell of a celltable widget with GWT?

334 views Asked by At

All is in the question, how can I create a custom cell with a canvas on it by using a CellTable in GWT ?

I search a way to transform the canvas into html to append it to the SafeHtmlBuilder parameter of the render method but with no success. Here is the interesting snippet of the custom cell:

public void render(Context context, String value, SafeHtmlBuilder sb) {

    Canvas c = Canvas.createIfSupported();

    // create a text into the canvas using the value parameter
    // something like (this is not important) : 
    c.getContext2d().drawTheText(value);

    // here is the problem, what kind of transformation may I do 
    // to use the canvas in this cell ?
    SafeHtml safeValue = SafeHtmlUtils.fromString(c.?????);
    sb.append(safeValue);
}

EDIT: here is the working solution, thanks to Thomas

sb.append(SafeHtmlUtils.fromTrustedString("<img src=\"" + canvas.toDataUrl() + "\" />"));

note that a template should be used instead of using directly a piece of html code as it.

1

There are 1 answers

3
Thomas Broyer On BEST ANSWER

I think you'll have to use toDataURL() and build an <img> element to display it.

Note that you could then reuse the same Canvas instance between invocations of render; just make sure you clear it before reuse.