question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Q: Adding Linear Gradient to fill Path area

See original GitHub issue

Hi! I tried to implement LinearGradient to fill out all area under Path line.

Wanted to get something like this:

image

Do you know how it could be implemented with react-native-skia LinearGradient component?

I can’t get the needed behaviour… 😦

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
chrispadercommented, Sep 12, 2022

This was implemented in #41 and is available now in versions above 0.3.0

1reaction
DDushkincommented, May 6, 2022

So Skia creates SVG path with commands the same way as you would create an SVG path manually with flags like M 0 100 L 100,100 z and etc. So here looking to your screenshot imagine the first and last point of your path and think how to make that path looped.

//./src/CreateGraphPath.ts

const path = Skia.Path.Make(); // -> initialize SVG path

  for (let i = 0; i < points.length; i++) {
    const point = points[i]!;

    // first point needs to start the path
    if (i === 0) path.moveTo(point.x, point.y); //-> start point

    const prev = points[i - 1];
    const prevPrev = points[i - 2];

    if (prev == null) continue;

    const p0 = prevPrev ?? prev;
    const p1 = prev;
    const cp1x = (2 * p0.x + p1.x) / 3;
    const cp1y = (2 * p0.y + p1.y) / 3;
    const cp2x = (p0.x + 2 * p1.x) / 3;
    const cp2y = (p0.y + 2 * p1.y) / 3;
    const cp3x = (p0.x + 4 * p1.x + point.x) / 6;
    const cp3y = (p0.y + 4 * p1.y + point.y) / 6;

    path.cubicTo(cp1x, cp1y, cp2x, cp2y, cp3x, cp3y); 
    if (i === points.length - 1) {
      path.cubicTo(point.x, point.y, point.x, point.y, 4 * point.x, point.y); //-> end point
    }
  }
  /* 
  At this point, we generated LineGraph,
  but to build the Gradient we need to connect the first and last points 
  */
  path.lineTo(width + graphPadding, height + graphPadding); //-> go to right bottom corner
  path.lineTo(0 + graphPadding, height + graphPadding); //-> go to left bottom corner
  
  /*
  Now we kinda have the correct shape of our gradient
  */
  return path;

Then in the render method, we add another Path with fill near our current Path with stroke

              <Path
                path={path}
                strokeWidth={lineThickness}
                color={color}
                style="stroke"
                strokeJoin="round"
                strokeCap="round"
              />
              {gradientColors && ( //-> New one
                <Path path={path}>
                  <LinearGradient
                    start={vec(0, 0)}
                    end={vec(0, height)}
                    colors={gradientColors} //as Color[]
                  />
                </Path>
              )}

This is how I solved the issue, maybe @mrousavy has a better solution?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can I apply a gradient along an SVG path? - Stack Overflow
Another way is to make two half circles and apply the opposite linear gradient to each's stroke, and make sure they are both...
Read more >
radial-gradient() - CSS: Cascading Style Sheets | MDN
A radial gradient is specified by indicating the center of the gradient (where the 0% ellipse will be) and the size and shape...
Read more >
Create gradients in Illustrator - Adobe Support
Apply gradient on one object. Select the other objects that you want to fill with same gradient. To do this, click the Selection...
Read more >
How To Apply SVG Linear Gradients To A Fill Or Stroke
SVG linear gradients are easy to work with and fun too. They can be added as the fill of an SVG graphic or...
Read more >
How to: Paint an Area with a Linear Gradient - WPF .NET ...
To create a horizontal linear gradient, change the StartPoint and EndPoint of the LinearGradientBrush to (0,0.5) and (1,0.5). In the following ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found