aframe - 文本的 A-Frame 设置属性
问题描述
我正在尝试创建一个具有以下属性的文本实体,这样我就不必在每次使用文本时都指定这些细节
text.setAttribute('color', #303030);
text.setAttribute('width', 2);
text.setAttribute('lineHeight', 60);
text.setAttribute('align', 'center');
text.setAttribute('baseline', 'top');
text.setAttribute('wrapcount', 12);
但我真的不明白“模式”和“AFRAME.registerComponent”是如何工作的。有人可以帮忙吗?
解决方案
您可能对mixins感兴趣,这是创建可重用模板的另一种形式:
<a-scene>
<a-assets>
<!-- define the template -->
<a-mixin id="red" material="color: red"></a-mixin>
</a-assets>
<!-- use the template -->
<a-box mixin="red"></a-box >
</a-scene>
在您的情况下,它可能是text
具有所有常见属性的简单混合:
<script src="https://aframe.io/releases/1.2.0/aframe.min.js"></script>
<script>
AFRAME.registerComponent("text-creator", {
init: function() {
var messages = ["one", "two", "three"]
for (var i = 0; i < 3; i++) {
// create an <a-entity>
var txt = document.createElement('a-entity')
// use the mixin
txt.setAttribute("mixin", "text")
// set the text value
txt.setAttribute("text", "value", messages[i])
txt.setAttribute("position", -0.25 + i + " 2 -2")
this.el.appendChild(txt)
}
}
})
</script>
<a-scene text-creator>
<a-assets>
<a-mixin id="text" text="color: blue; width: 2, lineHeight: 60, align: center, baseline: top, wrapcount: 12"></a-mixin>
</a-assets>
<a-box position="-1 0.5 -3" rotation="0 45 0" color="#4CC3D9"></a-box>
<a-sphere position="0 1.25 -5" radius="1.25" color="#EF2D5E"></a-sphere>
<a-cylinder position="1 0.75 -3" radius="0.5" height="1.5" color="#FFC65D"></a-cylinder>
<a-plane position="0 0 -4" rotation="-90 0 0" width="4" height="4" color="#7BC8A4"></a-plane>
</a-scene>
AFRAME.registerComponent(<name>, <body>)
只是说“我希望在将组件附加到实体<body>
时拥有该功能”的一种形式。<name>
架构只是组件属性的声明。尝试遵循文档并编写您自己的组件,并在其行为异常时随时发布问题。
推荐阅读
- list - 无法将预期类型与实际类型匹配以进行列表构造
- javascript - 如何访问列表中的第一个值?
- c# - DbContext 的 C# 问题:参数 1:无法将“字符串”转换为“Microsoft.EntityFrameworkCore.DbContextOptions”
- javascript - 获取获取请求的浏览器 Accept-Language 标头值
- excel - 如何取消选择excel过滤器中的行
- common-lisp - 关于 lisp 包管理器的初学者问题:quicklisp 是答案还是需要考虑其他事情?
- http-headers - 可以组合多个Headers中间件吗?
- php - 使用文本文件从命令行运行 php
- bash - 为什么我的数组总是只存储第一个索引的数据?
- httpclient - 日志分析 Post call 403 禁止错误