Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Placing the images in the right position #25

Open
ahujasid opened this issue Jan 31, 2024 · 0 comments
Open

Placing the images in the right position #25

ahujasid opened this issue Jan 31, 2024 · 0 comments

Comments

@ahujasid
Copy link

I was following the Codrops tutorial for this and managed to get it to work, but I can't figure out how to place the images in the threejs canvas in the same place as the existing image in the DOM. Any tips on how to do that?

This is the code right now:

function createMeshForImage(img, texture) {
    let bounds = img.getBoundingClientRect();
    let aspect = window.innerWidth / window.innerHeight;
    let width = bounds.width / window.innerWidth * aspect;
    let height = bounds.height / window.innerHeight * aspect;
    let geometry = new THREE.PlaneGeometry(width, height);
    let material = new THREE.MeshBasicMaterial({ map: texture });
    let mesh = new THREE.Mesh(geometry, material);
    mesh.userData.bounds = img.getBoundingClientRect();

    let x = (bounds.left + bounds.width / 2 - window.innerWidth / 2) / window.innerWidth * aspect;
    let y = -(bounds.top + bounds.height / 2 - window.innerHeight / 2) / window.innerHeight * aspect;
    mesh.position.set(x, y, 0);

    return mesh;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant