Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Opacity transition does not work properly for % values #147

Open
matthew-h-wang opened this issue Feb 1, 2024 · 2 comments
Open

Opacity transition does not work properly for % values #147

matthew-h-wang opened this issue Feb 1, 2024 · 2 comments

Comments

@matthew-h-wang
Copy link

When using transitions with the opacity style, values given in "%" units do not correctly transition from the prior value, but instead always transition from "0%". By contrast, using numbers for opacity values do correctly transition between opacity values.

Here's a simple example, where the intended effect is 5 circles who smoothly change to random opacities every update:

let data = [];

//Produce 5 points with increasing x values and random o values in range [0,100)
function updateData() {
	data = [];
	for(let i=0; i<5; i++) {
		data.push({x: i * 100 + 50, o: Math.floor(Math.random() * 100)});
	}
}

function update() {
	d3.select('svg')
	.selectAll('circle')
		.data(data)
		.join('circle')
		.attr('cy', 50)
		.attr('r', 40)
		.attr('cx', (d) => d.x)
		.transition()
		.duration(500)
		.style('opacity', (d) => d.o +"%"); // using percent units
}

function updateAll() {
	updateData();
	update();
}

updateAll();

The actual resulting effect is each update, the circles fade in from 0 opacity, rather than smoothly from the prior opacity value:

percentTransition.mov

By changing the final line in update() to this:

		.style('opacity', (d) => d.o * 0.01); // using decimal units

we get the intended effect:

decimalTransition.mov

I have not tested transitioning other style properties that use percent units, but it is possible this is an issue beyond just opacity.

@mbostock mbostock transferred this issue from d3/d3 Feb 1, 2024
@mbostock
Copy link
Member

mbostock commented Feb 1, 2024

I think this probably isn’t fixable and is the same fundamental issue as #72 (comment) which is that transition.style depends on the computed value of the style property. In the case of opacity, the computed value is a number between 0 and 1. So you always need to specify the target value of the transition in the same manner as the computed value for the default interpolator to work correctly.

@matthew-h-wang
Copy link
Author

After reading the documentation for transition.style, that makes more sense. It does feel like an easy pitfall that others might run into because many style properties work just fine with alternative units when transitions are not involved. Perhaps the docs could include some warnings/guidelines on what units are appropriate for certain common style properties.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants