summaryrefslogtreecommitdiff
path: root/src/scenes/first_box.tsx
blob: 4927157fdb45946013bb86f884a9e77d814568bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import {
  Img,
  Rect,
  Node,
  Video,
  makeScene2D,
  Txt,
  Line,
  LineSegment,
} from "@motion-canvas/2d";
import {
  Direction,
  beginSlide,
  createRef,
  map,
  slideTransition,
  tween,
  all,
  waitFor,
} from "@motion-canvas/core";

import { CodeBlock } from "@motion-canvas/2d/lib/components/CodeBlock";
import { theme } from "../theme";

const fibonacciFn = (n: number): number => {
  if (n <= 2) {
    return 1;
  }
  return fibonacciFn(n - 1) + fibonacciFn(n - 2);
};

const fibonacci = `
const fibonacci = (n: number): number => {
  if (n <= 2) {
    return 1;
  }
  return fibonacci(n - 1) + fibonacci(n - 2);
};
`
  .split("\n")
  .filter((x) => x.trim())
  .join("\n");

export default makeScene2D(function* (view) {
  const block = createRef<CodeBlock>();
  const node = createRef<Node>();
  const rect = createRef<Rect>();

  const boxMoji = createRef<Txt>();
  const inSegment = createRef<Line>();
  const outSegment = createRef<Line>();
  const input = createRef<Txt>();
  const output = createRef<Txt>();

  yield* view.add(
    <>
      <Rect
        ref={rect}
        radius={4}
        stroke={theme.overlay0.hex}
        fill={theme.crust.hex}
        lineWidth={4}
        padding={60}
        layout
      >
        <Node ref={node} opacity={0}>
          <CodeBlock
            fontFamily={theme.font}
            language="typescript"
            ref={block}
            fontSize={1}
            code={fibonacci}
          ></CodeBlock>
        </Node>
      </Rect>
      <Txt fontFamily={theme.font} fill={theme.text.hex} ref={boxMoji}>
        😴
      </Txt>
      <Line
        points={[]}
        ref={inSegment}
        stroke={theme.green.hex}
        lineWidth={8}
        radius={40}
        endArrow
      ></Line>
      <Line
        points={[]}
        ref={outSegment}
        stroke={theme.red.hex}
        lineWidth={8}
        radius={40}
        endArrow
      ></Line>
      <Txt
        opacity={0}
        fontFamily={theme.font}
        fill={theme.text.hex}
        ref={input}
      ></Txt>
      <Txt
        opacity={0}
        fontFamily={theme.font}
        fill={theme.text.hex}
        ref={output}
      ></Txt>
    </>,
  );

  yield boxMoji().position(rect().middle());
  yield* slideTransition(Direction.Left);

  yield* beginSlide("Black Box");

  const padding = 100;
  const left = rect().left();
  const right = rect().right();
  yield* all(
    inSegment().points([left.addX(-padding), left], 0.5),
    outSegment().points([right, right.addX(padding)], 0.5),
  );
  yield input().position(left.addX(-padding));
  yield output().position(right);
  for (const i of [1, 2, 3]) {
    yield* all(
      input().opacity(1, 0.5),
      input().text(i.toString(), 0.5),
      input().position(left.addX(-padding - 20), 0.5),
    );

    yield* beginSlide("Input " + i);

    yield* input().position(left, 0.5);
    yield* all(input().opacity(0, 0.2), boxMoji().text("👷‍♀️⚙️", 0.2));
    yield* waitFor(0.5);

    const result = fibonacciFn(i);
    yield* all(
      output().opacity(1, 0.5),
      output().text(result.toString(), 0.5),
      output().position(right, 0.5),
    );

    yield* all(boxMoji().text("😴", 0.2));

    yield* beginSlide("Output " + i);
  }

  yield* all(
    boxMoji().opacity(0, 0.2),
    block().fontSize(30, 1),
    node().opacity(1, 1),
  );

  yield* beginSlide("Revealing");
});