Giter Site home page Giter Site logo

Comments (10)

Plecra avatar Plecra commented on May 21, 2024

Oh, I see we're rendering on canvases for the shapes! Gradient should be perfect for this, but I can't figure out how to make it work :/

from motion-canvas.

Plecra avatar Plecra commented on May 21, 2024

Hahah!

const filled = createSignal(0);
const myRect = createRef<Rect>();
view.add(<Rect ref={myRect} width={420} height={20} x={0} fill={() => new Gradient({
  from: myRect().cacheRect().topLeft,
  to: myRect().cacheRect().topRight,
  stops: [{ color: "blue", offset: filled() }, { color: "white", offset: filled() }]
})}/>);
yield* filled(1, 2, linear);

That's lovely, pretty much what I wanted :) I'll leave it here in case you want to pop it on the docs for Gradient?

from motion-canvas.

Plecra avatar Plecra commented on May 21, 2024

(Related feature request: It'd be rlly awesome to be able to use absolute positions with Gradient so I can use it across multiple nodes)

from motion-canvas.

Plecra avatar Plecra commented on May 21, 2024

That would be to simplify this:

const filled = createSignal(0);
const space = createSignal<Bounds>();

const layout = <Layout layout direction="column" gap={40} alignItems="center">
  {[[300, 100], [420, 20]].map(([w, h]) => {
    const rect = <Rect width={w} height={h}/> as Rect;
    rect.fill(() => {
      const bounds = space();
      return new Gradient({
        from: bounds.center.transformAsPoint(rect.worldToLocal()),
        to: bounds.center.transformAsPoint(rect.worldToLocal()),
        type: "radial",
        toRadius: Math.sqrt(bounds.width**2 + bounds.height**2) / 2,
        stops: [{ color: "blue", offset: filled() }, { color: "white", offset: filled() }]
      })
    })
    return rect;
  })}
</Layout>;
space(() => layout.cacheRect().transform(layout.localToWorld()));
view.add(layout);

yield* filled(1, 2, linear);

too um 🤔

const filled = createSignal(0);
const space = createSignal<Bounds>();
const gradient = createSignal(() => new Gradient({
    from: space().center,
    to: space().center,
    type: "absolute-radial",
    toRadius: Math.sqrt(space().width**2 + space().height**2) / 2,
    stops: [{ color: "blue", offset: filled() }, { color: "white", offset: filled() }]
}))

const layout = <Layout layout direction="column" gap={40} alignItems="center">
  {[[300, 100], [420, 20]].map(([w, h]) => <Rect width={w} height={h} fill={gradient}/>)}
</Layout>;
space(() => layout.cacheRect().transform(layout.localToWorld()));
view.add(layout);

yield* filled(1, 2, linear);

from motion-canvas.

aarthificial avatar aarthificial commented on May 21, 2024

I think the best solution would be to turn Gradient into a node so that it could be placed in the scene:

const gradient = createRef<Gradient>();

view.add(
  <>
    <Gradient ref={gradient} />
    <Rect fill={gradient()} />
  </>
);

It wouldn't render anything, but moving it around would move the gradient from/to points. This way you could not only have absolute gradients but also parent them to other nodes.

from motion-canvas.

brandonpelfrey avatar brandonpelfrey commented on May 21, 2024

One possible solution to this might be something like...

<Rect radius={10} width={200} height={20} clip >
  <Rect fill={gradient} {/* Other props */} />
</Rect>

So the outer Rect is the actual line being filled, specified with clip properties so that none of the child Rect is drawn outside. Meanwhile the child Rect can be resized or style animated in order to get the style to animate within that line. Would this work? Agreed having a nicer integration with Gradient would be nice.

from motion-canvas.

Plecra avatar Plecra commented on May 21, 2024

Hmm could the sizes be specified as percentages of the parent? I don't want to have to manually scale filled

from motion-canvas.

aarthificial avatar aarthificial commented on May 21, 2024

If by "percentages of the parent" you mean its scale, then yes. After turning Gradient into a Node, putting it inside of another node would make it scale together with it:

const gradient = createRef<Gradient>();

view.add(
  <Rect fill={gradient()}>
    <Gradient ref={gradient} />
  </Rect>
);

But if you mean the size, then no. It's difficult to imagine how something like this would work since the gradient doesn't have to be aligned with the node - it could be diagonal, for example.

from motion-canvas.

Plecra avatar Plecra commented on May 21, 2024

Oh, that was confusing of me! :D I'd only seen @brandonpelfrey's comment when I sent that.

As for Gradient as a Node, I don't really understand what you're proposing. Gradients have N stops, they need colour information; and what I'm looking for is being able to build a gradient relative to the Node we're drawing it in, which I can't see the Gradient nodes' solution for. What do you imagine we'd write to render progress bar?

from motion-canvas.

chris-findlay avatar chris-findlay commented on May 21, 2024

Regarding progress along a Line, it'd be nice if a line had start% and end% props, as then we could render a line with start% = filled() and a clone with end% = filled(). Same for splines when we get them.

from motion-canvas.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.