-
Notifications
You must be signed in to change notification settings - Fork 62
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
Gradient along the spline? #334
Comments
I don't think this is typically supported by graphics packages and I think it is because it can be computationally a bit expensive and maybe difficult to get reliable results. That said, I think diagrams would be a great place to include this feature, though I'm not sure the best way to express it or if it could be supported by all backends. Here is an example of someone achieving this with SVG and JavaScript: https://bl.ocks.org/mbostock/4163057 |
@fryguybob |
There is a way to sample points using tracing like in |
@ivxvm I think you want |
I've implemented similar approach with diagrams and I'm very satisfied with results. It allows not only gradients, but variation of any style parameters along the trails. Here's an example of decreasing-width-decreasing-opacity: Code is a mess, can be improved for sure: morphingSpline :: Int -> (Double -> _ -> _) -> [P2 Double] -> Diagram B
morphingSpline numIterations styleFn points =
mconcat $ map render $ getZipList $ (,,) <$> ZipList points' <*> ZipList (tail points') <*> ZipList ticks
where
spline = cubicSpline False points :: Trail _ _
points' = map (\t -> spline `atParam` t) ticks
ticks = [0 :: Double, 1 / fromIntegral numIterations .. 1]
render (s, e, t) = strokeLocTrail (fromSegments [straight (e - s)] `at` P s) # styleFn t
styleFn t = opacity (1 - t) . lw (local ((1 - t) / 3))
diagram = morphingSpline 1000 styleFn points # bgFrame 1 white Ripple texture is a side effect of aliasing (or something else), gonna figure out how to fix it. |
You can simplify your code by using section. I was hoping it might also help with the ripple effect, but it doesn't. edit: you can fix the ripple texture by overlapping segments (tail became drop 2, fixed below) morphingSpline :: _ => Int -> (Double -> _ -> _) -> [P2 Double] -> Diagram b
morphingSpline numIterations styleFn points =
mconcat $ zipWith render ticks (drop 2 ticks)
where
spline = cubicSpline False points :: Located (Trail _ _)
ticks = [0, 1 / fromIntegral numIterations .. 1]
render s e = stroke (section spline s e) # styleFn s |
@bacchanalia Edit: |
lineCap LineCapSquare works for basically the same reason as dropping ticks works: the line cap is thick and therefore causes overlap. The pattern still shows up to a lesser extent with morphingSpline :: _ => Int -> (Double -> _ -> _) -> [P2 Double] -> Diagram b
morphingSpline numIterations styleFn points =
mconcat $ zipWith render (replicate 10 0 ++ ticks) ticks
where
spline = cubicSpline False points :: Located (Trail _ _)
ticks = [0, 1 / fromIntegral numIterations .. 1]
render s e = stroke (section spline s e) # styleFn s
points = map p2 [(0,0), (200,300), (500,-200), (-400,100), (0,300)]
-- using opacity/5 for n = 10 as a guess
styleFn t = opacity ((1 - t)/5) . lw (local (500*(1 - t) / 3))
diagram = morphingSpline 1000 styleFn points # pad 1.3 # bg white |
@bacchanalia Edit: |
Right, I should have mentioned that section was fixed in HEAD, but the fix hasn't been released yet. Sorry about that. |
@bacchanalia morphingSpline :: _ => Int -> (Double -> _ -> _) -> [P2 Double] -> Diagram b
morphingSpline numIterations styleFn points = mconcat $ zipWith render ticks (tail ticks) -- (replicate 10 0 ++ ticks) ticks
where
spline = cubicSpline False points :: Located (Trail _ _)
ticks = [0, 1 / fromIntegral numIterations .. 1]
render s e = stroke (section spline s e) # styleFn s
points = map p2 [(0,0), (200,300), (500,-200), (-400,100), (0,300)]
styleFn t = id -- opacity ((1 - t)/5) . lw (local (500*(1 - t) / 3))
diagram = morphingSpline 1000 styleFn points # pad 1.3 # bg white |
Is there any general way to specify gradient along the 1d position on spline?
For example, this one I made using
cubicSpline
, it consists of 6 points:https://imgur.com/MOqOKDN
Gradient is:
linear = mkLinearGradient (mkStops [(black, 0, 1), (white, 1, 1)]) 0 1 GradPad
What I want to achieve is that spline gradually becomes white along the way from the start to the end, so that point A is visible and B is not.
The text was updated successfully, but these errors were encountered: