Set Sprite Canvas Material to match the color of a different element

Can someone please guide me on changing the color of the Sprite Canvas Material?

var material = new THREE.SpriteCanvasMaterial( {
color: 0xffffff,

I am trying to make it match the color of another element based on a specific ID or class. However, I'm unsure of how to achieve this as the color system in CSS uses hexadecimal values like #FFFFFF, while here it is represented as 0xffffff.

I have removed my previous question as I have learned more about this topic. Any assistance on this matter would be greatly appreciated. Thank you in advance.

Answer №1

THREE.Color() object comes with the .setStyle ( style ) method, which takes in a

style — color as a CSS-style string
.

Consider having multiple elements with the "clicker" class

  <div class="clicker red"></div>
  <div class="clicker green"></div>
  <div class="clicker blue"></div>
  <div class="clicker white"></div>

each with specific color classes:

.clicker {
  width: 24px;
  height: 24px;
  border-radius: 12px;
}
.red {
  background-color: red;
}
.blue {
  background-color: blue;
}
.green {
  background-color: green;
}
.white {
  background-color: white;
}

To make them react to clicks, add event listeners like this:

var clickers = document.getElementsByClassName("clicker");
for(var i = 0; i < clickers.length; i++){
    var clicker = clickers[i];
    clicker.addEventListener("click", setColor);
}

where setColor is the function called when an element is clicked:

function setColor(e){
  var target = (e.target) ? e.target : e.srcElement; // retrieve the clicked element
  var css = window.getComputedStyle(target); // get computed style of the element
  var color = css.backgroundColor; // get the element's background color
  spriteMaterial.color.setStyle(color); // apply this color to the material of sprites using the "setStyle" method
}

Check out this jsfiddle example (click the colorful circles to change the color of the cube of sprites)

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

Eliminating the GWT Window Padding in LibGDX

Explanation: I am currently in the process of deploying a LibGDX GWT application to the browser. Challenge: In the image below, you can see the top left portion of my GWT application showcased by the large black rectangle. The issue here is the unwanted ...

Sending Data from Clicked Button to Another Component as a Prop

I am struggling to figure out how to pass a value that is set inside a button to a child component. Essentially, I want the value of the clicked button that displays a percentage to be passed as a prop value. This prop value should update depending on whic ...

How can we prevent the modal from extending beyond the boundaries of the phone screen when zoomed in

I am currently developing a web application that features a large content page, specifically a map which should display detailed information upon zooming in similar to Google Maps. The interactive elements on my map are clickable, triggering a modal popup ...

Ways to determine if the backbone.js model has been emptied

I often find myself wondering how to determine if a model has been cleared or is empty. For example, if I set the model with: model.set({name:'this is a test', id:1}); and then clear it with: model.clear(); ...

Using Bootstrap to horizontally align cards in a single row

When rendering the 4 items in a row using a for loop, I encounter an issue where the first line renders 4 items correctly but subsequent items are rendered on separate lines. code <div class="card-group"> {% for item in wt %} <di ...

Autodesk Forge Viewer experiences unexpected behavior when using a custom point shader and an orthographic camera

After reading a blog post on using PointCloud for fast rendering of sprites, I implemented it in my project. Everything was working fine with the perspective camera, but when I switched to the orthographic camera using viewer.navigation.toOrthographic(), t ...

Can you provide instructions for setting a projection matrix in Three.js that accomplishes this specific type of mapping?

Can you provide guidance on creating a projection matrix in Three.js that follows a specific rule for mapping 3D points to screen coordinates? screen_x = x - z*0.5 screen_y = y - z*0.5 depth = z ...

Develop an exclusive "click-once" button for a webpage

Can anyone assist me in creating a "one-time clickable" button to launch my website? I've searched through numerous threads on Stack Overflow but haven't found a solution yet. ...

Mastering Protractor's end-to-end control flow and managing time outs

When testing an angular app using protractor, I encountered a strange issue recently. Every now and then, or since a recent update, protractor seems to stall or slow down significantly. After investigating the problem, I discovered that a simple someEleme ...

Developing custom functions in ejs for individual posts

In my blog, I have a feed where all users' posts are displayed dynamically using ejs. There is a comment section that remains hidden until the user clicks the comments button. Below is a snippet of my ejs file: <% posts.forEach(post => { %> ...

What is the best method for choosing the final element with a specific class in each row?

Within each row, there are various cells that have the class .meow. How can I target the last .meow element in each row? The provided code currently selects all existing .meows... $("tr").each(function(){ $(".meow").css("border", "3px solid red"); ...

Transfer an object to $state.go

I'm having trouble solving this issue. Here's the state I am working with: var myState:ng.ui.IState = <ng.ui.IState> { url: '/new/{order.orderNumber}', controller: 'OrderController', controll ...

In a set of strings, locate the two shortest ones, remove them, and then add them back to the end repeatedly until there is only one string

I've been struggling with a coding challenge for a couple of hours now and could really use some help. Here are the detailed instructions: Take an array of strings and create a single string by following these steps: Repeat the following steps as lo ...

What causes the lack of dithering effect when using "dithering" on a three.js material?

My object is equipped with a basic material that looks like this: THREE.MeshPhysicalMaterial({ roughness:1, color: 0xffffff, dithering:true }) In addition, I have a simple directional light. Initially, I assumed that the " ...

Having trouble with expressJs router.post() not functioning properly with multiple middleware, resulting in an [object Undefined] error

I have been working on developing a REST API for my users, utilizing express-validator for validation before adding the user to the database. However, I encountered an issue while chaining my middleware in the router.py file which resulted in the error Err ...

JavaScript Click Event Not Functioning

I am attempting to create an interactive feature where clicking on any image will either reveal a clear version of the picture if it is blurred, or blur the picture if it is clear. The current issue I am facing is that when I click on a blurred image, it ...

Issue with Chrome related to svg getScreenCTM function

Check out the jsFiddle demo I am attempting to utilize the getScreenCTM function to retrieve the mouse position based on SVG image coordinates. It seems to work in IE and Firefox, but not in Chrome. When I define the attributes width and height in the SV ...

Leveraging the power of Promise creation

I am facing an issue where I am trying to utilize the getPromise function with various URLs in order to obtain different promises, but encountering undefined values in the success function of the second promise. var http=require('http'); var URL ...

Understanding the application of JSON data can be simplified by following these

I am looking to manipulate dates by either adding or subtracting them, but I am unsure of how to extract the dates from the other data present. var fetch = require('node-fetch'); fetch('https://api.nasa.gov/planetary/earth/assets?lon=100.7 ...

CSS or Coding? Learn how to display items side by side instead of on top of each other

Hey there! I manage a music website that is currently in beta. I'm running into an issue with the way the music results are appearing in the bottom player - they're all stacked on top of each other instead of being listed side by side. I've ...