Incorporating a dynamic image map with the power of three.js

I am a beginner with HTML image maps and three.js objects. I have successfully resized the image map as the window resizes, but I am struggling to insert additional JavaScript code for optimizing the performance.

Although there are existing threads on this topic, I haven't been able to determine where exactly in my code I should include the provided JavaScript solution that others have found helpful.

The JavaScript snippet I am referring to is designed to adjust the coordinates of the image map based on a scale value, which sounds like it would be beneficial for my project. However, I am unsure about the appropriate placement within my current script.

If you'd like to see my website in action, feel free to visit aurnab.info. It works best on Safari and Firefox browsers.

The following code represents the structure of my webpage, and I am looking to integrate the aforementioned script seamlessly:

<!DOCTYPE html>
<html lang="en">
<head>

<title>aurnab saleh</title>
<meta charset="utf-8>
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>

body {
background:url("obj/me/homefinal.jpg");
background-size:100%;
margin: 0;
width:1280px;
height:800px;
overflow:hidden;
background-repeat: no-repeat;
background-position:fixed;

}

canvas {
position:absolute;
top:0;
left:0;
z-index:-1;
}



</style>
</head>


<body>


<script src="../build/three.min.js"></script>

<script src="js/loaders/DDSLoader.js"></script>
<script src="js/loaders/MTLLoader.js"></script>
<script src="js/loaders/OBJMTLLoader.js"></script>

<script src="js/Detector.js"></script>
<script src="js/libs/stats.min.js"></script>



<img src="obj/me/homefinal.jpg" width="1280" height="800" usemap="#Map" border="0" style="opacity:0"/>
<map name="Map" id="Map">
    <area shape="rect" coords="650,365,713,422" href="http://soundcloud.com/bilderberg-group" target="_blank" alt="soundcloud" />
  <area shape="rect" coords="650,465,711,522" href="http://djgunk.tumblr.com" target="_blank" alt="tumblr" />
  <area shape="rect" coords="648,565,710,619" href="http://vimeo.com/aurnab" target="_blank" alt="vimeo" />
  <area shape="rect" coords="647,665,711,723" href="email.html" target="_blank" alt="gmail" />
</map>


<script>

// Insert mapRebuild function here

var container, stats;

var camera, scene, renderer;

var mouseX = 0, mouseY = 0;

var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;


init();
animate();


function init() {

container = document.createElement( 'div' );
document.body.appendChild( container );

// Additional three.js functions

}

function onWindowResize() {

windowHalfX = window.innerWidth / 3;
windowHalfY = window.innerHeight / 3;

// Adjusting camera settings

}

function onDocumentMouseMove( event ) {

mouseX = ( event.clientX - windowHalfX ) / 10;
mouseY = ( event.clientY - windowHalfY ) / -2;

}

function animate() {

requestAnimationFrame( animate );
render();

}

function render() {

camera.position.x += (- mouseX - camera.position.x ) * .08;
camera.position.y += ( - mouseY - camera.position.y ) * .008;

camera.lookAt( scene.position );

renderer.render( scene, camera );

}

</script>
</body>
</html>

Answer №1

If you're looking to have the image resize with the window, try this approach:

// Assign an ID to the image for JavaScript use
<img id = "yourImageID" src="obj/me/homefinal.jpg" usemap="#Map"  style="opacity:0;width:1280;height:800;border:0;"/>

// Set an ID for all areas (demonstrated with one area in this example)
<area id="area1" shape="rect" coords="650,365,713,422" href="http://soundcloud.com/bilderberg-group" target="_blank" alt="soundcloud" />

// Define variables for the image and area tags in the script
var image = document.getElementById("yourImageID");
var area1 = document.getElementByID("area1");
var coordsArea1 = [];// Create an array to store map coordinates
coordsArea1.push(650); 
coordsArea1.push(365);
coordsArea1.push(713);
coordsArea1.push(422);

var windowWidth = window.innerWidth;
var windowHeight = window.innerHeight;

For the window resize function:

function onWindowResize() {

  var percentX = windowWidth / window.innerWidth;
  var percentY = windowHeight / window.innerHeight;
  var windowWidth = window.innerWidth;
  var windowHeight = window.innerHeight;

  image.style.width = window.innerWidth * percentX;
  image.style.height = window.innerHeight * percentY;

  coordsArea1[0] *= percentX;
  coordsArea1[1] *= percentY;
  coordsArea1[2] *= percentX;
  coordsArea1[3] *= percentY;
  area1.coords = coordsArea1[0] + "," + coordsArea1[1] + "," + coordsArea1[2] + "," + coordsArea1[3];

            windowHalfX = window.innerWidth / 3; // Shouldn't this be window.innerWidth/2?
            windowHalfY = window.innerHeight / 3; // Same as above?

            camera.aspect = window.innerWidth / window.innerHeight;
            camera.updateProjectionMatrix();

            renderer.setSize(window.innerWidth, window.innerHeight);

        }

Note that manually changing width and height can distort the image. Consider using 'width = 1280' and 'height = auto' with some code adjustments.

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

Is there a way to simulate the parameters of a method callback from an external dependency in Nodejs

Imagine a scenario where I have the following structure: lib/modules/module1.js var m2 = require('module2'); module.exports = function(){ return { // ... get: function(cb){ m2.someMethod(params, function(error, ...

What is the best way to attach an event listener to every child node within a div that is stored in an array of divs?

Currently, I am dedicated to learning JS on my own and have decided to hold off on using jQuery until my JavaScript skills have improved. The objective is to attach an event listener for the click event to all divs of a specific class and have all child n ...

XPath allows for the selection of both links and anchors on a webpage

Currently, I am using Screaming Frog and seeking to utilize XPath for a specific task. My objective is to extract all links and anchors that contain a certain class within the main body content, excluding any links found within div.list. I have attempted ...

Tips for sending AngularJS expressions to a controller

I am looking to pass a value from an AngularJS Expression to the controller. Here is the HTML code : <div data-ng-controller="AlbumCtrl"> <div data-ng-repeat="z in songInfo"> <div data-ng-repeat="b in z.album& ...

The NextJS application briefly displays a restricted route component

I need to ensure that all routes containing 'dashboard' in the URL are protected globally. Currently, when I enter '/dashboard', the components display for about a second before redirecting to /login Is there a way to redirect users to ...

What is the best way to isolate a single element within a for loop and exclude all others?

I have implemented a dynamic Ajax call to compare the string entered in the text field (representing a city name) with the "type" value in a JSON array. As I iterate through the elements of the array, I am checking the values associated with the key "type ...

Is it possible to create a sticky element that stays fixed to the window without using JavaScript?

Using position: sticky has been a game-changer for me. It resolves many issues without the need for JavaScript. However, I've encountered a roadblock. I am trying to create a sticky element that is nested inside multiple <div> elements. Since po ...

What is the process for integrating CSS-styled text into CKEditor?

Currently, I am developing plugins that are designed to incorporate various elements like images and tables into the editor. My objective is to be able to view these elements in their final visual form within the editor itself. To achieve this, I plan on a ...

Utilizing Javascript to collapse the initial DIV element discovered with a specific classname

Seeking assistance with this particular Javascript code. The objective is to collapse the next element. Unfortunately, it is not functioning as expected due to an error in closing the first DIV in each block. The script should aim to collapse the initial ...

What are the best ways to keep a django page up to date without the need for manual

Currently, I am in the process of developing a Django website focused on the stock market. Utilizing an API, I am pulling real-time data from the stock market and aiming to update the live price of a stock every 5 seconds according to the information pro ...

What is the best way to refresh a page using Vue 3 Composition API and router?

Once confirmed in Vue 3 Composition API router.push('/IssueList'); When arriving at the IssueList page, I want my issue-incoming and issue-send components to refresh. Although I am redirecting to the page, the IssueList page does not refresh. ...

Centering input boxes within a panel body through the implementation of CSS grid technology

I am facing an issue with implementing a form input box inside a panel body using CSS grid layout. The input box seems to overflow outside the panel body, which is not the desired behavior. Strangely, the code works fine with regular CSS, but I prefer usin ...

Replacing elements with preg_replace in HTML including diacritics

Can someone assist me in using preg_replace for the following scenario: <p style="background:white"> <span style="font-size:10.0pt;font-family:&quot;Trebuchet MS&quot;,&quot;sans-serif&quot;"> Coût de la prestation : 43.19 . ...

How to handle event methods with Vue

Currently, I am facing a challenge in passing an event downward with a bound change listener to a child input component. Although I am utilizing wrapped input components, my goal is to define methods within the parent component. //App.js: <currency-inp ...

Just starting out with CSS and coding. Setting up radio buttons and divs for a simple beginner's gallery

One challenge that perplexes me is how to reposition the concealed divs below the radio buttons with labels. Check out my HTML here View my CSS code here /*color palette: abls [lightest to darkest] #eeeeee #eaffc4 #b6c399 ...

What is the reason for using a string as the index of an array in the following code?

var arrayOfNumbers = [1, 2, 3, 4, 5, 6, 78]; for(var index in arrayOfNumbers){ console.log(index+1); } The result produced by the given code snippet is as follows: 01 11 21 31 41 51 61 What is the reason behind JavaScript treating these ...

Combining Arrays Equally and Alternating using JavaScript and Google Apps Script

I am attempting to evenly and alternately merge multiple arrays in JavaScript/Google Apps Script. I have about 5 or 6 arrays that I need to merge. I have tried two different methods, but unfortunately, neither has worked for me. I don't have much expe ...

Tips for utilizing pre-installed validation regulations in VeeValidate 4?

Currently transitioning from Vue 2 to Vue 3, I am interested in utilizing VeeValidate 4 for my validations. However, it seems that the documentation does not provide any information on using built-in rules such as min, max, alpha_spaces, etc. Do I need to ...

Having trouble retrieving a value from a .JSON file (likely related to a path issue)

My React component is connected to an API that returns data: class Item extends Component { constructor(props) { super(props); this.state = { output: {} } } componentDidMount() { fetch('http://localhost:3005/products/157963') ...

Fixed-bottom footer in Bootstrap 5 that overlays content

Having trouble creating a fixed footer using the fixed-bottom class in react & bootstrap. The issue is that it's covering some content and I've tried various solutions without success. Any suggestions on how to fix this? (Code and Demo provided b ...