Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Texture problem with detached DOM elements #26

Open
jgottero opened this issue Feb 22, 2015 · 4 comments · May be fixed by #28
Open

Texture problem with detached DOM elements #26

jgottero opened this issue Feb 22, 2015 · 4 comments · May be fixed by #28

Comments

@jgottero
Copy link

When you create a texture from a DOM element, for example a canvas, if this element is not attached to the document, the texture will not work.

The reason is texture size is read from the element's offsetWidth/offsetHeight properties, and these properties are always 0 for DOM detached elements; hence texture size will be (0,0).

            boolean isImagePowerOfTwo = Mathematics.isPowerOfTwo( image.getOffsetWidth() ) 
                    && Mathematics.isPowerOfTwo( image.getOffsetHeight() );

(NB: I pasted these few lines of code but same problem in others methods.)

Three.js read width/height properties instead, which is nice because you must fill these properties when you create a canvas to use it as a texture; thus you dont need to attach the canvas to the document.
One possible solution would be to first read width/height properties, then having a fallback on offsetWidth/Height (I can provide a patch if you wish and if you agree on this solution).

@thothbot
Copy link
Owner

Can you provide your patch. I'll have to take a look at it.

jgottero added a commit to jgottero/parallax that referenced this issue Feb 26, 2015
Patch proposal to fix thothbot#26
When DOM elements are detached from the document, offsetWidth/Height are
always 0: whereas the element may have proper size set in width/height
attributes. This patch adds a fallback on width/height properties when
offsetWidth/Height are 0. It allows to use a canvas as a texture without
having to attach it first.
@jgottero jgottero linked a pull request Feb 26, 2015 that will close this issue
@jgottero
Copy link
Author

Here is my texture generation code:

    private Texture createTextTexture(double size, double margin, String text, Color color, Color backColor) {
        if (text == null)
            return null;
        Canvas canvas = Canvas.createIfSupported();
        Context2d context = canvas.getContext2d();
        double width = size + margin;
        double height = size + margin;
        canvas.setCoordinateSpaceWidth((int) width);
        canvas.setCoordinateSpaceHeight((int) height);
        context.setFont(size + "pt Arial");
        context.setFillStyle("#" + getHexString(backColor));
        context.fillRect(0, 0, width, height);
        context.setTextAlign("center");
        context.setTextBaseline("middle");
        context.setFillStyle("#" + getHexString(color));
        context.fillText(text, width / 2, height / 2);

        // Without the fix, this line is mandatory, otherwise the texture wont display
//        RootPanel.get().add(canvas, -10000, 0);

        Texture texture = new Texture(canvas.getElement());
        texture.setNeedsUpdate(true);
        return texture;
    }

Example usage:
createTextTexture(10, 20, "0", new Color(0xaaaaaa), new Color(0x202020));

@thothbot
Copy link
Owner

This is very nice solution. I suppose we should create TextTexture class in Parallax for this. What do you think?

@jgottero
Copy link
Author

I think it would be a nice addition, as rendering text is easy with canvas and well supported by browsers, but not everybody will have the idea to use it.
There are a few choices to consider:

  • Should it handle multiline text (this is not builtin in canvas but can be achieved with canvas' measureText())
  • Should the texture size be computed automatically based on font size and margin, as a power of 2

I'm a WebGL beginner and I wouldn't be able to tell what are the most common use cases for text textures, but the class should be easy to extends to cover custom use cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants