|
| 1 | +import { Node, lines, word, makeScene2D, Code, Txt, Rect, Grid, Line, Layout } from '@motion-canvas/2d'; |
| 2 | +import { all, sequence, waitFor } from '@motion-canvas/core/lib/flow'; |
| 3 | +import { Vector2 } from '@motion-canvas/core/lib/types'; |
| 4 | +import { createRef } from '@motion-canvas/core/lib/utils'; |
| 5 | +import { createSignal, DEFAULT } from '@motion-canvas/core/lib/signals'; |
| 6 | +import { CodeBlock, edit, insert, range } from '@motion-canvas/2d/lib/components/CodeBlock'; |
| 7 | + |
| 8 | +const RED = '#ff6470'; |
| 9 | +const GREEN = '#99C47A'; |
| 10 | +const BLUE = '#68ABDF'; |
| 11 | +const WHITE = '#FFFFFF'; |
| 12 | + |
| 13 | +export default makeScene2D(function* (view) { |
| 14 | + |
| 15 | + const grid_ref = createRef<Grid>(); |
| 16 | + const address_ref = createRef<Txt>(); |
| 17 | + const data_rect_ref_1 = createRef<Rect>(); |
| 18 | + const data_rect_ref_2 = createRef<Rect>(); |
| 19 | + |
| 20 | + const table_layout = |
| 21 | + <Layout |
| 22 | + direction={'column'} |
| 23 | + alignItems={'end'} |
| 24 | + x={-1300} |
| 25 | + layout /> |
| 26 | + table_layout.add( |
| 27 | + <Txt |
| 28 | + text={"heap memory"} |
| 29 | + fontFamily={"Fira Mono"} |
| 30 | + alignSelf={'start'} |
| 31 | + fill={WHITE} |
| 32 | + padding={10} |
| 33 | + />) |
| 34 | + table_layout.add( |
| 35 | + <Rect |
| 36 | + width={550} |
| 37 | + height={80} |
| 38 | + clip={true} |
| 39 | + > |
| 40 | + <Grid |
| 41 | + ref={grid_ref} |
| 42 | + spacing={50} |
| 43 | + stroke={'#444'} |
| 44 | + lineWidth={4} |
| 45 | + start={0.3} |
| 46 | + end={0.3} |
| 47 | + width={550} |
| 48 | + height={130} |
| 49 | + cache |
| 50 | + /> |
| 51 | + </Rect>) |
| 52 | + table_layout.add( |
| 53 | + <Txt |
| 54 | + ref={address_ref} |
| 55 | + text={""} |
| 56 | + fontFamily={"Fira Mono"} |
| 57 | + alignSelf={'start'} |
| 58 | + fill={'#666'} |
| 59 | + fontSize={32} |
| 60 | + marginBottom={20} |
| 61 | + paddingLeft={70} |
| 62 | + padding={10} |
| 63 | + />) |
| 64 | + table_layout.add( |
| 65 | + <Txt |
| 66 | + text={"stack"} |
| 67 | + fontFamily={"Fira Mono"} |
| 68 | + alignSelf={'start'} |
| 69 | + fill={WHITE} |
| 70 | + padding={10} |
| 71 | + />) |
| 72 | + |
| 73 | + var stack_refs = [] |
| 74 | + var line_refs = [] |
| 75 | + |
| 76 | + for (let i = 4; i >= 0; i--) { |
| 77 | + const text_ref = createRef<Txt>(); |
| 78 | + const line_ref = createRef<Line>(); |
| 79 | + stack_refs.push(text_ref); |
| 80 | + line_refs.push(line_ref); |
| 81 | + table_layout.add( |
| 82 | + <Rect layout direction={'row'}> |
| 83 | + <Txt |
| 84 | + text={i == 4 ? '...' : String(i)} |
| 85 | + fontFamily={"Fira Mono"} |
| 86 | + fontSize={35} |
| 87 | + fill={'gray'} |
| 88 | + paddingRight={30} |
| 89 | + margin={10} |
| 90 | + /> |
| 91 | + <Rect layout direction={'column'}> |
| 92 | + <Code |
| 93 | + ref={text_ref} |
| 94 | + code={"⁉️"} |
| 95 | + fontFamily={"Fira Mono"} |
| 96 | + alignSelf={'center'} |
| 97 | + fontSize={35} |
| 98 | + fontWeight={500} |
| 99 | + margin={15} |
| 100 | + /> |
| 101 | + <Line |
| 102 | + ref={line_ref} |
| 103 | + stroke={'#777'} |
| 104 | + lineWidth={5} |
| 105 | + points={[[0, 0], [400, 0]]} |
| 106 | + /> |
| 107 | + </Rect> |
| 108 | + </Rect> |
| 109 | + |
| 110 | + ); |
| 111 | + } |
| 112 | + stack_refs.reverse(); |
| 113 | + |
| 114 | + const command_txt_ref = createRef<Txt>() |
| 115 | + |
| 116 | + table_layout.add( |
| 117 | + <Rect layout direction={'column'} alignSelf={'start'} minHeight={250}> |
| 118 | + <Txt |
| 119 | + text={"command:"} |
| 120 | + fontFamily={"Fira Mono"} |
| 121 | + fill={'#777'} |
| 122 | + marginTop={100} |
| 123 | + /> |
| 124 | + <Txt |
| 125 | + ref={command_txt_ref} |
| 126 | + text={""} |
| 127 | + fontFamily={"Fira Mono"} |
| 128 | + marginTop={10} |
| 129 | + marginLeft={100} |
| 130 | + fill={WHITE} |
| 131 | + /> |
| 132 | + </Rect>) |
| 133 | + |
| 134 | + view.add(table_layout) |
| 135 | + view.add(<Rect |
| 136 | + x={-700} |
| 137 | + y={-345} |
| 138 | + ref={data_rect_ref_1} |
| 139 | + stroke={BLUE} |
| 140 | + lineWidth={4} |
| 141 | + width={190} |
| 142 | + height={40} |
| 143 | + opacity={0.0} |
| 144 | + />) |
| 145 | + view.add(<Rect |
| 146 | + x={-500} |
| 147 | + y={-345} |
| 148 | + ref={data_rect_ref_2} |
| 149 | + stroke={BLUE} |
| 150 | + lineWidth={4} |
| 151 | + width={190} |
| 152 | + height={40} |
| 153 | + opacity={0.0} |
| 154 | + />) |
| 155 | + |
| 156 | + const code_heap = `\ |
| 157 | +#include <iostream> |
| 158 | +
|
| 159 | +int main() { |
| 160 | + int size = 2; |
| 161 | + int* ptr = nullptr; |
| 162 | + { |
| 163 | + // 😱 Don't use unprotected new and new[]! |
| 164 | + int* array = new int[size]; |
| 165 | + array[0] = 42; |
| 166 | + array[1] = 23; |
| 167 | + ptr = array; |
| 168 | + std::cout << "Before stack cleanup.\\n"; |
| 169 | + for (int i = 0; i < size; ++i) { |
| 170 | + std::cout << ptr[i] << std::endl; |
| 171 | + } |
| 172 | + } |
| 173 | + std::cout << "After stack cleanup.\\n"; |
| 174 | + for (int i = 0; i < size; ++i) { |
| 175 | + std::cout << ptr[i] << std::endl; |
| 176 | + } |
| 177 | + delete[] ptr; // 😱 What points to our data? |
| 178 | + return 0; |
| 179 | +}` |
| 180 | + const code_ref = createRef<Code>() |
| 181 | + |
| 182 | + yield view.add( |
| 183 | + <Code |
| 184 | + ref={code_ref} |
| 185 | + fontSize={35} |
| 186 | + fontFamily={'Fira Mono'} |
| 187 | + fontWeight={500} |
| 188 | + offsetX={-1} |
| 189 | + x={-200} |
| 190 | + />); |
| 191 | + |
| 192 | + const duration = 1.0 |
| 193 | + |
| 194 | + // Frame |
| 195 | + yield* all( |
| 196 | + code_ref().code(code_heap, duration), |
| 197 | + waitFor(duration) |
| 198 | + ); |
| 199 | + |
| 200 | + yield* all( |
| 201 | + table_layout.x(-600, duration), |
| 202 | + grid_ref().end(1, 2 * duration), |
| 203 | + grid_ref().start(0, 2 * duration), |
| 204 | + waitFor(3 * duration) |
| 205 | + ); |
| 206 | + |
| 207 | + // Frame |
| 208 | + yield* all( |
| 209 | + stack_refs[0]().code(`size = 2`, duration), |
| 210 | + code_ref().selection(lines(3), duration), |
| 211 | + command_txt_ref().text('push(int)', duration / 3), |
| 212 | + waitFor(3 * duration) |
| 213 | + ); |
| 214 | + |
| 215 | + // Frame |
| 216 | + yield* all( |
| 217 | + stack_refs[1]().code(`ptr = nullptr`, duration), |
| 218 | + code_ref().selection(lines(4), duration), |
| 219 | + command_txt_ref().text('', duration / 3).to('push(int*)', duration / 3), |
| 220 | + waitFor(3 * duration) |
| 221 | + ); |
| 222 | + |
| 223 | + // Frame |
| 224 | + yield* all( |
| 225 | + command_txt_ref().text('', duration / 3).to('malloc(8)', duration / 3), |
| 226 | + code_ref().selection(word(7, 16, 15), duration), |
| 227 | + sequence(0.1, |
| 228 | + address_ref().text('0x8eceb0', duration / 2), |
| 229 | + data_rect_ref_1().opacity(0.8, duration), |
| 230 | + data_rect_ref_2().opacity(0.8, duration), |
| 231 | + ), |
| 232 | + waitFor(3 * duration) |
| 233 | + ); |
| 234 | + |
| 235 | + // Frame |
| 236 | + yield* all( |
| 237 | + stack_refs[2]().code(`array = 0x8eceb0`, duration), |
| 238 | + command_txt_ref().text('', duration / 3).to('push(int*)', duration / 3), |
| 239 | + code_ref().selection(lines(7), duration), |
| 240 | + waitFor(3 * duration) |
| 241 | + ); |
| 242 | + |
| 243 | + // Frame |
| 244 | + yield* all( |
| 245 | + command_txt_ref().text('', duration / 3), |
| 246 | + code_ref().selection(lines(8), duration), |
| 247 | + data_rect_ref_1().fill(BLUE, duration), |
| 248 | + waitFor(3 * duration) |
| 249 | + ); |
| 250 | + |
| 251 | + // Frame |
| 252 | + yield* all( |
| 253 | + code_ref().selection(lines(9), duration), |
| 254 | + data_rect_ref_2().fill(BLUE, duration), |
| 255 | + waitFor(3 * duration) |
| 256 | + ); |
| 257 | + |
| 258 | + // Frame |
| 259 | + yield* all( |
| 260 | + stack_refs[1]().code(`ptr = 0x8eceb0`, duration), |
| 261 | + code_ref().selection(lines(10), duration), |
| 262 | + waitFor(3 * duration) |
| 263 | + ); |
| 264 | + |
| 265 | + // Frame |
| 266 | + yield* all( |
| 267 | + code_ref().selection(lines(11, 14), duration), |
| 268 | + waitFor(3 * duration) |
| 269 | + ); |
| 270 | + |
| 271 | + // Frame |
| 272 | + yield* all( |
| 273 | + sequence(0.1, |
| 274 | + stack_refs[2]().code('⁉️', duration)), |
| 275 | + code_ref().selection(lines(15), duration), |
| 276 | + command_txt_ref().text('', duration / 3).to('pop()', duration / 3), |
| 277 | + waitFor(3 * duration) |
| 278 | + ); |
| 279 | + |
| 280 | + // Frame |
| 281 | + yield* all( |
| 282 | + code_ref().selection(lines(16, 19), duration), |
| 283 | + command_txt_ref().text('', duration / 3), |
| 284 | + waitFor(3 * duration) |
| 285 | + ); |
| 286 | + |
| 287 | + // Frame |
| 288 | + yield* all( |
| 289 | + sequence(0.1, |
| 290 | + data_rect_ref_1().opacity(0, duration), |
| 291 | + data_rect_ref_2().opacity(0, duration)), |
| 292 | + code_ref().selection(lines(20), duration), |
| 293 | + waitFor(3 * duration) |
| 294 | + ); |
| 295 | + |
| 296 | + // Frame |
| 297 | + yield* all( |
| 298 | + sequence(0.1, |
| 299 | + stack_refs[1]().code('⁉️', duration), |
| 300 | + stack_refs[0]().code('⁉️', duration)), |
| 301 | + code_ref().selection(lines(22), duration), |
| 302 | + command_txt_ref().text('', duration / 3), |
| 303 | + waitFor(3 * duration) |
| 304 | + ); |
| 305 | + |
| 306 | + |
| 307 | + |
| 308 | + // Frame |
| 309 | + yield* all( |
| 310 | + code_ref().selection(DEFAULT, duration), |
| 311 | + command_txt_ref().text('', duration / 3), |
| 312 | + waitFor(3 * duration) |
| 313 | + ); |
| 314 | + |
| 315 | + yield* waitFor(3.0); |
| 316 | + |
| 317 | +}); |
0 commit comments