【发布时间】:2020-08-01 01:35:44
【问题描述】:
我有一个容器,我使用 three.js 和网格将图像应用到该容器。
这就是我将网格应用于场景的方式:
this.$els = {
el: el,
image: el.querySelector('.ch__image') <-- size of container image is being applied to
};
this.loader = new THREE.TextureLoader();
this.image = this.loader.load(this.$els.image.dataset.src);
this.sizes = new THREE.Vector2(0, 0);
this.offset = new THREE.Vector2(0, 0);
getSizes() {
const { width, height, top, left } = this.$els.image.getBoundingClientRect();
this.sizes.set(width, height);
this.offset.set(left - window.innerWidth / 2 + width / 2, -top + window.innerHeight / 2 - height / 2)
}
createMesh() {
this.geometry = new THREE.PlaneBufferGeometry(1, 1, 1, 1);
this.material = new THREE.MeshBasicMaterial({
map: this.image
});
this.mesh = new THREE.Mesh(this.geometry, this.material);
this.mesh.position.set(this.offset.x, this.offset.y, 0);
this.mesh.scale.set(this.sizes.x, this.sizes.y, 1);
this.scene.add(this.mesh)
}
目前正在拉伸图像/纹理以适应容器 - 我怎样才能使行为像 object-fit: cover 或 background-size: cover?
【问题讨论】:
标签: javascript css three.js