What is the mechanism behind positioning a canvas image using position.set center?

Hi everyone, I recently came across this interesting example using three.js HERE. While exploring the example, I stumbled upon a function that caught my attention:

// This function is responsible for positioning the 5 static red dots 
function updateHUDSprites () {

    var width = window.innerWidth / 2;
    var height = window.innerHeight / 2;

    var material = spriteTL.material;

    var imageWidth = material.map.image.width / 2;
    var imageHeight = material.map.image.height / 2;

    // The following lines position the 5 static red dots 
    spriteTL.position.set( - width + imageWidth,   height - imageHeight, 1 ); // top left
    spriteTR.position.set(   width - imageWidth,   height - imageHeight, 1 ); // top right
    spriteBL.position.set( - width + imageWidth, - height + imageHeight, 1 ); // bottom left
    spriteBR.position.set(   width - imageWidth, - height + imageHeight, 1 ); // bottom right
    spriteC.position.set( 0, 0, 1 ); // center

}

This function essentially aligns all 5 PNG images. Although I have experimented with the code extensively, I am still puzzled by how the centering process works. Take, for instance, the following line:

spriteC.position.set( 0, 0, 1 ); // center

After playing around with the example, I realized that the above code actually centers the image. But I'm unable to comprehend the values passed to the position.set() function. If someone could explain how spriteC.position.set( 0, 0, 1 ); achieves centering of the image, I would greatly appreciate it.

Answer №1

The positioning of the camera will determine the outcome

When rendering sprites, the orthographic camera is utilized with the code

renderer.render( sceneOrtho, cameraOrtho );
. This camera is positioned at coordinates (0,0,10), facing towards the origin at (0,0,0)

Objects located at the origin or aligned along the z-axis (such as our sprite with coordinates (0,0,1)) will appear centered. Moving objects to the left requires a negative value for the x coordinate, while positive values shift them to the right. Similar adjustments can be made with the y coordinate as well.

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

Exporting JSON data as an Excel file in AngularJS, including the option to customize the fonts used for the

Currently, I am working on a project where I need to convert JSON data to an Excel file using JavaScript in combination with AngularJS. So far, I have successfully converted the JSON data to CSV format. However, I faced issues with maintaining the font s ...

Guidelines on how to retrieve information from an AJAX request in PHP

I am in the process of developing an application that involves calling a JS function in PHP like this: $data = "<script>JSFunction();</script>"; The JSFunction is defined as follows: function JSFunction(){ $.ajax({ type: 'POST' ...

The WHATWG URL API allows creation of a new URL using the new URL

I've been experimenting with node and attempting to create an instance of the URL class to utilize its useful properties. Here's what I tried: const { URL } = require('url'); (...) http.createServer((request,response) => { let u ...

What is the best method to remove the identifier from checkboxes and buttons and iterate using a for loop?

Is there a way to eliminate the need for individual IDs on buttons and checkboxes? I'm looking to streamline my code and remove any duplicates. I attempted to target the checkboxes using const checkbox = document.querySelectorAll('input[type="che ...

Utilizing AngularJS and Node.js to update MongoDB

Looking for a solution to update my MongoDB using Node.js and AngularJS within the front-end of my application. The code I currently have is causing an error stating `TypeError: Cannot read property 'put' of undefined. Here is my AngularJS contr ...

Accessing a JavaScript object outside of its containing JavaScript file within a Dojo

I am working on a Dojo application that includes a Dgrid. My objective is to send the javascript containing the dgrid data to the server. However, I am encountering difficulties in accessing the javascript object beyond the file where it is defined. The a ...

Using the `useNavigate` function from React Router Dom may trigger a complete reload or re-render of the entire application, resulting

Whenever I attempt to programmatically navigate forward or back using a button click in React Router Dom, the useNavigate() function seems to clear all state, redux, and context and revert everything to its initial state. For example: <IconButton onCli ...

Issue: The specific module is unable to be located, specifically on the Heroku platform

While my application performs well locally and on a Travis CI build server, it encounters issues when deployed on Heroku. The error message Error: Cannot find module is displayed, leading to app crashes. Here are some details about the npm module: It r ...

Unique creation: Bespoke Bootstrap-Vue selection box module

Struggling to create dynamic form components, particularly with checkboxes <template v-if="type === 'switch'"> <b-form-checkbox switch size="lg" :name=" ...

Is it possible to remove the address bar from appearing on a browser when a page loads?

I am in the process of developing a customer wifi landing page, and although we have made progress with ensuring it is the first page that loads, I want to take it a step further. My goal is to require customers to agree to our usage policy before gaining ...

Tips for optimizing mobile performance by loading .obj models into objects on three.js

How can I properly load .obj models with objLoader and MTLLoader for my three.js mini game? I am facing an issue where the game loads fine on computers but fails to load on mobile browsers. Specifically, when accessed on a phone browser, the game attempts ...

Interactive form fields and showcasing data

Hi there, I'm not very familiar with jQuery but I need some help with the following code: jQuery(function($) { $('#precoInserido').bind('keydown keyup keypress', function() { $('#precoVer').html(this.value || "??") ...

HTML5 Boilerplate and optimizing the critical rendering path by deferring scripts and styles

Previously, I constructed my website layouts using the HTML5 Boilerplate method: incorporating styles and Modernizr in the head section, jQuery (either from Google CDN or as a hosted file) along with scripts just before the closing body tag. This is an exa ...

Executing a Google Script will result in a newly inserted column automatically adopting the

My coding issue involves a simple scenario where cell A1 contains a date, for example 06/06/2017. What I aim to do is add three columns and apply formulas to them. I insert columns using the following code: ss.insertColumns(1,3); Subsequently, I set for ...

Tips on managing ASP .NET API's HttpResponseMessage for file downloads

I came across a solution on how to download a file from an asp.net API at this link: As a result, I created an API handler with the following code: public HttpResponseMessage Post([FromBody]dynamic result) { var localFilePath = graph ...

What is the best way to incorporate an immediately invoked function expression (IIFE) within the return statement of a React

I currently have a modal that pops up when the user clicks a button on my page and it's functioning perfectly: render() { return ( <div> <section> <button onClick={() => this.refs.simpleDialog.show()}> ...

The getTotalLength() method in SVG may not provide an accurate size measurement when dealing with non-scaling-stroke. To obtain the correct scale of

In my current project, I am utilizing JavaScript to determine the length of a path and apply half of that length to the stroke-DashArray. However, I have encountered an issue as I am using vector-effect="non-scaling-stroke" in order to maintain a consisten ...

Dynamically include a new prop to the final element within an array of React components

I have been searching everywhere for a solution to this problem, but I can't seem to find one as I am unable to edit the props object. Here's what we are currently doing. We have a menu with subsections and we achieve this by: const firstSectio ...

Utilizing the openweathermap weather widget on a Leaflet map within the R programming

I am attempting to incorporate custom weather tiles onto a leaflet map within a Shiny application by utilizing the leaflet-openweathermap JavaScript library found here. As someone who is not well-versed in JavaScript, I am encountering difficulties with re ...

I keep getting redirected to a blank page by JS

I've created a JavaScript script that smoothly fades in the page when a user enters it and fades out when they click a link to another page. The script is working as intended, but I'm facing an issue with anchor links on the page. Whenever I clic ...