onload前面加async
时间: 2023-08-18 12:04:09 浏览: 294
在使用`onload`事件时,可以在前面加上`async`关键字来使成为异步函数。这做的目的是让`onload`事件在加载资源时不会阻塞页面的其他操作。以下是一个示例:
```javascript
<script>
window.onload = async function() {
// 异步操作
await someAsyncFunction();
console.log("页面加载完成");
}
</script>
```
通过在`onload`函数前加上`async`关键字,我们可以在其中使用`await`来等待异步操作完成。这样,当页面加载完成后,才会执行后续的代码逻辑。这种方式可以避免阻塞页面加载,提升用户体验。
相关问题
private async parseMaterial(osgStateSet: any) { let material = new THREE.MeshBasicMaterial({ // side: THREE.DoubleSide, }); //THREE.FrontSide 背面 // THREE.BackSide 前面 // THREE.DoubleSide 双面 let osgImage = osgStateSet.TextureAttributeList[0].value.StateAttribute.Image; // let texture = this.parseImage(osgImage); let fileName = osgImage.Name; const isJPEG = fileName.search(/.jpe?g($|?)/i) > 0; const isPNG = fileName.search(/.png($|?)/i) > 0; if (!isPNG && !isJPEG) { return; } let mimeType = isPNG ? 'image/png' : 'image/jpeg'; let imageUri: any = new Blob([osgImage.Data], { type: mimeType }); let base64 = await this.blobToBase64(imageUri); // debugger let imageUrl: any = URL.createObjectURL(imageUri); try { let texture = new THREE.TextureLoader().load(base64, () => { texture.needsUpdate = true; // texture.format = THREE.RGBAFormat; texture.type = THREE.UnsignedShort5551Type; texture.minFilter = THREE.LinearMipmapNearestFilter; texture.magFilter = THREE.LinearMipmapNearestFilter; texture.generateMipmaps = false; // texture.format = THREE.RGBAIntegerFormat; // texture.minFilter = THREE.NearestFilter; // texture.magFilter = THREE.NearestFilter; // texture.minFilter = THREE.LinearMipMapLinearFilter; // texture.magFilter = THREE.LinearFilter; // texture.wrapS = THREE.RepeatWrapping; // texture.wrapT = THREE.RepeatWrapping; // texture.anisotropy = 1; // texture.generateMipmaps = false; // texture.encoding = THREE.sRGBEncoding; }); imageUri = null; osgImage = null; imageUrl = null; if (texture) { material.map = texture; } } catch (e) { console.log('纹理加载出错', e); } osgStateSet = null; return material; } private blobToBase64(blob: any) { return new Promise((resolve, reject) => { const fileReader = new FileReader(); // readAsDataURL fileReader.readAsDataURL(blob); fileReader.onload = (e: any) => { resolve(e.target.result); }; }); }没有显示base64图片的纹理,也没有报错,请找出问题,并给出详细代码
在代码中,你使用了一个Blob对象来加载纹理图片,并将其转换成了base64编码,但是在加载纹理时,却直接使用了base64编码字符串来加载纹理,这个可能是导致问题的原因。正确的做法应该是使用图片的URL来加载纹理,而不是直接使用base64编码字符串。
具体修改代码如下:
```typescript
private async parseMaterial(osgStateSet: any) {
let material = new THREE.MeshBasicMaterial({
// side: THREE.DoubleSide,
});
//THREE.FrontSide 背面
// THREE.BackSide 前面
// THREE.DoubleSide 双面
let osgImage = osgStateSet.TextureAttributeList[0].value.StateAttribute.Image;
// let texture = this.parseImage(osgImage);
let fileName = osgImage.Name;
const isJPEG = fileName.search(/.jpe?g($|?)/i) > 0;
const isPNG = fileName.search(/.png($|?)/i) > 0;
if (!isPNG && !isJPEG) {
return;
}
let mimeType = isPNG ? 'image/png' : 'image/jpeg';
let imageUri: any = new Blob([osgImage.Data], { type: mimeType });
try {
// 使用图片的URL来加载纹理
let texture = await new Promise<THREE.Texture>((resolve) => {
let imageUrl = URL.createObjectURL(imageUri);
new THREE.TextureLoader().load(imageUrl, (texture) => {
// 在加载完成的回调函数中,手动释放URL对象,避免内存泄漏问题
URL.revokeObjectURL(imageUrl);
texture.needsUpdate = true;
// texture.format = THREE.RGBAFormat;
texture.type = THREE.UnsignedShort5551Type;
texture.minFilter = THREE.LinearMipmapNearestFilter;
texture.magFilter = THREE.LinearMipmapNearestFilter;
texture.generateMipmaps = false;
// texture.format = THREE.RGBAIntegerFormat;
// texture.minFilter = THREE.NearestFilter;
// texture.magFilter = THREE.NearestFilter;
// texture.minFilter = THREE.LinearMipMapLinearFilter;
// texture.magFilter = THREE.LinearFilter;
// texture.wrapS = THREE.RepeatWrapping;
// texture.wrapT = THREE.RepeatWrapping;
// texture.anisotropy = 1;
// texture.generateMipmaps = false;
// texture.encoding = THREE.sRGBEncoding;
resolve(texture);
});
});
if (texture) {
material.map = texture;
}
} catch (e) {
console.log('纹理加载出错', e);
}
osgStateSet = null;
return material;
}
private blobToBase64(blob: any) {
return new Promise((resolve, reject) => {
const fileReader = new FileReader();
// readAsDataURL
fileReader.readAsDataURL(blob);
fileReader.onload = (e: any) => {
resolve(e.target.result);
};
});
}
```
其中,使用了一个Promise对象来等待纹理加载完成后返回,同时在加载完成的回调函数中,手动释放URL对象,避免内存泄漏问题。另外,建议在加载纹理时,设置合适的minFilter和magFilter,以获得更好的渲染效果。
阅读全文