Transitioning the gradient fill of a single node among many - javascript

Let's say I have an SVG with a structure similar to this:
<svg>
<defs>
<linearGradient id="gradient-red">...</linearGradient>
<linearGradient id="gradient-blue">...</linearGradient>
</defs>
<g class="node">
<circle r="50" style="fill: url('#gradient-red');"></circle>
</g>
<g class="node">
<circle r="100" style="fill: url('#gradient-red');"></circle>
</g>
<g class="node">
<circle r="150" style="fill: url('#gradient-red');"></circle>
</g>
<g class="node">
<circle r="200" style="fill: url('#gradient-red');"></circle>
</g>
<g class="node">
<circle r="250" style="fill: url('#gradient-red');"></circle>
</g>
</svg>
I now have five circles with reddish gradients. I understand how to change the color of a selected node -- I just target it (via d3.select) and alter its style to 'fill', 'url("#gradient-blue"). But how would I go about transitioning the gradient fill from red to blue for that one node?
Something like this results in no tween/transition and instead causes an instant color swap:
d3.transition().duration(1000)
.tween('start', () => {
let test = d3.select(currentTarget);
test.transition().duration(1000).style('fill', 'url("#gradient-blue")');
And if I were to transition the stop-color of the gradients themselves, it changes all of the nodes/circles (because you're altering the <defs>).
What am I doing wrong?

Transition's interpolation
In D3, a transition basically interpolates a start value to an end value. This can be easy to demonstrate if we interpolate numbers. For instance, let's transition from 50 to 2000:
const interpolator = d3.interpolate(50, 2000);
d3.range(0, 1.05, 0.05).forEach(function(d) {
console.log(interpolator(d))
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
We can also interpolate strings:
const interpolator = d3.interpolate("March, 2000", "March, 2020");
d3.range(0, 1.05, 0.05).forEach(function(d) {
console.log(interpolator(d))
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
The problem
Now, let's have a look at your case: you want to interpolate from this:
url("#gradient-red")
To this:
url("#gradient-blue")
What are the possible intermediates here? Can you see that this is impossible? Here is the proof:
const interpolator = d3.interpolate("url(#gradient-red)", "url(#gradient-blue)");
d3.range(0, 1.1, 0.1).forEach(function(d) {
console.log(interpolator(d))
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
As you can see, the very first interpolation will instantly lead to the end value.
Possible solutions
The most obvious solution is interpolating the stop colour. However, as you just discovered, this will change the gradient of all circles.
So, the naive fix is creating several gradients, one for each circle, with unique IDs. While this may be an adequate solution for 3 or 4 circles, it's clearly not a clever solution if you have tens or hundreds of elements.
That being said, this is my suggestion:
Create a temporary gradient, let's give it the ID #gradient-temporary, just like the red one.
Then, when you select (or filter it somehow) a circle, change it's fill from "url(#gradient-red)" to "url(#gradient-temporary)". This change is immediate, no effect is obvious on the screen.
Do the transition on the stop colour of this temporary gradient.
When the transition finishes, change the circle's fill from "url(#gradient-temporary)" to "url(#gradient-blue)". Again, this is immediate. Also, change the stop colour of the temporary gradient back to red.
That way, you can have hundreds of circles, but you just need 3 gradients to transition them.
Here is a demo with that approach, click on each circle to transition it:
const circles = d3.selectAll("circle");
circles.on("click", function() {
const element = this;
d3.select(element).style("fill", "url(#gradient-temporary)");
d3.select("#gradient-temporary").select("stop:nth-child(2)")
.transition()
.duration(1000)
.style("stop-color", "rgb(0,0,255)")
.on("end", function() {
d3.select(element).style("fill", "url(#gradient-blue)");
d3.select("#gradient-temporary").select("stop:nth-child(2)")
.style("stop-color", "rgb(255,0,0)")
})
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<svg>
<defs>
<linearGradient id="gradient-red" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="0%" style="stop-color:rgb(211,211,211);stop-opacity:1" />
<stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
</linearGradient>
<linearGradient id="gradient-temporary" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="0%" style="stop-color:rgb(211,211,211);stop-opacity:1" />
<stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
</linearGradient>
<linearGradient id="gradient-blue" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="0%" style="stop-color:rgb(211,211,211);stop-opacity:1" />
<stop offset="100%" style="stop-color:rgb(0,0,255);stop-opacity:1" />
</linearGradient>
</defs>
<g class="node">
<circle r="20" cx="20" cy="70" style="fill: url('#gradient-red');"></circle>
</g>
<g class="node">
<circle r="20" cx="80" cy="70" style="fill: url('#gradient-red');"></circle>
</g>
<g class="node">
<circle r="20" cx="140" cy="70" style="fill: url('#gradient-red');"></circle>
</g>
<g class="node">
<circle r="20" cx="200" cy="70" style="fill: url('#gradient-red');"></circle>
</g>
<g class="node">
<circle r="20" cx="260" cy="70" style="fill: url('#gradient-red');"></circle>
</g>
</svg>

Related

How can I set the offset on a straight line in svg

I want to make a linear gradient on a straight line.But the offset always remains constant does not change because it is a straight line.
For example, the first stop is color red and the second stop is color blue, but the offset is a single color.
<defs>
<linearGradient id="Gradientm5cf5" x1="11.111" x2="33.111" y1="44.111" y2="77.111" gradientUnits="userSpaceOnUse">
<stop offset="0%" stop-color="red"></stop>
<stop offset="50%" stop-color="blue"></stop>
</linearGradient>
</defs>
<path d="M 1518 1131 L 1681.63 1131" fill="none" stroke="url(#Gradientm5cf5)" stroke-miterlimit="10" pointer-events="stroke" stroke-width="6" style="--animation-time:1s;" class="flow">
</path>

How to add text inside an SVG element

I would like to add text inside an element such that my intro is behind a rectangular, transparent element.
I tried adding text inside an SVG like this
<svg height="150" width="500">
<text x="100" y="30" fill="red">I love SVG!</text>
<defs>
<radialGradient id="grad1" cx="50%" cy="50%" r="50%" fx="50%" fy="50%">
<stop offset="0%" style="stop-color:rgb(255,255,255);
stop-opacity:0" />
<stop offset="100%" style="stop-color:rgb(0,0,255);stop-opacity:1" />
</radialGradient>
</defs>
<ellipse cx="200" cy="70" rx="85" ry="55" fill="url(#grad1)" />
</svg>
However I can't Send the object backwards. Is this even possible?
Is there a better approach I can use to meet this objective rather than the solution I'm trying to use?
Are you just talking about whether the text is behind the oval, or on top of it?
If so, just changing the order of elements fixes that:
<svg height="150" width="500">
<defs>
<radialGradient id="grad1" cx="50%" cy="50%" r="50%" fx="50%" fy="50%">
<stop offset="0%" style="stop-color:rgb(255,255,255);
stop-opacity:0" />
<stop offset="100%" style="stop-color:rgb(0,0,255);stop-opacity:1" />
</radialGradient>
</defs>
<ellipse cx="200" cy="70" rx="85" ry="55" fill="url(#grad1)" />
<text x="100" y="30" fill="red">I love SVG!</text>
</svg>
Or is there something else you're trying to do? (Please note, even with the text on top of the oval, the contrast isn't that great, and it's still hard to read.)

how to append element to dom with case-sensitive elem name

In the following example I have an inline SVG. You can see that within it, some of the SVG elements (<radialGradient> and <linearGradient>) are camel case. How would I programatically add another such element to the <defs> parent.
document.createElement() converts its argument to lowercase before creating the node, so the case sensitive element name is lost. And the Square remains white in my rendered SVG.
Is there any way to do this using javascript?
const linearGradient = document.createElement('linearGradient')
linearGradient.id="my-gradient"
linearGradient.setAttribute('x1', '320.38')
linearGradient.setAttribute('y1', '145.87')
linearGradient.setAttribute('x2', '685.45')
linearGradient.setAttribute('y2', '447.31')
linearGradient.setAttribute('gradientUnits','userSpaceOnUse')
const stop1 = document.createElement('stop')
stop1.setAttribute('offset', '0')
stop1.setAttribute('stop-color', '#ed1e79')
const stop2 = document.createElement('stop')
stop2.setAttribute('offset', '1')
stop2.setAttribute('stop-color', '#ff0')
linearGradient.appendChild(stop1)
linearGradient.appendChild(stop2)
document.querySelector('svg defs').appendChild(linearGradient)
<svg id="a663773f-8c77-4356-b99a-00f824054fe3" data-name="Layer 1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 894.23 805.64">
<defs>
<radialGradient id="e87c7fc2-e4ed-4d65-889f-d6153929018e" cx="659.66" cy="541.62" r="261.56" gradientUnits="userSpaceOnUse">
<stop offset="0" stop-color="#615ab2"/>
<stop offset="1" stop-color="#c0bde0"/>
</radialGradient>
<linearGradient id="a0a8f98c-a36f-4841-bfb8-51385632467d" x1="23.64" y1="496.01" x2="632.48" y2="496.01" gradientUnits="userSpaceOnUse">
<stop offset="0" stop-color="#fff"/>
<stop offset="1" stop-color="#96e700"/>
</linearGradient>
</defs>
<title>shapes</title>
<g>
<rect x="212.97" y="11" width="529.92" height="529.92" style="fill: url(#my-gradient)"/>
<path d="M769.89,99V606.92H262V99H769.89m22-22H240V628.92H791.89V77Z" transform="translate(-38 -76.99)"/>
</g>
<g>
<path d="M659.66,803.19a261.58,261.58,0,1,1,185-76.61A259.86,259.86,0,0,1,659.66,803.19Z" transform="translate(-38 -76.99)" style="fill: url(#e87c7fc2-e4ed-4d65-889f-d6153929018e)"/>
<path d="M659.66,291.06a249.68,249.68,0,1,1-97.52,19.68,249,249,0,0,1,97.52-19.68m0-22c-150.53,0-272.56,122-272.56,272.56s122,272.57,272.56,272.57,272.57-122,272.57-272.57-122-272.56-272.57-272.56Z" transform="translate(-38 -76.99)"/>
</g>
<g>
<polygon points="175.85 576.03 23.64 427.66 233.99 397.1 328.06 206.49 422.13 397.1 632.48 427.66 480.27 576.03 516.2 785.53 328.06 686.62 139.92 785.53 175.85 576.03" style="fill: url(#a0a8f98c-a36f-4841-bfb8-51385632467d)"/>
<path d="M366.06,308.34l81.65,165.43,5.11,10.37,11.45,1.67,182.57,26.52L514.73,641.11l-8.28,8.07,1.95,11.4,31.19,181.83L376.3,756.56l-10.24-5.38-10.24,5.38L192.53,842.41l31.18-181.83,2-11.4-8.28-8.07L85.28,512.33l182.57-26.52,11.44-1.67,5.12-10.37,81.65-165.43m0-49.71L264.68,464,38,497,202,656.86,163.31,882.63,366.06,776,568.81,882.63,530.09,656.86,694.12,497,467.43,464,366.06,258.63Z" transform="translate(-38 -76.99)"/>
</g>
</svg>
I think
document.createElementNS('http://www.w3.org/2000/svg', 'linearGradient')
should work, according to this

How to calculate the position of a circle depending on the current time on the curve

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1158 696">
<defs>
<linearGradient id="grad1" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="0%" style="stop-color:red;stop-opacity:1" />
<stop offset="50%" style="stop-color:blue;stop-opacity:1" />
<stop offset="100%" style="stop-color:red;stop-opacity:1" />
</linearGradient>
</defs>
<g><g><g><path fill="none" stroke="url(#grad1)" stroke-miterlimit="50" stroke-width="4" d="M0 212.4c241.4 0 274.174-213.852 579-210 304.826 1.853 345.472 211 581 210"/></g></g></g>
<g>
<g opacity=".76">
<path fill="#011134" d="M.25 214.001c239.653 0 274.65-213.852 580.005-210 305.355 3.853 340.062 210 580.005 210v486H.25v-486z"/>
</g>
</g>
<g transform="translate(0, 198)" >
<filter id="blurMe">
<feGaussianBlur in="SourceGraphic" stdDeviation="2" />
</filter>
<circle filter="url(#blurMe)" cx="16" cy="16" r="12" fill="red" />
</g>
</svg>
I've tried to use a formula of Normal distribution
I need to achive a result where the circle moves along the curve depends on the current time for that i need to find a formula which represents the graph described above, but the Gaussian distribution does not give me a desirable graph, the Gaussian distribution gives me next:

directed gradient in svg's path element

I'm writing a simple webpage that displays a graph and shows dependencies. I found an unexpected behavior in how path elements are rendered within svg.
Here's the full HTML of the example:
<html>
<body>
<svg id="svgConnections" xmlns="http://www.w3.org/2000/svg" style="width: 300px; height: 120px">
<defs>
<linearGradient id="grad1" >
<stop offset="0%" style="stop-color:yellow;stop-opacity:1" />
<stop offset="100%" style="stop-color:red;stop-opacity:1" />
</linearGradient>
</defs>
<path d="M40,40 L100,100 Z" stroke="url(#grad1)" strokeWidth="1px" />
<path d="M200,100 L140,40 Z" stroke="url(#grad1)" strokeWidth="1px" />
</svg>
</body>
</html>
The same example is on https://jsfiddle.net/4fLjm0e2/
What bugs me is that the first line, which goes from top left to bottom right corner, looks exactly like the second line, which goes "in reverse": from bottom right corner to the top left.
How do I make the path always start with yellow and end with red?
This is not a bug. This is problem in understanding.
The default behavior of a linear gradient is to transition along a horizontal line from the left side of an object to its right side. It doesn't matter if you draw a path from left to right or from right to left. In both cases gradient will appear as from left to right as per default settings.
Consider the demo below:
<svg width="120" height="120" xmlns="http://www.w3.org/2000/svg">
<defs>
<linearGradient id="grad1" gradientUnits="userSpaceOnUse">
<stop offset="0%" style="stop-color:yellow;stop-opacity:1" />
<stop offset="100%" style="stop-color:red;stop-opacity:1" />
</linearGradient>
</defs>
<g stroke-width="2">
<path d="M10,40 L110,40 Z" stroke="url(#grad1)" />
<path d="M110,70 L10,70 Z" stroke="url(#grad1)" />
</g>
</svg>
If you wants the transition of colors to occur across a vertical line or a line at an angle, you must specify the line's starting point with the x1 and
y1 attributes and its ending points with the x2 and y2 attributes.
Rather than duplicate the stops into each <linearGradient> element, we'll use the xlink:href attribute to refer to the original gradient. The stops will be inherited, but the x- and y-coordinates will be overridden by each individual gradient.
<linearGradient id="grad1" x1="0" y1="0" x2="1" y2="1">
<stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" />
<stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
</linearGradient>
<linearGradient id="grad2" xlink:href="#grad1" x1="1" y1="1" x2="0" y2="0"></linearGradient>
Extending the above example:
<svg width="120" height="120" xmlns="http://www.w3.org/2000/svg">
<defs>
<linearGradient id="grad1" gradientUnits="userSpaceOnUse">
<stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" />
<stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
</linearGradient>
<linearGradient id="grad2" xlink:href="#grad1" x1="120" y1="0" x2="0" y2="0"></linearGradient>
</defs>
<g stroke-width="2">
<path d="M10,40 L110,40" stroke="url(#grad1)" />
<path d="M110,70 L10,70 Z" stroke="url(#grad2)" />
</g>
</svg>
As in your example, you are using diagonal paths so we need to override x1, y1, x2 and y2 attribute of both <linearGradient> elements.
These values on the first <linearGradient> element will override the default left to right settings to produce a diagonal gradient from top left to the right bottom.
While on the <linearGradient> element these values will change the direction of gradient i.e from bottom to top.
Now we can apply these gradients to the respective paths.
<svg width="300" height="120" xmlns="http://www.w3.org/2000/svg">
<defs>
<linearGradient id="grad1" x1="0" y1="0" x2="1" y2="1">
<stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" />
<stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
</linearGradient>
<linearGradient id="grad2" xlink:href="#grad1" x1="1" y1="1" x2="0" y2="0"></linearGradient>
</defs>
<g stroke-width="2">
<path d="M40,40 L100,100 Z" stroke="url(#grad1)" />
<path d="M200,100 L140,40 Z" stroke="url(#grad2)" />
</g>
</svg>
Note: This Question can be useful in context with the current problem.

Categories