Skip to content

Commit

Permalink
chore: add a manim example
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoiver committed Jan 16, 2025
1 parent 1f46dda commit b25e9d1
Show file tree
Hide file tree
Showing 26 changed files with 1,327 additions and 48 deletions.
6 changes: 5 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
module.exports = {
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended'],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'@vue-macros/eslint-config',
],

parser: 'vue-eslint-parser',
parserOptions: {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"@types/pngjs": "^6.0.1",
"@typescript-eslint/eslint-plugin": "^6.21.0",
"@typescript-eslint/parser": "^6.21.0",
"@vue-macros/eslint-config": "^0.7.1",
"case-police": "^0.5.14",
"eslint": "^7.32.0",
"eslint-plugin-jest": "24.3.6",
Expand Down Expand Up @@ -85,4 +86,4 @@
"esbuild": "0.23.1"
}
}
}
}
3 changes: 1 addition & 2 deletions packages/core/src/drawcalls/SDF.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,7 @@ export class SDF extends Drawcall {

if (!this.instanced && !this.#uniformBuffer) {
this.#uniformBuffer = this.device.createBuffer({
viewOrSize:
Float32Array.BYTES_PER_ELEMENT * (16 + 4 + 4 + 4 + 4 + 4 + 4),
viewOrSize: Float32Array.BYTES_PER_ELEMENT * (16 + 4 * 8),
usage: BufferUsage.UNIFORM,
hint: BufferFrequencyHint.DYNAMIC,
});
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/drawcalls/ShadowRect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export class ShadowRect extends Drawcall {

if (!this.instanced && !this.#uniformBuffer) {
this.#uniformBuffer = this.device.createBuffer({
viewOrSize: Float32Array.BYTES_PER_ELEMENT * (16 + 4 + 4 + 4),
viewOrSize: Float32Array.BYTES_PER_ELEMENT * (16 + 4 * 4),
usage: BufferUsage.UNIFORM,
hint: BufferFrequencyHint.DYNAMIC,
});
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/shaders/sdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,8 @@ void main() {
${wireframe_frag}
// outputColor = vec4(1.0, 0.0, 0.0, 1.0);
if (outputColor.a < epsilon)
discard;
}
Expand Down
40 changes: 40 additions & 0 deletions packages/core/src/shapes/mixins/Transformable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ export interface ITransformable {
get position(): ObservablePoint;
set position(value: IPointData);

get translateX(): number;
set translateX(value: number);

get translateY(): number;
set translateY(value: number);

/**
* The scale factors of this object along the local coordinate axes.
*
Expand All @@ -42,6 +48,12 @@ export interface ITransformable {
get scale(): ObservablePoint;
set scale(value: IPointData);

get scaleX(): number;
set scaleX(value: number);

get scaleY(): number;
set scaleY(value: number);

/**
* The pivot for the object in radians.
*/
Expand Down Expand Up @@ -81,13 +93,41 @@ export function Transformable<TBase extends GConstructor>(Base: TBase) {
this.transform.position.copyFrom(value);
}

get translateX(): number {
return this.transform.position.x;
}
set translateX(value: number) {
this.transform.position.x = value;
}

get translateY(): number {
return this.transform.position.y;
}
set translateY(value: number) {
this.transform.position.y = value;
}

get scale(): ObservablePoint {
return this.transform.scale;
}
set scale(value: IPointData) {
this.transform.scale.copyFrom(value);
}

get scaleX(): number {
return this.transform.scale.x;
}
set scaleX(value: number) {
this.transform.scale.x = value;
}

get scaleY(): number {
return this.transform.scale.y;
}
set scaleY(value: number) {
this.transform.scale.y = value;
}

/**
* The center of rotation, scaling, and skewing for this display object in its local space. The `position`
* is the projection of `pivot` in the parent's local space.
Expand Down
21 changes: 13 additions & 8 deletions packages/core/src/utils/glyph/tiny-sdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,20 @@ export class TinySDF {
);

if (w === 0 || h === 0) {
const width = w + 2 * this.buffer;
const height = h + 2 * this.buffer;

const len = Math.max(width * height, 0);
const data = new Uint8ClampedArray(len * 4);
return {
data: new Uint8Array(0),
width: 0,
height: 0,
glyphWidth: 0,
glyphHeight: 0,
glyphTop: 0,
glyphLeft: 0,
glyphAdvance: 0,
data,
width,
height,
glyphWidth: w,
glyphHeight: h,
glyphTop,
glyphLeft,
glyphAdvance,
};
}

Expand Down
51 changes: 46 additions & 5 deletions packages/core/src/utils/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,8 @@ export function typeofShape(
}

export async function deserializeNode(data: SerializedNode) {
const { type, attributes, children } = data;
// @ts-ignore
const { type, attributes, children, text } = data;
let shape: Shape;
if (type === 'g') {
shape = new Group();
Expand All @@ -277,10 +278,47 @@ export async function deserializeNode(data: SerializedNode) {
shape = new Rect();
} else if (type === 'polyline') {
shape = new Polyline();
// @ts-ignore
} else if (type === 'line') {
shape = new Polyline();
attributes.points = [
// @ts-ignore
[attributes.x1, attributes.y1],
// @ts-ignore
[attributes.x2, attributes.y2],
];
// @ts-ignore
delete attributes.x1;
// @ts-ignore
delete attributes.y1;
// @ts-ignore
delete attributes.x2;
// @ts-ignore
delete attributes.y2;
// @ts-ignore
} else if (type === 'polygon') {
shape = new Path();
// attributes.points: "0,150 100,150 100,50"
// convert to d attribute
// @ts-ignore
attributes.d = (attributes.points as string)
.split(' ')
.map((xy, i, points) => {
const [x, y] = xy.split(',').map(Number);
const command = i === 0 ? 'M' : 'L';
if (i === points.length - 1) {
return `${command} ${x},${y} Z`;
}
return `${command} ${x},${y}`;
})
.join(' ');
delete attributes.points;
} else if (type === 'path') {
shape = new Path();
} else if (type === 'text') {
shape = new Text();
// @ts-ignore
attributes.content = text;
} else if (type === 'rough-circle') {
shape = new RoughCircle();
// TODO: implement with path
Expand All @@ -294,7 +332,7 @@ export async function deserializeNode(data: SerializedNode) {
shape = new RoughPath();
}

const { transform, ...rest } = attributes;
let { transform, ...rest } = attributes;
Object.assign(shape, rest);

// create Image from DataURL
Expand All @@ -313,14 +351,16 @@ export async function deserializeNode(data: SerializedNode) {
}

if (transform) {
if (isString(transform)) {
transform = parseTransform(transform);
}

const { position, scale, skew, rotation, pivot } = transform;
shape.transform.position.set(position.x, position.y);
shape.transform.scale.set(scale.x, scale.y);
shape.transform.skew.set(skew.x, skew.y);
shape.transform.rotation = rotation;
shape.transform.pivot.set(pivot.x, pivot.y);

console.log(shape);
}

if (children && children.length > 0) {
Expand Down Expand Up @@ -879,7 +919,8 @@ export function fromSVGElement(element: SVGElement, uid = 0): SerializedNode {
attributeName === 'strokeDashoffset' ||
attributeName === 'fontSize'
) {
value = Number(value);
// remove 'px' suffix
value = Number(value.replace('px', ''));
} else if (attributeName === 'textAnchor') {
attributeName = 'textAlign';
if (value === 'middle') {
Expand Down
8 changes: 8 additions & 0 deletions packages/site/docs/.vitepress/config/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,14 @@ export const en = defineConfig({
text: 'Load web font',
link: 'web-font-loader',
},
{
text: 'Pythagorean theorem',
link: 'pythagorean-theorem',
},
{
text: 'Web Animations API',
link: 'web-animations-api',
},
],
},
],
Expand Down
8 changes: 8 additions & 0 deletions packages/site/docs/.vitepress/config/zh.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,14 @@ export const zh = defineConfig({
text: '加载 Web 字体',
link: 'web-font-loader',
},
{
text: '勾股定理',
link: 'pythagorean-theorem',
},
{
text: 'Web Animations API',
link: 'web-animations-api',
},
],
},
],
Expand Down
Loading

0 comments on commit b25e9d1

Please sign in to comment.