art icon indicating copy to clipboard operation
art copied to clipboard

Add fallback for missing font option in canvas/text

Open johannhof opened this issue 10 years ago • 5 comments

When using react-art it's hard to understand how text is supposed to work, especially considering that

<Text x={10} y={10}>Hello World!</Text>

renders in SVG mode but not in canvas mode.

This is because in canvas mode, without specifying font={fontSize:12}, lineHeight in renderShapeTo turns to NaN and takes the y parameter down with it.

Considering the dramatic lack of documentation for this otherwise excellent library we should add a fallback so that people don't get demotivated and the render behaviour stays consistent.

johannhof avatar Jul 11 '15 05:07 johannhof

Does SVG inherit font from the surrounding context or what?

sophiebits avatar Jul 15 '15 14:07 sophiebits

I don't think so but the point is rather that we produce a NaN in the position calculation for the y field without this change, and unsurprisingly, a position of NaN is not handled very gracefully by the canvas.

johannhof avatar Jul 15 '15 15:07 johannhof

I'm talking about this part

        var em = this._fontSize,
            y = em / 2,
            lineHeight = 1.1 * em,
            lines = text,
            l = lines.length;

        if (fill){
            context.fillStyle = fill;
            for (var i = 0; i < l; i++)
                context.fillText(lines[i], 0, y + i * lineHeight);
        }

johannhof avatar Jul 15 '15 15:07 johannhof

Right. Ideally we can pick some default behavior that's consistent across the SVG and Canvas (and VML) implementations.

sophiebits avatar Jul 19 '15 04:07 sophiebits

Ah, I see your point. I'll try to find out what the current default behaviour for SVG and VML is.

johannhof avatar Jul 25 '15 05:07 johannhof