Skip to content

Commit 57c73c3

Browse files
committed
Final text and animations
1 parent c6efe85 commit 57c73c3

2 files changed

Lines changed: 64 additions & 3 deletions

File tree

animation/projects/src/std_function/scenes/std_function.tsx

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { makeScene2D, Code, LezerHighlighter, lines } from '@motion-canvas/2d';
1+
import { makeScene2D, Code, LezerHighlighter, lines, Node, Rect, Txt, Line } from '@motion-canvas/2d';
22
import { all, createRef, waitFor } from '@motion-canvas/core';
33
import { MyStyle } from '../../styles';
44
import { parser as parser_cpp } from '@lezer/cpp';
@@ -118,6 +118,63 @@ export default makeScene2D(function* (view) {
118118
yield* centerOn(codeRef(), [lines(23, 23)], 1, 35);
119119
yield* waitFor(3);
120120

121+
const myFuncRect = createRef<Rect>();
122+
const baseRect = createRef<Rect>();
123+
const implRect = createRef<Rect>();
124+
const tRect = createRef<Rect>();
125+
126+
const line1 = createRef<Line>();
127+
const line2 = createRef<Line>();
128+
const line3 = createRef<Line>();
129+
130+
view.add(
131+
<Node y={0} scale={1.4}>
132+
<Rect ref={myFuncRect} width={240} height={60} x={-505} fill="#2d2d2d" radius={10} opacity={0}>
133+
<Txt text="~MyFunction()" fill="#eeeeee" fontFamily="Fira Mono" fontSize={24} />
134+
</Rect>
135+
<Line ref={line1} points={[[-375, 0], [-295, 0]]} stroke="#569CD6" lineWidth={4} endArrow end={0} />
136+
<Rect ref={baseRect} width={260} height={60} x={-155} fill="#2d2d2d" radius={10} opacity={0}>
137+
<Txt text="~CallableBase()" fill="#eeeeee" fontFamily="Fira Mono" fontSize={24} />
138+
</Rect>
139+
<Line ref={line2} points={[[-15, 0], [65, 0]]} stroke="#569CD6" lineWidth={4} endArrow end={0} />
140+
<Rect ref={implRect} width={300} height={60} x={225} fill="#2d2d2d" radius={10} opacity={0}>
141+
<Txt text="~CallableImpl<T>()" fill="#eeeeee" fontFamily="Fira Mono" fontSize={24} />
142+
</Rect>
143+
<Line ref={line3} points={[[385, 0], [465, 0]]} stroke="#569CD6" lineWidth={4} endArrow end={0} />
144+
<Rect ref={tRect} width={150} height={60} x={550} fill="#2d2d2d" radius={10} opacity={0}>
145+
<Txt text="~T()" fill="#eeeeee" fontFamily="Fira Mono" fontSize={24} />
146+
</Rect>
147+
</Node>
148+
);
149+
150+
// 6. Destructors flow
151+
yield* codeRef().opacity(0, 1);
152+
153+
yield* myFuncRect().opacity(1, 0.5);
154+
yield* waitFor(0.5);
155+
yield* line1().end(1, 0.5);
156+
yield* baseRect().opacity(1, 0.5);
157+
yield* waitFor(0.5);
158+
yield* line2().end(1, 0.5);
159+
yield* implRect().opacity(1, 0.5);
160+
yield* waitFor(0.5);
161+
yield* line3().end(1, 0.5);
162+
yield* tRect().opacity(1, 0.5);
163+
yield* waitFor(3);
164+
165+
yield* all(
166+
tRect().opacity(0, 0.5),
167+
line3().opacity(0, 0.5),
168+
implRect().opacity(0, 0.5),
169+
line2().opacity(0, 0.5),
170+
baseRect().opacity(0, 0.5),
171+
line1().opacity(0, 0.5),
172+
myFuncRect().opacity(0, 0.5),
173+
);
174+
175+
yield* centerOn(codeRef(), DEFAULT, 0, 20);
176+
yield* codeRef().opacity(1, 1);
177+
121178
yield* centerOn(codeRef(), DEFAULT, 1, 20);
122179
yield* waitFor(3);
123180
});

lectures/std_function.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
- [Type Erasure (How it works under the hood)](#type-erasure-how-it-works-under-the-hood)
99
- [Summary](#summary)
1010

11+
<!-- Hey everyone. It has been a while since the last video, but I have a good reason for such a delay! Anyway, I finally found the time to record another video so let's dive in! -->
12+
1113
Remember in one of our [previous lectures](lambdas.md) we looked at lambdas? We saw how they give us an easy and clean way to create function objects on the fly, saving us from writing boilerplate structs just to pass a callable - for example, a simple comparator or an operation to a standard algorithm.
1214

1315
But what happens if we want to store these callables somewhere and call them at a later time? And what if we want these callables to be *anything* that behaves like a function, not just lambdas?
@@ -343,8 +345,10 @@ Real `std::function` class is, of course, much more complex. It handles differen
343345
> ⚠️ Finally, it won't hurt to stress this again: you'll probably never need to implement your own type-erased wrapper. But it's good to know how it works under the hood. Who knows, maybe it'll come up in a job interview (like it did for me some years ago, looking at you, Misha)!
344346

345347
## Summary
346-
To sum up, `std::function` is a powerful tool when we need a generic, uniform way to store and pass around things that can be called. It bridges the gap between the static compile-time world of lambdas/functors and the dynamic runtime world of vectors of callbacks. All without defining common base classes for all of our callables!
348+
To sum up, `std::function` is a powerful tool when we need a generic, uniform way to store and pass around things that can be called. It is extremely versatile, allowing to use any callable object with the same interface.
349+
350+
However, with great power comes great responsibility (and potential heap allocations) as well as a potential runtime overhead due to the use of a vtable.
347351

348-
But remember, with great power comes great responsibility (and potential heap allocations). So we should probably use `std::function` for callbacks, event handlers, and storing tasks, but stick to templates when we need maximum performance on hot paths, unless we measured otherwise.
352+
So, as a rule of thumb, we should probably use `std::function` for callbacks, event handlers, and storing tasks, but stick to templates when we need maximum performance on hot paths, unless we measured otherwise.
349353

350354
<!-- And that's it for now! Thanks a lot for watching and I'll catch you in the next video, bye! -->

0 commit comments

Comments
 (0)