angular - getBoundingClientRect 不是 Angular 11 上的函数 - Three.js
问题描述
我尝试了几次来解决这个问题,但我一直无法找到解决方案,我希望有人能帮助我。我正在尝试在我的网页上提供有关图像大小的信息,以便稍后实现滚动,这是我的代码:
ngAfterViewInit() {
let images = [{...document.querySelectorAll('img')}];
this.addImages(images);
}
addImages(images) {
this.imageStore = images.map(img => {
let bounds = img.getBoundingClientRect();
console.log(bounds);
let geometry = new THREE.PlaneBufferGeometry(bounds.width, bounds.height, 1, 1);
let material = new THREE.MeshBasicMaterial({ color: 0xff0000 })
let mesh = new THREE.Mesh(geometry, material)
this.scene.add(mesh);
return {
img: img,
mesh: mesh,
top: bounds.top,
left: bounds.left,
width: bounds.width,
height: bounds.height
}
})
}
该代码应该返回我的 HTML 页面上图像的大小,但我得到了错误
解决方案
我认为问题在于您需要等待图像加载完毕。
如果您的图像在数组中(有些像)
images=["https://picsum.photos/200/300?random=1",
"https://picsum.photos/200/300?random=2"]
您在 *ngFor 中显示它,使用事件(load)
<ng-container *ngFor="let img of images">
<img #mimg [src]="img" (load)="pushData(mimg)">
</ng-container>
array:any[]=[]
pushData(el:any)
{
this.array.push(el.getBoundingClientRect())
if (this.array.length==this.images.length)
{
console.log("all images loaded")
..do something..
}
}
更新因为我们不知道加载图像的顺序,我们可以强制传递索引
<ng-container *ngFor="let img of images;let i=index">
<img #mimg [src]="img" (load)="pushData(mimg,i)">
</ng-container>
pushData(el:any,index:number)
{
this.array[index]={
...el.getBoundingClientRect(),
img:el
}
if (!this.array.find(x=>!x))
{
console.log("all images loaded")
}
}
推荐阅读
- c# - 如何读取 EPC 和 TID 执行库存(Zebra Xamarin RFID SDK)?
- python - Python pip - 无法更新 pip
- php - Laravel 路由语言环境中间件无法与模型绑定一起使用
- firebase - 如何从 FirebaseAuthException 正确捕获异常
- postgresql - Spring JPA postgresql-错误查询结构与函数结果类型不匹配
- azure - 更新 Azure ML 实时终结点
- javascript - 使用 Intersection Observer 检查元素是否在浏览器视口上完全可见
- python - 聚合无容量网络设计问题的基本运输问题
- sql - 如何在“出租车”和“图书”类别中选择第一个获得 1000 积分的 1000 位顾客?(SQLite)
- android - 如何在 Android 10 上执行警报?