Skip to content

Commit 49e369a

Browse files
committed
feat: add assets and code video
1 parent 6e82072 commit 49e369a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+9591
-33
lines changed

.prettierrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
"printWidth": 80,
55
"trailingComma": "all",
66
"arrowParens": "avoid",
7-
"proseWrap": "always"
7+
"proseWrap": "always",
8+
"plugins": ["prettier-plugin-organize-imports"]
89
}

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ A set of videos made using [Motion Canvas](https://motioncanvas.io).
1616

1717
## Available examples
1818

19+
### Could This Asset Be Code
20+
21+
```shell
22+
npm run asset-code
23+
```
24+
25+
<a href="https://youtu.be/s1ZQnS_tOg0">
26+
<img alt="One Year of Motion Canvas" src="https://img.youtube.com/vi/s1ZQnS_tOg0/maxresdefault.jpg" width=480 />
27+
</a>
28+
1929
### One Year of Motion Canvas
2030

2131
```shell

examples/asset-code/package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "@examples/asset-code",
3+
"private": true,
4+
"description": "Source code for the Could This Asset Be Code video",
5+
"license": "MIT",
6+
"scripts": {
7+
"serve": "vite"
8+
}
9+
}
12.2 MB
Binary file not shown.
2.43 KB
Loading
Lines changed: 49 additions & 0 deletions
Loading
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/// <reference types="@motion-canvas/core/project" />
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import {Txt, withDefaults} from '@motion-canvas/2d';
2+
3+
export const ATxt = withDefaults(Txt, {
4+
fill: 'rgba(255, 255, 255, 0.6)',
5+
fontFamily: 'JetBrains Mono',
6+
fontWeight: 700,
7+
height: 40,
8+
lineHeight: 48,
9+
fontSize: 28,
10+
});
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import {
2+
drawImage,
3+
Img,
4+
ImgProps,
5+
initial,
6+
signal,
7+
vector2Signal,
8+
} from '@motion-canvas/2d';
9+
import {
10+
BBox,
11+
PossibleVector2,
12+
ReferenceReceiver,
13+
SignalValue,
14+
SimpleSignal,
15+
useThread,
16+
Vector2Signal,
17+
} from '@motion-canvas/core';
18+
19+
export interface AtlasProps extends ImgProps {
20+
grid?: SignalValue<PossibleVector2>;
21+
gridX?: SignalValue<number>;
22+
gridY?: SignalValue<number>;
23+
index?: SignalValue<number>;
24+
}
25+
26+
export function animate(
27+
from: number,
28+
length: number,
29+
duration: number = 0.1,
30+
ref?: ReferenceReceiver<Atlas>,
31+
): ReferenceReceiver<Atlas> {
32+
const time = useThread().time;
33+
return atlas => {
34+
ref?.(atlas);
35+
atlas.index(() => {
36+
const frames = Math.round(time() / duration);
37+
return from + (frames % length);
38+
});
39+
};
40+
}
41+
42+
export class Atlas extends Img {
43+
@initial(1)
44+
@vector2Signal('grid')
45+
public declare readonly grid: Vector2Signal<this>;
46+
47+
@initial(0)
48+
@signal()
49+
public declare readonly index: SimpleSignal<number, this>;
50+
51+
public constructor(props: AtlasProps) {
52+
super({
53+
smoothing: false,
54+
...props,
55+
});
56+
}
57+
58+
protected draw(context: CanvasRenderingContext2D) {
59+
this.drawShape(context);
60+
const alpha = this.alpha();
61+
if (alpha > 0) {
62+
const box = BBox.fromSizeCentered(this.computedSize());
63+
context.save();
64+
context.clip(this.getPath());
65+
if (alpha < 1) {
66+
context.globalAlpha *= alpha;
67+
}
68+
context.imageSmoothingEnabled = this.smoothing();
69+
const image = this.image();
70+
const grid = this.grid();
71+
const width = image.naturalWidth / grid.x;
72+
const height = image.naturalHeight / grid.y;
73+
const index = this.index();
74+
const x = index % grid.x;
75+
const y = Math.floor(index / grid.x);
76+
const source = new BBox(x * width, y * height, width, height);
77+
78+
drawImage(context, this.image(), source, box);
79+
context.restore();
80+
}
81+
82+
if (this.clip()) {
83+
context.clip(this.getPath());
84+
}
85+
86+
this.drawChildren(context);
87+
}
88+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import {initial, Shape, ShapeProps, vector2Signal} from '@motion-canvas/2d';
2+
import {
3+
clampRemap,
4+
createSignal,
5+
loop,
6+
PossibleVector2,
7+
Random,
8+
SignalValue,
9+
spawn,
10+
usePlayback,
11+
Vector2,
12+
Vector2Signal,
13+
} from '@motion-canvas/core';
14+
15+
export interface BinaryProps extends ShapeProps {
16+
reveal: SignalValue<PossibleVector2>;
17+
}
18+
19+
export class Binary extends Shape {
20+
@initial(0)
21+
@vector2Signal()
22+
public declare readonly reveal: Vector2Signal<this>;
23+
24+
private progress = createSignal(0);
25+
26+
public constructor(props: BinaryProps) {
27+
super(props);
28+
spawn(
29+
loop(() => {
30+
this.progress(this.progress() + usePlayback().deltaTime);
31+
}),
32+
);
33+
}
34+
35+
protected draw(context: CanvasRenderingContext2D) {
36+
this.requestFontUpdate();
37+
38+
context.save();
39+
this.applyStyle(context);
40+
this.applyText(context);
41+
context.font = this.styles.font;
42+
context.textBaseline = 'top';
43+
const size = this.computedSize();
44+
context.translate(size.x / -2, size.y / -2);
45+
const char = new Vector2(50, 80);
46+
const steps = size.div(char).ceiled;
47+
const random = new Random(1);
48+
const progress = this.progress();
49+
const reveal = this.reveal();
50+
for (let x = -1; x <= steps.x; x++) {
51+
const array = random.floatArray(steps.y);
52+
for (let y = -1; y <= steps.y; y++) {
53+
const cycles = Math.floor(progress);
54+
let zero: number;
55+
let charY: number;
56+
charY = char.y * (y + 1 - (progress % 1));
57+
zero = array[(y + cycles) % steps.y] > 0.5 ? 1 : 0;
58+
const position = new Vector2(x, y + 1)
59+
.sub(steps.div(2))
60+
.div(steps.x).magnitude;
61+
// context.globalAlpha = ((y + 1) / steps.y) > reveal.y ? 0 : 1;
62+
// context.globalAlpha *= x / steps.x > reveal.x ? 0 : 1;
63+
context.globalAlpha = clampRemap(
64+
-0.05,
65+
0.05,
66+
0,
67+
1,
68+
reveal.x - position,
69+
);
70+
context.fillText(zero > 0.5 ? '1' : '0', char.x * x, charY);
71+
}
72+
}
73+
74+
context.restore();
75+
}
76+
}

0 commit comments

Comments
 (0)