简体   繁体   English

在A-Frame中固定文本位置

[英]Fixed text position in A-Frame

I am trying to add fixed text in a-frame. 我正在尝试在框架中添加固定文本。 I can add a a-text tag like this: 我可以添加一个文本标签,如下所示:

<a-camera wasd-controls-enabled="true" position="0 1 0" look-controls>
  <a-text position="0 0 -1" value="Test" color="#fff"></a-text>
</a-camera>

But this method does not make the text fixed to the side of the screen so the text will overflow when I change the screen size. 但是此方法不会将文本固定在屏幕的侧面,因此当我更改屏幕大小时,文本将溢出。 Is this even possible? 这有可能吗?

It's generally best practice to add the text to the scene rather than to the camera. 通常,最佳做法是将文本添加到场景而不是摄像机。 This gives the user the option to look in either direction so they can see everything. 这使用户可以选择向任一方向看,以便他们可以看到所有内容。

https://aframe.io/docs/0.8.0/components/camera.html#fixing-entities-to-the-camera https://aframe.io/docs/0.8.0/components/camera.html#fixing-entities-to-the-camera

If you still decide to anchor the text to the camera, the simple solution would be to make sure that the text is at a distance great enough that all of it can be seen comfortably at the smallest screen size (vertical phone orientation, etc.). 如果您仍然决定将文本锚定在摄像机上,则简单的解决方案是确保文本的距离足够大,以便在最小的屏幕尺寸(垂直电话方向等)下都能舒适地看到所有文本。 。

A more complex solution would be to change the size of the <a-text> primitive (or containing entity using the text component) on the window.resize event, and adjusting the text container accordingly. 一个更复杂的解决方案是在window.resize事件上更改<a-text>原语(或使用text组件包含实体)的大小,并相应地调整文本容器。 This can be done by creating a custom component. 这可以通过创建自定义组件来完成。

Here is a rudimentary example: 这是一个简单的示例:

AFRAME.registerComponent('resize-text', {

  init: function() {

    var self = this;

    window.addEventListener('resize', function(e) {

      var height = window.innerHeight;
      var width = window.innerWidth;

      // console.log('resized!', height, width);

      self.el.setAttribute('width', ( width / 100 ));

    });

  }

});

And here is a demo of that code in action: https://codepen.io/dansinni/pen/pVJmQg 这是该代码的演示示例: https : //codepen.io/dansinni/pen/pVJmQg

Resize the window or drag the center divider and you will see that the text size changes. 调整窗口大小或拖动中心分隔线,您将看到文本大小更改。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM