Jump to content

Search In
  • More options...
Find results that contain...
Find results in...

Applying a fill to an element based off of a D3 colour scale

Go to solution Solved by OSUblake,

Recommended Posts


I've got an SVG map made out of hexagons / polygons and in each polygon, there is a data-attribute which I use to give each polygon's it's corresponding colour using the D3 Colour Scales method. 


    for (i = 0; i < poly.length; i++) {

        var data = parseInt(poly[i].getAttribute('data-canopy'));
        poly[i].style.fill = grayscaleCol(data);

So it goes through an array of all the polygons, gets the data attribute which is called 'canopy' and then provides a fill based on the data using the colour scale 'grayscaleCol'.


This is what the colour scale looks like: 

var grayscaleCol = d3.scaleLinear().domain([5, 10, 15, 25, 50, 75]).range(['#727272', '#686868', '#515151', '#3A3A3A', '#252525', '#131313']);


I'm still very much a newb when it comes to GSAP but would it be possible to have our gsap target be an array of elements and then animate the fill of each element using the 'canopy' data attribute (which is a number and is different for each polygon) and the colour scale? Could I use 'this.getAttribute' within gsap or anything similar?



Link to comment
Share on other sites

  • Solution

Hi Josh, 


If I understand your question correctly, you should be able to use a function to grab the color like this.


gsap.to(poly, {
  fill: (i, el) => grayscaleCol(+el.getAttribute("data-canopy"))


  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.