Display the sprite and PlaneGeometry image from three.js in full size regardless of their dimensions

My goal is to display the image in its entirety regardless of size.

var src1 = "https://i.imgur.com/C9lWPeL.jpg",
  src2 = "https://i.imgur.com/a9zyCRt.jpg";

const src = src1;

var scene = new THREE.Scene();
var loader = new THREE.TextureLoader();
loader.load(
  src,
  function(texture) {
    var spriteMaterial = new THREE.SpriteMaterial({
      map: texture,
      color: 0xffffff
    });
    var imageWidth = spriteMaterial.map.image.width;
    var imageHeight = spriteMaterial.map.image.height;
    var camera = new THREE.PerspectiveCamera(80, imageWidth / imageHeight, 1, 1000);
    camera.position.z = 500;
    var sprite = new THREE.Sprite(spriteMaterial);
    sprite.scale.set(1 * imageWidth, 1 * imageHeight, 1.0);
    sprite.position.set(0, 0, 0);
    scene.add(sprite);

    var geometry = new THREE.PlaneBufferGeometry(700, 700 * imageHeight / imageWidth, 0);
    var material = new THREE.MeshBasicMaterial({
      map: texture,
      color: 0xffffff
    });
    var plane = new THREE.Mesh(geometry, material);
    plane.position.set(0, 0, 0);
    //scene.add( plane );

    var renderer = new THREE.WebGLRenderer();
    renderer.setSize(700, 700 * imageHeight / imageWidth);
    renderer.render(scene, camera);
    container.appendChild(renderer.domElement);
  },
);
<script src="https://threejs.org/build/three.js"></script>
<div id=container></div>

Answer №1

Opting for the sprite and utilizing an OrthographicCamera is the recommended approach.

When it comes to PlaneBufferGeometry, determining the correct distance between the camera and the near plane can be achieved as follows:

var camera = new THREE.PerspectiveCamera(45, imageWidth / imageHeight, 0.001, 1000);
camera.position.z = halfHeight / Math.tan(Math.PI / 8);

This will effectively resolve the issue at hand.

In case plane.position.z != 0, you should incorporate it into the camera position like so:

camera.position.z += plane.position.z

var src1 = "https://i.imgur.com/C9lWPeL.jpg",
  src2 = "https://i.imgur.com/a9zyCRt.jpg";

const src = src1;

var scene = new THREE.Scene();
var loader = new THREE.TextureLoader();
loader.load(
  src,
  function(texture) {
    var spriteMaterial = new THREE.SpriteMaterial({
      map: texture,
      color: 0xffffff
    });
    var imageWidth = spriteMaterial.map.image.width;
    var imageHeight = spriteMaterial.map.image.height;
    var width = 700,
      height = width * imageHeight / imageWidth,
      halfHeight = height / 2;


    /*var camera = new THREE.OrthographicCamera( imageWidth / - 2, imageWidth / 2, imageHeight / 2, imageHeight / - 2, 0, 1000 );
    var sprite = new THREE.Sprite( spriteMaterial );
    sprite.scale.set(1 * imageWidth, 1 * imageHeight, 1.0);
    sprite.position.set(0, 0, 0);
    scene.add( sprite );*/

    var camera = new THREE.PerspectiveCamera(45, imageWidth / imageHeight, 0.001, 1000);
    camera.position.z = halfHeight / Math.tan(Math.PI / 8);
    var geometry = new THREE.PlaneBufferGeometry( width, height, 0);
    var material = new THREE.MeshBasicMaterial({
      map: texture,
      color: 0xffffff
    });
    var plane = new THREE.Mesh(geometry, material);
    plane.position.set(0, 0, 0);
    scene.add(plane);

    var renderer = new THREE.WebGLRenderer();
    renderer.setSize(width, height);
    renderer.render(scene, camera);
    container.appendChild(renderer.domElement);
  },
);
<script src="https://threejs.org/build/three.js"></script>
<div id=container></div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unable to display canvas background image upon webpage loading

Currently working on a JavaScript project to create a captcha display on a canvas. The issue I'm facing is that the background image does not load when the page initially opens. However, upon hitting the refresh button, it functions as intended. Here& ...

What is the best way to postpone the angular animation until after the model has been bound

I am currently working on a project using AngularJS where I am implementing animations for state transitions. To help with this process, I have been referring to the angular wiki and found this Plunker example: http://plnkr.co/edit/NsZhDL?p=preview My is ...

Dealing with API Troubles: Resolving 'AxiosError: Request failed with status code 401' Issue in ReactJS

I encountered the following error: AxiosError: Request failed with status code 401 While attempting to log in to my react app, I researched similar solutions and discovered that some answers suggested the issue could be related to missing headers in Axi ...

Error: No schema found for the specified "User" model

Attempting to establish a connection with the MongoDB database using Mongoose and defining the model resulted in the following error message: MissingSchemaError: Schema hasn't been registered for model "User" Several approaches were taken to address ...

Tips on incorporating a color picker in HTML using jQuery

Looking to implement an HTML color picker with the help of JQuery. Wondering if there is a simpler method to do this without relying on external JQuery libraries? Experimented with <input type="color"> but encountered issues that prevented it from ...

Creating a Dynamic Input Validation Range with JQuery

Greetings and thank you for taking the time to review this! :-) The form validation is functioning correctly with required fields, but I am facing a challenge with setting up numeric range validation dynamically for an autocomplete feature. The JQuery val ...

An issue has occurred: Class cannot be defined through reflection

I'm in the process of developing an App using the resources on this specific webpage My attempt at running e2e-tests was unsuccessful. I encountered an issue with starting the selenium server. How can I resolve this? NPM Version: 5.6.0 Yarn Versio ...

An in-depth guide on implementing debounce functionality for `keyup` events in Vue.js

I am attempting to detect when the user begins typing and stops typing using the debounce function. I experimented with Lodash and Underscore.js. On my textArea v-on:keyup="handler($event)" handler: function(e) { ...

Clicking does not result in a change of the view

While experimenting with this code snippet : http://jsfiddle.net/9fR23/187/ Something seems off as the table elements are not being hidden when clicking on the "Flip!" div. The elements are supposed to be hidden based on the state change determined by th ...

Watching for changes to an element's visibility within the viewport and automatically scrolling it

I'm having trouble making the input scroll to .here when its value matches "1". Even though I tried using a button with a handle-click function and it worked. Please lend me a hand with this issue. <template> <button @click="scrollToV ...

How to generate and modify a .txt file using JavaScript

I am currently working on a website that keeps track of the individuals who visit it For this purpose, I have designed an HTML form page where users can input their names However, I am facing difficulty in figuring out how to store and maintain this reco ...

Refine a Collection of Objects by Applying Multiple Criteria and Searching for Partial Matches

Looking for a solution to filter arrays of objects with partial matches? Most filters available only provide exact matches, but I need something more flexible. Whether it's uppercase or lowercase, partial matches are essential. The challenge is findin ...

The scripts within the body tag are failing to load

After trying to embed angular into the body tag, I noticed that nothing is loading up. Upon inspecting the resources panel, I found that only files from the head are present. Moving all the scripts to the head section resolves the issue and everything load ...

A guide on verifying a DNS server switch using Node.js

I require assistance with a particular topic. My goal is to develop a script that can detect when a DNS is changed due to a user switching network providers. Upon attempting dns.getServers() in the node cli, I noticed that it only returned the DNS of the ...

Disable the ability for new windows, such as popups, submit buttons, and forms with the target set to blank, to reference

Encountering an issue, I stumbled upon some solutions here and on various other websites that I would like to share with you. The Problem: The window.opener functions of child pages stopped working after some security updates in modern browsers around 202 ...

Modify the font style of the recommended actions buttons within the webchat interface

I am attempting to modify the font of the "suggested actions" button similar to how it is demonstrated in this reference for changing the font of the bubble text: https://github.com/Microsoft/BotFramework-WebChat/blob/master/SAMPLES.md In the provided exa ...

Firebase Hosting integrated with Cloud Functions

Struggling with deploying my functions and hosting. While I have managed to get them working separately on different branches, integrating both hosting and cloud functions is proving to be a challenge. It seems like my cloud function is not deploying succ ...

Create your own AngularJS directive for displaying or hiding elements using ng-show/ng

Caution: Angular rookie in action. I'm attempting to craft a personalized widget that initially displays a "Reply" link, and upon clicking it, should hide the link and reveal a textarea. Here's my current progress, but unfortunately, it's n ...

Choose a new image by confirming your selection with a dialog box

I have a collection of images displayed in a grid, and I want to be able to select one of them to update the value with the chosen image. Before proceeding with the update, I need a confirmation prompt to ensure the user wants to proceed or cancel the acti ...

Conceal list items by clicking elsewhere on the page

Currently, I am facing an issue with my search user functionality. Whenever a user clicks anywhere else on the page, the list of results does not disappear unless the user manually deletes all the words they have typed. This behavior is not ideal and despi ...