javascript - 生成 A-Frame 屏幕截图时出现 JavaScript 错误
问题描述
我正在尝试从 A-Frame 生成屏幕截图,但不断收到此错误:
TypeError: document.querySelector(...).components.screenshot is undefined
这是测试代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://aframe.io/releases/0.8.0/aframe.min.js"></script>
</head>
<body>
<a-scene screenshot="width: 640; height: 320">
<a-entity id="box" geometry="primitive: box; width: 1; depth: 1; height: 1" position="-1 0.5 -3" rotation="0 45 0" material="color: #4CC3D9"></a-entity>
<a-entity id="sphere" geometry="primitive: sphere; radius: 1.25" material="color: #EF2D5E" position="0 1.25 -5"></a-entity>
<a-entity id="cylinder" geometry="primitive: cylinder; radius: 0.5; height: 1.5" position="1 0.75 -3" material="color: #FFC65D"></a-entity>
<a-entity id="plane" position="0 0 -4" rotation="-90 0 0" geometry="primitive: plane; width: 4; height: 4" material="color: #7BC8A4"></a-entity>
<a-entity id="sky" geometry="primitive: sphere; radius: 100" material="color: #ECECEC; shader: flat; side: back"></a-entity>
</a-scene>
<script>
document.querySelector('a-scene').components.screenshot.capture('perspective')
</script>
</body>
</html>
JavaScript 这行来自官方文档: https ://aframe.io/docs/0.8.0/components/screenshot.html
这是 A-Frame 源: https ://github.com/aframevr/aframe/blob/master/src/components/scene/screenshot.js
解决方案
当您尝试访问其成员函数时,屏幕截图组件尚未初始化。
尝试创建一个包含您的代码的新组件:
AFRAME.registerComponent("foo",
init: function() {
document.querySelector('a-scene').components.screenshot.capture('perspective')
}
})
HTML
<a-scene screenshot foo>
就像我在这里做的
您还可以侦听a-scene
sloaded
事件,或者根据需要使用 setTimeout()。
推荐阅读
- python - “TemplateDoesNotExist at / home1.html”而 {%load static%} 在中创建 'UNWANTED TOKEN ' 错误
- sql - 选择没有分组的最大值
- c# - 从 powershell 获取 Acl 并从 c# 获取 GetAccessControl 为在 NFS 驱动器中创建的文件夹抛出错误代码 50
- javascript - 点击 Javascript 放大图像(无框架)
- python - 当我在 cmd 中输入“python --version”时,什么也没有发生
- apache-camel - Apache Camel Rest - v3.2.0 - 多部分文件上传问题
- flutter - 如何将 ListView 放在带有填充的脚手架下?
- javascript - 编译 ejs 时 C:\Users\Dominic\Desktop\html 代码\portfolio\passport-tutorial\views\partials\messages.ejs 中出现意外的令牌“捕获”
- python - 我不断收到错误之一,需要以下参数:-m --MODEL,我该如何解决?
- google-coral - 为珊瑚开发板供电后,控制台上没有显示任何内容