Font transparency is not displaying properly

I am attempting to adjust the opacity of text displayed on a d3 pie chart, but I'm facing issues where only the fill is rendering correctly and not the opacity. Below is the code snippet where I am trying to apply opacity:

  var txts = svg.data([json]).selectAll(".theTxts")
    .data(partition.nodes)
    .enter()
    .append("text")
    .attr("class", "theTxts")
    .attr("dx", 10) //Adjust the text position from the arc's starting angle
    .attr("dy", 18) //Move the text down
    .append("textPath")
    .attr("xlink:href", function(d, i) {
      return "#theArc_" + i;
    })
    .text(function(d) {
      return d.name;
    })
    .style("opacity", 0.0001)
    .style("fill", "#000");

  txts.transition()
    .duration(1400)
    .style("opacity", 1)
    .style("fill", "#000"); 

You can see a working example of the visual here:

https://plnkr.co/edit/VU85z7zz50PN4geTbf4F?p=preview

Answer №1

In accordance with the SVG specification, the opacity property

Applies to: container elements (except ‘mask’) and graphics elements

Interestingly, the <textPath> does not fall under either category. It is classified as a text content element and a text content child element, therefore making it ineligible for styling using the opacity attribute.

To style these elements, you must focus on the surrounding <text> instead. The graphics element and text content element nature of the <text> allows it to be styled using the opacity property.

In your code, ensure you define the initial opacity on the <text>s that you reference as txts. Subsequently, append the <textPath>s to these <text>s based on this reference. There is no need to create a separate reference for the <textPath> elements since they are not utilized elsewhere. Transition the original <text>s to the final opacity value using this same reference.

var txts = svg.data([json]).selectAll(".theTxts")
  .data(partition.nodes)
  .enter()
  .append("text")
    .attr("class", "theTxts")
    .attr("dx", 10) 
    .attr("dy", 18) 
    .style("opacity", 0.0001)  // Define initial opacity on the <text>s
    .style("fill", "#000");
  
txts                           // Append <textPaths>s. No need to keep the reference
  .append("textPath")
    .attr("xlink:href", function(d, i) {
      return "#theArc_" + i;
    })
    .text(function(d) {
      return d.name;
    });
    
txts.transition()              // Transition the original <text>s to new opacity value
  .duration(1400)
    .style("opacity", 1)
    .style("fill", "#000"); 

Check out the updated plunk for a demonstration of this functionality.

Looking ahead—SVG 2

The dynamics will change once SVG 2 becomes an official W3C recommendation and gets implemented across all browsers. In SVG 2, the textPath element evolves into a graphics element, enabling it to be styled using the opacity property. The code snippet below can be useful in identifying if your browser supports the textPath element according to SVG 2:

console.log(
  SVGGraphicsElement &&     // Part of SVG 2; not available in IE 11
  document.createElementNS("http://www.w3.org/2000/svg", "textPath")
    instanceof SVGGraphicsElement
);

If your browser adheres to SVG 2's version of textPath utilizing the SVGGraphicsElement interface, which is unique to SVG 2, the above code will output true. This evaluation returns true in Chrome 56 and FF 51 for me, but appears as false in IE 11.

Answer №2

Appreciate the feedback on the issue I came across. Thanks to Geraldo's input, I discovered that the problem was isolated to Firefox, while Internet Explorer was handling the transition correctly.

Following altocumulus's suggestion, I introduced an additional variable for the text element. The updated code snippet now appears as follows:

var txts = svg.data([json]).selectAll(".theTxts")
    .data(partition.nodes)
    .enter()
    .append("text")
    .style("opacity", 0)
    .attr("class", "theTxts")
    .attr("dx", 10) //Shift text from arc starting angle
    .attr("dy", 18) //Move text downwards

var txtPth =txts.append("textPath")
    .attr("xlink:href", function(d, i) {
      return "#theArc_" + i;
    })
    .text(function(d) {
      return d.name;
    })
    .style("fill", "#000");

txts.transition()
    .duration(7000)
    .style("opacity", 1)
    .style("fill", "#000");

The transition now functions smoothly on both IE and FF. You can view the updated plunk here.

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

A guide on smoothly navigating to the desired row in a gridview using Asp.net

Currently, I am developing a project using ASP.net and C# technology. In my application, I integrated a GridView that contains multiple rows. Within the grid, there is a text box and a search button available. The columns displayed in the grid are Id and ...

`Where can I locate a grid example?`

Right here: Upon reading the documentation, I discovered: onItemInserting has the following arguments: { grid // represents the grid instance item // item being inserted } In my software application there are multi ...

Is there a way to implement an onclick event on a specific line of text that triggers a popup box only when the text is clicked, without the popup appearing automatically when

I received assistance from a fellow user to customize a popup that turned out great. However, I'm having trouble getting the onclick function to work properly so that the popup only appears when the phone number is clicked. Ideally, I want the phone n ...

"Encountering a 'Cannot GET' error message while utilizing Rest API in Node.js

Currently, I am developing a project using nodejs along with the expressjs framework. My focus right now is on setting up and running a "Rest Api," but I seem to be encountering an error message that reads: Cannot GET /published Let me share my routes fil ...

Tips for adding an additional div inside a navigation container, evenly spacing objects, and aligning the search bar to the right

I'm currently working on designing a simple navigation bar but I'm facing difficulties in aligning the elements correctly. See my progress here: https://jsfiddle.net/zigzag/bL1jxfax/ Here are my objectives: 1) Ensure that the navigation bar rea ...

Integrate actual Angular $timeout service implementation into a unit test using Karma and Jasmine

I have a small Angular service that implements asynchronous rate-limiting for other functions, similar to this sample. Given that the core purpose of this class is to manage asynchronous behaviors, I need to test it in an asynchronous manner - purely synch ...

What methods are available for testing JavaScript interactions like freehand drawing on an HTML5 canvas using a mouse?

When it comes to testing object states, I am well-versed in using rspec in Ruby on Rails models. For instance, ensuring that a certain object meets certain expectations based on its prior state and a specific call. However, I am now faced with a new chall ...

What is the most effective method for showcasing HTML content in a recyclerView?

Within the recyclerView, I showcase comments that are stored in HTML format. My attempt to display the comments in a webView was successful, allowing me to use custom *.css, but it caused significant lag in the recyclerView when scrolling. Additionally, m ...

Utilizing Node.js in Phonegap

Currently, I am in the process of creating an iOS application with PhoneGap and I have an interest in incorporating node.js into a specific aspect of it. Is it possible to integrate an instance of node.js within the app using PhoneGap? ...

Customizing CSS according to specific URLs

I have two different domains - one ending with .nl and the other ending with .be. For instance, domain.nl and domain.be. Both domains share a similar overall style, but I want certain elements to have distinct styling depending on whether it is the .nl o ...

The Context API leaves me feeling lost and confused

I am currently utilizing Auth0 for user sign up. My goal is to extract the user id listed under sub:value, and then add it to my database to associate it with a user's post. To achieve this, I am attempting to utilize a Context API to retrieve the use ...

Looking for assistance with fixing the echo issue in my PHP temperature converter script. Any help is greatly appreciated

Below is my PHP code for converting temperatures: <html> <head> <title>Temperature Conversion</title> <meta charset="utf-8"> </head> <body> <form name="tempConvert" method=&q ...

Duplicate the array of objects and make alterations without affecting the original array

I have an array of objects that I need to deep copy and make modifications to each object without altering the original array or its contents. Here is my approach in JavaScript, but I am open to suggestions on a better method. const users = [ { ...

Spin the div in the opposite direction after being clicked for the second time

After successfully using CSS and Javascript to rotate a div by 45 degrees upon clicking, I encountered an issue when trying to rotate it back to 0 degrees with a second click. Despite my searches for a solution, the div remains unresponsive. Any suggestion ...

Generating a JavaScript array containing all elements belonging to a specific class name

As I work on my website, I am attempting to create an array from elements that have a specific class. This array should retrieve the videofile attribute value from all `a` tags with the class `videoLink`. The desired values in the final array should be: ...

Node.js async.each triggers the error message "Callback has already been invoked"

I seem to be facing a problem that I can't pinpoint despite searching for mistakes extensively. The issue arises when async.each throws a "Callback was already called" error. Below is the code snippet where I encounter this problem: async.each(this. ...

While my other function remains incomplete, AngularJS $http continues to run without interruption

I'm facing an issue in my AngularJS function where the $http request is being fired before my first function is completed. Here's a sample of how my code looks: $scope.function = function(){ $scope.functionOne(); // This function initializ ...

Receive alerts in Swift from WKWebView when a particular screen is displayed in HTML

Check out this sample HTML file I have. I'm currently using the WKWebView to display this HTML. My goal is to receive a notification in our Swift code when the user finishes the game and the "high score" screen appears, so we can dismiss the view and ...

Issue with .html causing .hover to malfunction

Attempting a basic image rollover using jQuery, I've encountered an issue with the code below: HTML: <div class="secondcircle" id="circleone"> <p> <img src="/../ex/img/group1.png"> </p> </div> JS: $("# ...

What could be causing an issue with CORS in ExpressJS in one scenario but not in another?

I am currently in the process of setting up a database and connecting it to various routes. Interestingly, I have been successful with one route ('register') but encountering issues with another ('login'). Whenever I attempt to run the ...