[英]Is there a way to listen to the `onload` events of `src` attributes? (A-frame)
Is there a way to listen to the onload
events of src
attributes? 有没有办法监听
src
属性的onload
事件? I can't use <a-assets>
because I'm using an <a-sky>
because <a-assets>
don't seem to work with <a-sky>
. 我无法使用
<a-assets>
因为我正在使用<a-sky>
因为<a-assets>
似乎无法与<a-sky>
。 And tried loading the images of <a-assets>
and <a-sky>
simultanously, but one image loads first and then the other, which means that the loading time is doubled. 并尝试同时加载
<a-assets>
和<a-sky>
图像,但是先加载一个图像,然后再加载另一个,这意味着加载时间增加了一倍。
What's the proper way of listening to the src
attribute? 侦听
src
属性的正确方法是什么? So I can perform an action when the image in it has loaded? 因此,当其中的图像已加载时,我可以执行一个操作?
(I want to avoid having an blank scene while the image on the src
of the <a-sky>
is loading.) (我要避免在加载
<a-sky>
的src
上的图像时出现空白场景。)
Like Don said, use the crossorigin
flag on the asset to get it working with your <a-sky>
. 就像Don所说的
crossorigin
,在资产上使用crossorigin
标志使它与您的<a-sky>
。
Once you have that, you can listen to img.onload
/ loaded
like you would with a normal DOM event. 一旦有了它,就可以像正常的DOM事件一样监听
img.onload
/ loaded
。 Below I wrap some code inside an A-Frame component that automatically hooks into the scene: 下面,我在A-Frame组件中包装了一些代码,该组件会自动挂接到场景中:
<script>
// Do something on asset load.
AFRAME.registerComponent('do-on-asset-load', {
schema: {
src: {type: 'selector'}
},
init: function () {
var el = this.el;
var assetEl = this.data.src;
assetEl.addEventListener('load', function () {
// Do something with your element.
});
}
});
</script>
<a-scene>
<a-assets>
<img id="my-asset" src="https://..." crossorigin="anonymous">
</a-assets>
<a-sky src="#my-asset" do-on-asset-load="#my-asset"></a-sky>
</a-scene>
If you weren't using assets, you'd have to reach into the material to grab the internally-created image: 如果您不使用资产,则必须深入素材以获取内部创建的图像:
document.querySelector('a-sky').components.material.material.map.image
I can't find a load
event equivalent that works here, but <a-assets/>
is supposed to work in this case. 我找不到在这里可以使用的等效
load
事件,但是在这种情况下<a-assets/>
应该可以使用。 If the shorthand <a-sky/>
doesn't work with assets, you can use the longer form: 如果速记
<a-sky/>
不适用于资产,则可以使用较长的形式:
<a-scene>
<a-assets>
<img id="my-asset" src="https://..." crossorigin="anonymous">
</a-assets>
<a-entity geometry="primitive: sphere;
radius: 5000;"
material="src: #asset-id;
side: back;">
</a-entity>
</a-scene>
Note: I don't know why
crossorigin="anonymous"
is necessary.注意:我不知道为什么需要
crossorigin="anonymous"
。 It probably shouldn't be, but it is as of 08/07/16.可能不应该这样,但截至16年8月7日。
Consider this: *"If the additional-layers of 'A-Frame or what-have-you' did not exist, could you attach a DOM event-handler to "an attribute of" something?" 考虑一下:*“如果不存在“ A框架或您拥有什么”的附加层,您可以将DOM事件处理程序附加到“某物的属性 ”吗?”
The answer would be "No": "DOM objects" generate and receive events, but their attributes do not. 答案将是“否”: “ DOM 对象”会生成和接收事件,但它们的属性却不会。
Therefore, 'A-Frame or what-have-you,' being necessarily built on top of these things and therefore wholly dependent upon it, by-definition "can do no more (or less)." 因此,“A-Frame或什么具备的,你,”正在必然建立在这些东西上面 ,因此完全依赖于它,通过定义“可以做没有更多的(或更少)。”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.