aframevr / aframevr/aframe

Ability to autosize text container

Open
#2,675 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
17.6k
Forks
4.4k
PR merge metrics
No merged PRs in 30d

Description

It would be great to have a way to render a text on a background with some padding around it.

I’ve tried an example from docs, however, it seems that auto width mode actually sets the maximum width of the container, leaving the gap on the right:

screen shot 2017-05-18 at 14 31 53

What I would like to achieve is an ability to render a label with some background around it (similar to Bootstrap labels for example)

Here is a hacky solution that I came up with:

import AFRAME from 'aframe';


const fontWidthFactor = 1000;

AFRAME.registerComponent('text-auto-width', {
    dependencies: ['text'],
    schema: {
        padding: {default: 0}
    },
    init() {
        // Hook into text component's updateLayout method
        const textComponent = this.el.components.text;

        const textMesh = this.el.object3D.children[1];

        if (textMesh.geometry.layout) {
            resize(textMesh.geometry.layout);
        } else {
            const origUpdate = textComponent.updateLayout;

            textComponent.updateLayout = (data) => {
                origUpdate.call(textComponent, data);

                resize(textMesh.geometry.layout);
            };
        }

        const resize = (layout) => {
            const {value, width} = this.el.getAttribute('text');

            const metrics = layout.computeMetrics(value, 0, value.length, Math.INT_MAX);

            const actualWidth = metrics.width * width / fontWidthFactor + this.data.padding * 2;

            const geometry = this.el.getAttribute('geometry');

            this.el.setAttribute('geometry', {...geometry, width: actualWidth});
        }
    }
});

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the text component's auto-width behavior and the provided text-auto-width JavaScript example, especially its layout measurement and geometry resizing. Done means a text label can render with a background sized to the text plus padding, without the reported gap on the right.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
ar-vr-xr
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.