跳到主要内容

CodeBlock(已弃用)

CodeBlock 已被弃用,并将在第 4 版本中移除。请改用新的 Code 节点。

CodeBlock 组件用于展示语法高亮的代码。它可以自动高亮许多常见语言。随后可以就地修改这些代码,向观众展示变化。修改后的代码会以动画形式从先前代码过渡到新代码:移除旧代码、平移剩余代码、插入新代码。你还可以"选中"代码,以强调重要的代码片段。

使用组件​

要显示代码,请设置 code 和 language 属性。

import {CodeBlock} from '@motion-canvas/2d/lib/components/CodeBlock';

yield view.add(
<CodeBlock language="c#" code={`Console.WriteLine("Hello World!")`} />,
);

你可以在 Starry Night 仓库中找到可用语言的列表——该库用于语法高亮。默认语言是 tsx,即 Motion Canvas 所使用的语言,本指南的其余部分都将使用它。

请注意,与大多数组件不同,对于任何包含 CodeBlock 的 add 调用,你都必须使用 yield。这会提示 Motion Canvas 准备语法高亮器。

yield view.add(<CodeBlock />);

缩进​

为方便起见,当代码以新行开头时,代码的缩进将自动调整。

import {CodeBlock} from '@motion-canvas/2d/lib/components/CodeBlock';

yield view.add(
// note that the ` bracket is followed by a new line
<CodeBlock
code={`
console.log('Hello World!')
// more indented
// less indented`}
/>,
);
console.log('Hello World!');
// more indented
// less indented

缩进随后由缩进最少的代码行决定。

import {CodeBlock} from '@motion-canvas/2d/lib/components/CodeBlock';

yield view.add(
// note that the ` bracket is followed by a new line
<CodeBlock
code={`
console.log('Hello World!')
// less indented
// more indented`}
/>,
);
console.log('Hello World!');
// less indented
// more indented

选中​

可以"选中"代码以引起注意。默认行为是将未选中的文本去饱和。

使用辅助函数​

你可以借助三个辅助函数来定义选中范围:range、word 和 lines。

import {CodeBlock} from '@motion-canvas/2d/lib/components/CodeBlock';
import {createRef} from '@motion-canvas/core';

const codeBlockRef = createRef<CodeBlock>();
yield view.add(
<CodeBlock ref={codeBlockRef} code={`...`} selection={range(0, 2, 0, 4)} />,
);
// or
yield codeBlockRef.selection(range(0, 2, 0, 4));

range 需要 4 个参数:起始行、起始字符偏移量、结束行和结束字符偏移量。但请注意,所有值都是从零开始索引的。因此,如果你想在第一行选中一个长度为 5 个字符的单词,且它前面还有 4 个字符,则应使用如下调用:

range(
0, // starting on the first line
4, // starting with the 5th character
0, // ending on the first line
8, // ending with the 9th character
);

不过,针对这种特定用例,还有另一个辅助函数 word。它接受 2-3 个参数——起始行和字符偏移量,以及一个可选的单词长度。

前面使用 range 的示例可以用 word 辅助函数改写如下:

word(
0, // starting on the first line
4, // starting with the 5th character,
5, // the selection will have a total of 5 chars, can be ommited, which will select the remainder of this line
);

请注意,word 辅助函数只能按行应用。

最后,你可以使用 lines 函数选中整行代码。以下代码将选中第 5 行到第 10 行:

lines(
4, // starting line
9, // ending line, can be ommited, which will only select the starting line
);

你还可以使用展开运算符混合搭配这些辅助函数!

你可以使用以下表达式选中第 3 行,以及第 5 行中从第 6 个字符开始、长度为 10 个字符的单词:

yield * codeRef().selection([...lines(2), ...word(4, 5, 10)], 1);

如果你想撤销选中,可以选择从 0 到 Infinity 的所有行,或者像下面这样直接传入 DEFAULT 作为选中项:

import {DEFAULT} from '@motion-canvas/core';

// highlight lines 1 and 2
yield * codeRef().selection(lines(1, 2), 1);

// highlight all lines
yield * codeRef().selection(DEFAULT, 1);

直接使用​

在底层,这些辅助函数返回一个嵌套数组,该数组使用两个元组定义多个选中范围,每个元组定义一个选中项的行号和字符偏移量。

<CodeBlock
selection={[
[
// First selection
[lineFrom, characterFrom],
[lineTo, characterTo],
],
[
// Second selection
[lineFrom, characterFrom],
[lineTo, characterTo],
],
[
//... etc
],
]}
/>

例如,lines(4,7) 将返回如下结构:

[
[
[4, 0],
[7, Infinity],
],
];

代码动画​

你可以插入、删除或编辑所显示的代码,任何一种操作都会以动画形式将代码过渡到新状态。所有这些更改都通过 CodeBlock 实例上的 edit 方法完成。

要插入代码,首先使用 createRef 存储你的 CodeBlock 实例,以便后续编辑。然后调用 edit,并在其中嵌入 insert 调用来添加新代码。

import {CodeBlock, insert} from '@motion-canvas/2d/lib/components/CodeBlock';
import {createRef} from '@motion-canvas/core';

const codeRef = createRef<CodeBlock>();

yield view.add(<CodeBlock ref={codeRef} code={`var myBool;`} />);

// duration of 1.2 seconds
yield * codeRef().edit(1.2)`var myBool${insert(' = true')};`;
var myBool;
// will animate to
var myBool = true;

删除代码的过程类似,只不过所提供的代码会在动画过程中被移除。

import {CodeBlock, remove} from '@motion-canvas/2d/lib/components/CodeBlock';

yield view.add(<CodeBlock ref={codeRef} code={`var myBool = true;`} />);

yield * codeRef().edit(1.2)`var myBool${remove(' = true')};`;
var myBool = true;
// will animate to
var myBool;

最后,替换代码将删除和插入合并到一次调用中。

yield view.add(<CodeBlock ref={codeRef} code={`var myBool = true;`} />);

yield * codeRef().edit(1.2)`var myBool = ${edit('true', 'false')};`;
var myBool = true;
// will animate to
var myBool = false;

编辑代码会更新你的选中范围,以高亮显示更改。如果你希望在动画过程中保留选中范围,请使用 edit(duration, false)。

yield view.add(<CodeBlock ref={codeRef} code={`var myBool;`} />);

// note the second argument to edit
yield * codeRef().edit(1.2, false)`var myBool${insert(' = true')};`;

在一段视频的过程中,你可以对一个代码块应用多次编辑,每次都将先前的代码修改为新状态。

yield view.add(<CodeBlock ref={codeRef} code={`var myBool;`} />);

yield * codeRef().edit(1.2)`var myBool${insert(' = true')};`;
yield * waitFor(1);
yield * codeRef().edit(1.2)`var myBool = ${edit('true', 'false')};`;
yield * waitFor(1);
yield * codeRef().edit(1.2)`var myBool${remove(' = false')};`;

上述代码将依次呈现以下动画过程:

var myBool;
var myBool = true;
var myBool = false;
var myBool;

你还可以在一次 edit 中对代码块应用多项更改,使所有更改同时生效。

yield view.add(<CodeBlock ref={codeRef} code={`var myBool;`} />);

yield *
codeRef().edit(1.2)`${edit('var', 'const')} myBool${insert(' = true')};`;
var myBool;
// will animate to
const myBool = true;