🎥 Make videos programmatically with React
progress
Jonny Burgerbad2f3b76404
diff --git a/packages/docs/src/components/DoMoreHero/3d-svg.ts b/packages/docs/src/components/DoMoreHero/3d-svg.tsnew file mode 100644index 0000000000..2a9656351a--- /dev/null+++ b/packages/docs/src/components/DoMoreHero/3d-svg.ts@@ -0,0 +1,59 @@+import type { Vector4D } from "./matrix";++export type ThreeDReducedInstruction =+ | {+ type: "M";+ point: Vector4D;+ }+ | {+ type: "L";+ point: Vector4D;+ }+ | {+ type: "C";+ cp1: Vector4D;+ cp2: Vector4D;+ point: Vector4D;+ }+ | {+ type: "Q";+ cp: Vector4D;+ point: Vector4D;+ }+ | {+ type: "Z";+ point: Vector4D;+ };++const serializeThreeDReducedInstruction = (+ instruction: ThreeDReducedInstruction+): string => {+ if (instruction.type === "M") {+ return `M ${instruction.point[0]} ${instruction.point[1]}`;+ }++ if (instruction.type === "L") {+ return `L ${instruction.point[0]} ${instruction.point[1]}`;+ }++ if (instruction.type === "C") {+ return `C ${instruction.cp1[0]} ${instruction.cp1[1]} ${instruction.cp2[0]} ${instruction.cp2[1]} ${instruction.point[0]} ${instruction.point[1]}`;+ }++ if (instruction.type === "Q") {+ return `Q ${instruction.cp[0]} ${instruction.cp[1]} ${instruction.point[0]} ${instruction.point[1]}`;+ }++ if (instruction.type === "Z") {+ return "Z";+ }++ throw new Error("Unknown instruction type");+};++export const threeDIntoSvgPath = (+ instructions: ThreeDReducedInstruction[]+): string =>+ instructions+ .map((instruction) => serializeThreeDReducedInstruction(instruction))+ .join(" ");diff --git a/packages/docs/src/components/DoMoreHero/DoMoreHero.tsx b/packages/docs/src/components/DoMoreHero/DoMoreHero.tsxnew file mode 100644index 0000000000..e74f0392ab--- /dev/null+++ b/packages/docs/src/components/DoMoreHero/DoMoreHero.tsx@@ -0,0 +1,179 @@+import { useColorMode } from "@docusaurus/theme-common";+import { getBoundingBox, parsePath, resetPath } from "@remotion/paths";+import { Player } from "@remotion/player";+import { makeTriangle } from "@remotion/shapes";+import React from "react";+import {+ AbsoluteFill,+ interpolate,+ spring,+ useCurrentFrame,+ useVideoConfig,+} from "remotion";+import { transformElement } from "./element";+import { Faces } from "./Faces";+import { extrudeElement } from "./join-inbetween-tiles";+import {+ rotateX,+ rotateY,+ rotateZ,+ scaled,+ translateX,+ translateY,+ translateZ,+} from "./matrix";++const InFrameLogo: React.FC<{+ background: string;+}> = ({ background }) => {+ const { width: bBoxWidth, height: bBoxHeight, fps } = useVideoConfig();+ const viewBox = [-bBoxWidth / 2, -bBoxHeight / 2, bBoxWidth, bBoxHeight].join(+ " "+ );+ const frame = useCurrentFrame();++ const zoomIn = (Math.sin(frame / 100) + 1) * 0.2;++ const delayedFrame = Math.max(frame - 80, 0);++ const paths = new Array(3).fill(true).map((_, i) => {+ const triangle = makeTriangle({+ direction: "right",+ length: 1500 + i * 675,+ edgeRoundness: 0.71,+ });+ const path = resetPath(triangle.path);+ const parsed = parsePath(path);++ const boundingBox = getBoundingBox(path);+ const width = boundingBox.x2 - boundingBox.x1;+ const height = boundingBox.y2 - boundingBox.y1;++ const depth = 20 * 7.5;+ const spread =+ depth ++ (Math.sin(frame / 200) ++ (1 ++ spring({+ frame,+ fps,+ config: {+ damping: 200,+ },+ durationInFrames: 200,+ delay: 80,+ }))) *+ 500;++ const color = i === 2 ? "#E9F3FD" : i === 1 ? "#C1DBF9" : "#0b84f3";++ const extruded = extrudeElement({+ backFaceColor: color,+ sideColor: "black",+ frontFaceColor: color,+ depth,+ points: parsed,+ strokeWidth: 20,+ description: `triangle-${i}`,+ strokeColor: "black",+ });+ const projected = transformElement(extruded, [+ translateZ(spread * i - spread),+ translateX(-width / 2),+ translateY(-height / 2 + 20),+ rotateX(-(i * delayedFrame) / 300),+ rotateY(delayedFrame / 100),+ rotateZ(delayedFrame / 100),+ scaled(0.3 + zoomIn),+ ]);++ return projected;+ });++ return (+ <AbsoluteFill+ style={{+ backgroundColor: background,+ }}+ >+ <svg+ viewBox={viewBox}+ style={{+ overflow: "visible",+ opacity: interpolate(frame, [0, 70], [0, 1]),+ }}+ >+ <Faces elements={paths.reverse()} />+ </svg>+ </AbsoluteFill>+ );+};++const gradientSteps = [+ 0, 0.013, 0.049, 0.104, 0.175, 0.259, 0.352, 0.45, 0.55, 0.648, 0.741, 0.825,+ 0.896, 0.951, 0.987,+];++const gradientOpacities = [+ 0, 8.1, 15.5, 22.5, 29, 35.3, 41.2, 47.1, 52.9, 58.8, 64.7, 71, 77.5, 84.5,+ 91.9,+];++const globalGradientOpacity = 1;++const inputProps = {};++export const DoMoreHero: React.FC = () => {+ const { colorMode } = useColorMode();++ const targetColor =+ colorMode === "dark" ? "hsl(240deg, 4%, 11%)" : "hsl(0, 0%, 100%)";++ const steps =+ colorMode === "dark"+ ? gradientSteps+ .map((g, i) => {+ return `hsla(240deg, 4%, 11%, ${g}) ${+ gradientOpacities[i] * globalGradientOpacity+ }%`;+ })+ .join(", ")+ : gradientSteps+ .map((g, i) => {+ return `hsla(0, 0%, 100%, ${g}) ${+ gradientOpacities[i] * globalGradientOpacity+ }%`;+ })+ .join(", ");++ return (+ <div+ style={{+ position: "relative",+ }}+ >+ <Player+ component={InFrameLogo}+ compositionWidth={1920 * 2}+ compositionHeight={1080 * 1.4}+ durationInFrames={100000}+ fps={30}+ loop+ autoPlay+ style={{+ width: "100%",+ }}+ inputProps={inputProps}+ />+ <div+ style={{+ height: "20vw",+ marginTop: "-20vw",+ width: "100%",+ position: "absolute",+ backgroundImage: `linear-gradient(to bottom, ${steps}, ${targetColor} 100%)`,+ }}+ />+ </div>+ );+};diff --git a/packages/docs/src/components/DoMoreHero/Face.tsx b/packages/docs/src/components/DoMoreHero/Face.tsxnew file mode 100644index 0000000000..fa6de145b9--- /dev/null+++ b/packages/docs/src/components/DoMoreHero/Face.tsx@@ -0,0 +1,57 @@+import React, { useState } from "react";+import { random } from "remotion";+import type { ThreeDReducedInstruction } from "./3d-svg";+import { threeDIntoSvgPath } from "./3d-svg";++export type FaceSVGProps = {+ strokeLinecap?: React.SVGAttributes<SVGPathElement>["strokeLinecap"];+ strokeMiterlimit?: React.SVGAttributes<SVGPathElement>["strokeMiterlimit"];+};++export const Face: React.FC<+ {+ points: ThreeDReducedInstruction[];+ color: string;+ strokeColor: string;+ strokeWidth: number;+ } & FaceSVGProps+> = ({+ color,+ points,+ strokeColor,+ strokeWidth,+ strokeMiterlimit,+ strokeLinecap,+}) => {+ const [id] = useState(() => random(null).toString().replace(".", ""));+ const d = threeDIntoSvgPath(points);++ return (+ <>+ <defs>+ {strokeWidth ? (+ <mask id={id}>+ <path+ strokeLinecap={strokeLinecap}+ shapeRendering="crispEdges"+ strokeMiterlimit={strokeMiterlimit}+ strokeWidth={strokeWidth}+ d={d}+ fill="white"+ />+ </mask>+ ) : null}+ </defs>+ <path+ d={d}+ fill={color}+ mask={strokeWidth ? `url(#${id})` : undefined}+ stroke={strokeColor}+ strokeMiterlimit={strokeMiterlimit}+ shapeRendering="crispEdges"+ strokeLinecap={strokeLinecap}+ strokeWidth={strokeWidth}+ />+ </>+ );+};diff --git a/packages/docs/src/components/DoMoreHero/Faces.tsx b/packages/docs/src/components/DoMoreHero/Faces.tsxnew file mode 100644index 0000000000..bf704c942d--- /dev/null+++ b/packages/docs/src/components/DoMoreHero/Faces.tsx@@ -0,0 +1,51 @@+import React from "react";+import type { ThreeDElement } from "./element";+import type { FaceSVGProps } from "./Face";+import { Face } from "./Face";+import type { FaceType } from "./map-face";++const sortFacesZIndex = (face: FaceType[]): FaceType[] => {+ return face.slice().sort((a, b) => {+ return b.centerPoint[2] - a.centerPoint[2];+ });+};++export const Faces: React.FC<+ {+ elements: ThreeDElement[];+ noSort?: boolean;+ } & FaceSVGProps+> = ({ elements, noSort, ...svgProps }) => {+ const sortedElement = noSort+ ? elements+ : elements.sort((a, b) => {+ return b.centerPoint[2] - a.centerPoint[2];+ });++ return (+ <>+ {sortedElement.map((element, i) => {+ const sortedFaces = sortFacesZIndex(element.faces);++ return (+ <React.Fragment key={i}>+ {sortedFaces.map(+ ({ points, color, strokeWidth, strokeColor }, i) => {+ return (+ <Face+ key={JSON.stringify(points) + i}+ strokeColor={strokeColor}+ color={color}+ points={points}+ strokeWidth={strokeWidth}+ {...svgProps}+ />+ );+ }+ )}+ </React.Fragment>+ );+ })}+ </>+ );+};diff --git a/packages/docs/src/components/DoMoreHero/element.ts b/packages/docs/src/components/DoMoreHero/element.tsnew file mode 100644index 0000000000..b5812557a5--- /dev/null+++ b/packages/docs/src/components/DoMoreHero/element.ts@@ -0,0 +1,42 @@+import { makeId } from "./make-id";+import type { FaceType } from "./map-face";+import { transformFace } from "./map-face";+import type { MatrixTransform4D, Vector4D } from "./matrix";+import { multiplyMatrix } from "./matrix";++export type ThreeDElement = {+ faces: FaceType[];+ id: string;+ description: string;+ centerPoint: Vector4D;+};++export const makeElement = (+ face: FaceType | FaceType[],+ centerPoint: Vector4D,+ description: string+): ThreeDElement => {+ return {+ faces: Array.isArray(face) ? face : [face],+ id: makeId(),+ centerPoint,+ description,+ };+};++export const transformElement = (+ element: ThreeDElement,+ transformations: MatrixTransform4D[]+): ThreeDElement => {+ return {+ ...element,+ faces: element.faces.map((face) => {+ return transformFace(face, transformations);+ }),+ id: makeId(),+ centerPoint: transformations.reduce(+ (point, transformation) => multiplyMatrix(transformation, point),+ element.centerPoint+ ),+ };+};diff --git a/packages/docs/src/components/DoMoreHero/fix-z.ts b/packages/docs/src/components/DoMoreHero/fix-z.tsnew file mode 100644index 0000000000..ae0d5eaf36--- /dev/null+++ b/packages/docs/src/components/DoMoreHero/fix-z.ts@@ -0,0 +1,51 @@+import type { Instruction } from "@remotion/paths";+import { reduceInstructions } from "@remotion/paths";+import type { ThreeDReducedInstruction } from "./3d-svg";+import type { Vector4D } from "./matrix";++export const turnInto3D = (+ instructions: Instruction[]+): ThreeDReducedInstruction[] => {+ let lastMove: Vector4D = [0, 0, 0, 1];+ const newInstructions: ThreeDReducedInstruction[] = [];+ const reduced = reduceInstructions(instructions);++ for (let i = 0; i < reduced.length; i++) {+ const instruction = reduced[i];++ if (instruction.type === "Z") {+ newInstructions.push({+ type: "Z",+ point: lastMove,+ });+ } else if (instruction.type === "M") {+ lastMove = [instruction.x, instruction.y, 0, 1];+ newInstructions.push({+ point: [instruction.x, instruction.y, 0, 1],+ type: "M",+ });+ } else if (instruction.type === "L") {+ newInstructions.push({+ type: "L",+ point: [instruction.x, instruction.y, 0, 1],+ });+ } else if (instruction.type === "C") {+ newInstructions.push({+ type: "C",+ point: [instruction.x, instruction.y, 0, 1],+ cp1: [instruction.cp1x, instruction.cp1y, 0, 1],+ cp2: [instruction.cp2x, instruction.cp2y, 0, 1],+ });+ } else if (instruction.type === "Q") {+ newInstructions.push({+ type: "Q",+ cp: [instruction.cpx, instruction.cpy, 0, 1],+ point: [instruction.x, instruction.y, 0, 1],+ });+ } else {+ throw new Error("unknown");+ }+ }++ return newInstructions;+};diff --git a/packages/docs/src/components/DoMoreHero/join-inbetween-tiles.ts b/packages/docs/src/components/DoMoreHero/join-inbetween-tiles.tsnew file mode 100644index 0000000000..f78ccba842--- /dev/null+++ b/packages/docs/src/components/DoMoreHero/join-inbetween-tiles.ts@@ -0,0 +1,173 @@+import type { Instruction } from "@remotion/paths";+import { reduceInstructions } from "@remotion/paths";+import { getBoundingBoxFromInstructions } from "@remotion/paths/dist/get-bounding-box";+import type { ThreeDReducedInstruction } from "./3d-svg";+import type { ThreeDElement } from "./element";+import { makeElement } from "./element";+import { turnInto3D } from "./fix-z";+import type { FaceType } from "./map-face";+import { transformFace, translateSvgInstruction } from "./map-face";+import type { Vector4D } from "./matrix";+import { translateZ } from "./matrix";+import { subdivideInstructions } from "./subdivide-instruction";+import { truthy } from "./truthy";++export const extrudeElement = ({+ depth,+ sideColor,+ frontFaceColor,+ backFaceColor,+ points,+ strokeWidth,+ description,+ strokeColor,+}: {+ depth: number;+ sideColor: string;+ frontFaceColor: string;+ backFaceColor: string;+ points: Instruction[];+ strokeWidth: number;+ description: string;+ strokeColor: string;+}): ThreeDElement => {+ const boundingBox = getBoundingBoxFromInstructions(+ reduceInstructions(points)+ );++ const centerX = (boundingBox.x2 - boundingBox.x1) / 2 + boundingBox.x1;+ const centerY = (boundingBox.y2 - boundingBox.y1) / 2 + boundingBox.y1;++ const threeD = turnInto3D(points);+ const instructions: FaceType = {+ centerPoint: [0, 0, 0, 1],+ points: subdivideInstructions(+ subdivideInstructions(subdivideInstructions(threeD))+ ),+ strokeWidth,+ strokeColor,+ color: "black",+ description,+ };++ const unscaledBackFace = transformFace(instructions, [translateZ(depth / 2)]);+ const unscaledFrontFace = transformFace(instructions, [+ translateZ(-depth / 2),+ ]);++ const inbetween = unscaledBackFace.points.map((t, i): FaceType => {+ const nextInstruction =+ i === unscaledBackFace.points.length - 1+ ? unscaledBackFace.points[0]+ : unscaledBackFace.points[i + 1];++ const currentPoint = t.point;+ const nextPoint = nextInstruction.point;+ const movingOver: Vector4D = [+ nextPoint[0],+ nextPoint[1],+ nextPoint[2] - depth,+ nextPoint[3],+ ];++ const translatedInstruction = translateSvgInstruction(+ inverseInstruction(nextInstruction, currentPoint),+ 0,+ 0,+ -depth+ );+ const newInstructions: ThreeDReducedInstruction[] = [+ {+ type: "M" as const,+ point: currentPoint,+ },+ nextInstruction,+ nextInstruction.type === "Z"+ ? {+ type: "M" as const,+ point: nextInstruction.point,+ }+ : null,+ {+ type: "L" as const,+ point: movingOver,+ },+ translatedInstruction,+ {+ type: "L" as const,+ point: currentPoint,+ },+ ].filter(truthy);++ return {+ points: newInstructions,+ color: sideColor,+ centerPoint: [0, 0, 0, 1],+ strokeWidth: 0,+ strokeColor: "black",+ description: description + "inbetween",+ };+ });++ const scaledFrontFace: FaceType = {+ ...unscaledFrontFace,+ color: frontFaceColor,+ description: description + "(front)",+ };+ const scaledBackFace: FaceType = {+ ...unscaledBackFace,+ color: backFaceColor,+ description: description + "(back)",+ };++ return makeElement(+ [...inbetween, scaledFrontFace, scaledBackFace],+ [centerX, centerY, 0, 1],+ description+ );+};++const inverseInstruction = (+ instruction: ThreeDReducedInstruction,+ comingFrom: Vector4D+): ThreeDReducedInstruction => {+ if (instruction.type === "M") {+ return {+ type: "M",+ point: comingFrom,+ };+ }++ if (instruction.type === "L") {+ return {+ type: "L",+ point: comingFrom,+ };+ }++ if (instruction.type === "C") {+ return {+ type: "C",+ point: comingFrom,+ cp1: instruction.cp2,+ cp2: instruction.cp1,+ };+ }++ if (instruction.type === "Q") {+ return {+ type: "Q",+ point: comingFrom,+ cp: instruction.cp,+ };+ }++ if (instruction.type === "Z") {+ return {+ type: "L",+ point: comingFrom,+ };+ }++ throw new Error("Unknown instruction type");+};diff --git a/packages/docs/src/components/DoMoreHero/make-id.ts b/packages/docs/src/components/DoMoreHero/make-id.tsnew file mode 100644index 0000000000..4146b06402--- /dev/null+++ b/packages/docs/src/components/DoMoreHero/make-id.ts@@ -0,0 +1,5 @@+import { random } from "remotion";++export const makeId = () => {+ return random(null).toString().replace(".", "");+};diff --git a/packages/docs/src/components/DoMoreHero/map-face.ts b/packages/docs/src/components/DoMoreHero/map-face.tsnew file mode 100644index 0000000000..72dfe59c4f--- /dev/null+++ b/packages/docs/src/components/DoMoreHero/map-face.ts@@ -0,0 +1,167 @@+import { parsePath } from "@remotion/paths";+import type { ThreeDReducedInstruction } from "./3d-svg";+import type { ThreeDElement } from "./element";+import { transformElement } from "./element";+import { turnInto3D } from "./fix-z";+import type { MatrixTransform4D, Vector4D } from "./matrix";+import { multiplyMatrix, multiplyMatrixAndSvgInstruction } from "./matrix";++export type FaceType = {+ color: string;+ points: ThreeDReducedInstruction[];+ centerPoint: Vector4D;+ strokeWidth: number;+ strokeColor: string;+ description: string;+};++export const translateSvgInstruction = (+ instruction: ThreeDReducedInstruction,+ x: number,+ y: number,+ z: number+): ThreeDReducedInstruction => {+ if (instruction.type === "M") {+ return {+ type: "M",+ point: [+ instruction.point[0] + x,+ instruction.point[1] + y,+ instruction.point[2] + z,+ instruction.point[3],+ ],+ };+ }++ if (instruction.type === "L") {+ return {+ type: "L",+ point: [+ instruction.point[0] + x,+ instruction.point[1] + y,+ instruction.point[2] + z,+ instruction.point[3],+ ],+ };+ }++ if (instruction.type === "C") {+ return {+ type: "C",+ point: [+ instruction.point[0] + x,+ instruction.point[1] + y,+ instruction.point[2] + z,+ instruction.point[3],+ ],+ cp1: [+ instruction.cp1[0] + x,+ instruction.cp1[1] + y,+ instruction.cp1[2] + z,+ instruction.cp1[3],+ ],+ cp2: [+ instruction.cp2[0] + x,+ instruction.cp2[1] + y,+ instruction.cp2[2] + z,+ instruction.cp2[3],+ ],+ };+ }++ if (instruction.type === "Q") {+ return {+ type: "Q",+ point: [+ instruction.point[0] + x,+ instruction.point[1] + y,+ instruction.point[2] + z,+ instruction.point[3],+ ],+ cp: [+ instruction.cp[0] + x,+ instruction.cp[1] + y,+ instruction.cp[2] + z,+ instruction.cp[3],+ ],+ };+ }++ if (instruction.type === "Z") {+ return {+ type: "Z",+ point: [+ instruction.point[0] + x,+ instruction.point[1] + y,+ instruction.point[2] + z,+ instruction.point[3],+ ],+ };+ }++ throw new Error("Unknown instruction type: " + JSON.stringify(instruction));+};++export const transformFace = (+ face: FaceType,+ transformations: MatrixTransform4D[]+): FaceType => {+ return {+ ...face,+ points: face.points.map((p) => {+ return transformations.reduce((acc, t) => {+ return multiplyMatrixAndSvgInstruction(t, acc);+ }, p);+ }),+ centerPoint: transformations.reduce((acc, t) => {+ const result = multiplyMatrix(t, acc);+ return result;+ }, face.centerPoint),+ };+};++export const transformFaces = ({+ faces,+ transformations,+}: {+ faces: FaceType[];+ transformations: MatrixTransform4D[];+}) => {+ return faces.map((face) => {+ return transformFace(face, transformations);+ });+};++export const transformElements = (+ elements: ThreeDElement[],+ transformations: MatrixTransform4D[]+) => {+ return elements.map((element) => {+ return transformElement(element, transformations);+ });+};++export const makeFace = ({+ points,+ strokeWidth,+ strokeColor,+ fill,+ description,+}: {+ points: string | ThreeDReducedInstruction[];+ strokeWidth: number;+ strokeColor: string;+ fill: string;+ description: string;+}): FaceType => {+ const centerPoint: Vector4D = [0, 0, 0, 1];++ return {+ centerPoint,+ color: fill,+ points: typeof points === "string" ? turnInto3D(parsePath(points)) : points,+ strokeWidth,+ strokeColor,+ description,+ };+};diff --git a/packages/docs/src/components/DoMoreHero/matrix.ts b/packages/docs/src/components/DoMoreHero/matrix.tsnew file mode 100644index 0000000000..896530d16e--- /dev/null+++ b/packages/docs/src/components/DoMoreHero/matrix.ts@@ -0,0 +1,312 @@+import type { ThreeDReducedInstruction } from "./3d-svg";++export const translated4d = function (vec: Vector) {+ return stride({ v: vec, m: identity4(), width: 4, offset: 3, colStride: 0 });+};++export const stride = function ({+ v,+ m,+ width,+ offset,+ colStride,+}: {+ v: Vector;+ m: MatrixTransform4D;+ width: number;+ offset: number;+ colStride: number;+}) {+ for (let i = 0; i < v.length; i++) {+ m[+ i * width + // Column+ ((i * colStride + offset + width) % width) // Row+ ] = v[i];+ }++ return m;+};++export const identity4 = function (): MatrixTransform4D {+ const n = 4;+ let size = n * n;+ const m = new Array(size) as MatrixTransform4D;+ while (size--) {+ m[size] = size % (n + 1) === 0 ? 1.0 : 0.0;+ }++ return m;+};++export const m44multiply = (+ ...matrices: MatrixTransform4D[]+): MatrixTransform4D => {+ return multiplyMany(4, matrices);+};++// Accept an integer indicating the size of the matrices being multiplied (3 for 3x3), and any+// number of matrices following it.+function multiplyMany(+ size: number,+ listOfMatrices: MatrixTransform4D[]+): MatrixTransform4D {+ if (listOfMatrices.length < 2) {+ throw new Error("multiplication expected two or more matrices");+ }++ let result = mul(listOfMatrices[0], listOfMatrices[1], size);+ let next = 2;+ while (next < listOfMatrices.length) {+ result = mul(result, listOfMatrices[next], size);+ next++;+ }++ return result as MatrixTransform4D;+}++function mul(m1: number[], m2: number[], size: number): number[] {+ if (m1.length !== m2.length) {+ throw new Error(+ `Undefined for matrices of different sizes. m1.length=${m1.length}, m2.length=${m2.length}`+ );+ }++ if (size * size !== m1.length) {+ throw new Error(+ `Undefined for non-square matrices. array size was ${size}`+ );+ }++ const result = Array(m1.length);+ for (let r = 0; r < size; r++) {+ for (let c = 0; c < size; c++) {+ // Accumulate a sum of m1[r,k]*m2[k, c]+ let acc = 0;+ for (let k = 0; k < size; k++) {+ acc += m1[size * r + k] * m2[size * k + c];+ }++ result[r * size + c] = acc;+ }+ }++ return result;+}++export type Vector2D = [number, number];+export type Vector = [number, number, number];+export type Vector4D = [number, number, number, number];++const rotated = function (axisVec: Vector, radians: number): MatrixTransform4D {+ return rotatedUnitSinCos(+ normalize(axisVec),+ Math.sin(radians),+ Math.cos(radians)+ );+};++export const rotateX = (radians: number) => {+ return rotated([1, 0, 0], radians);+};++export const rotateY = (radians: number) => {+ return rotated([0, 1, 0], radians);+};++export const rotateZ = (radians: number) => {+ return rotated([0, 0, 1], radians);+};++export type MatrixTransform4D = [+ number,+ number,+ number,+ number,+ number,+ number,+ number,+ number,+ number,+ number,+ number,+ number,+ number,+ number,+ number,+ number+];++export const scaled = function (value: number | Vector) {+ const vec: Vector = typeof value === "number" ? [value, value, value] : value;+ return stride({ v: vec, m: identity4(), width: 4, offset: 0, colStride: 1 });+};++const rotatedUnitSinCos = function (+ axisVec: Vector,+ sinAngle: number,+ cosAngle: number+): MatrixTransform4D {+ const x = axisVec[0];+ const y = axisVec[1];+ const z = axisVec[2];+ const c = cosAngle;+ const s = sinAngle;+ const t = 1 - c;+ return [+ t * x * x + c,+ t * x * y - s * z,+ t * x * z + s * y,+ 0,+ t * x * y + s * z,+ t * y * y + c,+ t * y * z - s * x,+ 0,+ t * x * z - s * y,+ t * y * z + s * x,+ t * z * z + c,+ 0,+ 0,+ 0,+ 0,+ 1,+ ];+};++export const normalize = function (v: Vector): Vector {+ return mulScalar(v, 1 / vectorLength(v));+};++export const normalize4d = function (v: Vector4D): Vector4D {+ return mulScalar(v, 1 / vectorLength(v));+};++const vectorLength = function (v: number[]) {+ return Math.sqrt(lengthSquared(v));+};++const lengthSquared = function (v: number[]) {+ return dot(v, v);+};++export const dot = function (a: number[], b: number[]) {+ if (a.length !== b.length) {+ throw new Error(+ `Cannot perform dot product on arrays of different length (${a.length} vs ${b.length})`+ );+ }++ return a+ .map((v, i) => {+ return v * b[i];+ })+ .reduce((acc, cur) => {+ return acc + cur;+ });+};++const translated = function (vec: Vector) {+ return stride({+ v: vec,+ m: identity4(),+ width: 4,+ offset: 3,+ colStride: 0,+ });+};++export const translateX = (x: number) => {+ return translated([x, 0, 0]);+};++export const translateY = (y: number) => {+ return translated([0, y, 0]);+};++export const translateZ = (z: number) => {+ return translated([0, 0, z]);+};++export type Camera = {+ near: number;+ far: number;+ angle: number;+ eye: Vector;+ coa: Vector;+ up: Vector;+};++export const mulScalar = function <T extends number[]>(v: T, s: number): T {+ return v.map((i) => {+ return i * s;+ }) as T;+};++export function multiplyMatrixAndSvgInstruction(+ matrix: MatrixTransform4D,+ point: ThreeDReducedInstruction+): ThreeDReducedInstruction {+ if (point.type === "C") {+ return {+ type: "C",+ cp1: multiplyMatrix(matrix, point.cp1),+ cp2: multiplyMatrix(matrix, point.cp2),+ point: multiplyMatrix(matrix, point.point),+ };+ }++ if (point.type === "Q") {+ return {+ type: "Q",+ cp: multiplyMatrix(matrix, point.cp),+ point: multiplyMatrix(matrix, point.point),+ };+ }++ if (point.type === "M") {+ return {+ type: "M",+ point: multiplyMatrix(matrix, point.point),+ };+ }++ if (point.type === "L") {+ return {+ type: "L",+ point: multiplyMatrix(matrix, point.point),+ };+ }++ if (point.type === "Z") {+ return {+ type: "Z",+ point: multiplyMatrix(matrix, point.point),+ };+ }++ throw new Error("Unknown instruction type: " + JSON.stringify(point));+}++export const multiplyMatrix = (+ matrix: MatrixTransform4D,+ point: Vector4D+): Vector4D => {+ if (matrix.length !== 16 || point.length !== 4) {+ throw new Error("Invalid matrix or vector dimension");+ }++ const result: Vector4D = [0, 0, 0, 0];++ for (let i = 0; i < 4; i++) {+ for (let j = 0; j < 4; j++) {+ result[i] += matrix[i * 4 + j] * point[j];+ }+ }++ return result;+};++export const sub4d = function (a: Vector4D, b: Vector4D): Vector4D {+ return a.map((v, i) => {+ return v - b[i];+ }) as Vector4D;+};diff --git a/packages/docs/src/components/DoMoreHero/subdivide-instruction.ts b/packages/docs/src/components/DoMoreHero/subdivide-instruction.tsnew file mode 100644index 0000000000..c94edd2f0e--- /dev/null+++ b/packages/docs/src/components/DoMoreHero/subdivide-instruction.ts@@ -0,0 +1,238 @@+import type { Instruction } from "@remotion/paths";+import type { ThreeDReducedInstruction } from "./3d-svg";+import type { Vector2D, Vector4D } from "./matrix";++export const subdivideInstructions = (+ instructions: ThreeDReducedInstruction[]+): ThreeDReducedInstruction[] => {+ const newInstructions: ThreeDReducedInstruction[] = [];+ instructions.forEach((instruction, i) => {+ if (instruction.type === "M") {+ newInstructions.push(instruction);+ return;+ }++ if (instruction.type === "Z") {+ newInstructions.push(instruction);+ return;+ }++ const previousInstruction = instructions[i - 1];+ const subdivided = subdivideInstruction(+ previousInstruction.point,+ instruction+ );+ newInstructions.push(...subdivided);+ });+ return newInstructions;+};++const subdivideInstruction = (+ from: Vector4D,+ instruction: ThreeDReducedInstruction+): ThreeDReducedInstruction[] => {+ if (instruction.type === "C") {+ return subdivide3DCInstruction(from, instruction);+ }++ if (instruction.type === "L" || instruction.type === "M") {+ return subdivideLOrMInstruction(from, instruction);+ }++ if (instruction.type === "Q") {+ return subdivideQInstruction(from, instruction);+ }++ if (instruction.type === "Z") {+ return [instruction];+ }++ throw new Error("Cannot subdivide instruction");+};++const subdivideLOrMInstruction = (+ from: Vector4D,+ instruction: ThreeDReducedInstruction+) => {+ if (instruction.type !== "L" && instruction.type !== "M") {+ throw new Error("Expected L or M instruction");+ }++ const t = 0.5;+ const q0: Vector4D = [+ (1 - t) * from[0] + t * instruction.point[0],+ (1 - t) * from[1] + t * instruction.point[1],+ (1 - t) * from[2] + t * instruction.point[2],+ (1 - t) * from[3] + t * instruction.point[3],+ ];++ const curves: [ThreeDReducedInstruction, ThreeDReducedInstruction] = [+ { type: instruction.type, point: q0 },+ { type: instruction.type, point: instruction.point },+ ];++ return curves;+};++export const subdivide2DCInstruction = (+ fromX: number,+ fromY: number,+ instruction: Instruction,+ t: number+) => {+ if (instruction.type !== "C") {+ throw new Error("Expected C instruction");+ }++ const q0: Vector2D = [+ (1 - t) * fromX + t * instruction.cp1x,+ (1 - t) * fromY + t * instruction.cp1y,+ ];++ const q1: Vector2D = [+ (1 - t) * instruction.cp1x + t * instruction.cp2x,+ (1 - t) * instruction.cp1y + t * instruction.cp2y,+ ];++ const q2: Vector2D = [+ (1 - t) * instruction.cp2x + t * instruction.x,+ (1 - t) * instruction.cp2y + t * instruction.y,+ ];++ const r0: Vector2D = [+ (1 - t) * q0[0] + t * q1[0],+ (1 - t) * q0[1] + t * q1[1],+ ];++ const r1: Vector2D = [+ (1 - t) * q1[0] + t * q2[0],+ (1 - t) * q1[1] + t * q2[1],+ ];++ const s0: Vector2D = [+ (1 - t) * r0[0] + t * r1[0],+ (1 - t) * r0[1] + t * r1[1],+ ];++ const curves: [Instruction, Instruction] = [+ {+ type: "C",+ x: s0[0],+ y: s0[1],+ cp1x: q0[0],+ cp1y: q0[1],+ cp2x: r0[0],+ cp2y: r0[1],+ },+ {+ type: "C",+ x: instruction.x,+ y: instruction.y,+ cp1x: r1[0],+ cp1y: r1[1],+ cp2x: q2[0],+ cp2y: q2[1],+ },+ ];++ return curves;+};++const subdivide3DCInstruction = (+ from: Vector4D,+ instruction: ThreeDReducedInstruction+) => {+ if (instruction.type !== "C") {+ throw new Error("Expected C instruction");+ }++ const t = 0.5;++ const q0: Vector4D = [+ (1 - t) * from[0] + t * instruction.cp1[0],+ (1 - t) * from[1] + t * instruction.cp1[1],+ (1 - t) * from[2] + t * instruction.cp1[2],+ (1 - t) * from[3] + t * instruction.cp1[3],+ ];++ const q1: Vector4D = [+ (1 - t) * instruction.cp1[0] + t * instruction.cp2[0],+ (1 - t) * instruction.cp1[1] + t * instruction.cp2[1],+ (1 - t) * instruction.cp1[2] + t * instruction.cp2[2],+ (1 - t) * instruction.cp1[3] + t * instruction.cp2[3],+ ];++ const q2: Vector4D = [+ (1 - t) * instruction.cp2[0] + t * instruction.point[0],+ (1 - t) * instruction.cp2[1] + t * instruction.point[1],+ (1 - t) * instruction.cp2[2] + t * instruction.point[2],+ (1 - t) * instruction.cp2[3] + t * instruction.point[3],+ ];++ const r0: Vector4D = [+ (1 - t) * q0[0] + t * q1[0],+ (1 - t) * q0[1] + t * q1[1],+ (1 - t) * q0[2] + t * q1[2],+ (1 - t) * q0[3] + t * q1[3],+ ];++ const r1: Vector4D = [+ (1 - t) * q1[0] + t * q2[0],+ (1 - t) * q1[1] + t * q2[1],+ (1 - t) * q1[2] + t * q2[2],+ (1 - t) * q1[3] + t * q2[3],+ ];++ const s0: Vector4D = [+ (1 - t) * r0[0] + t * r1[0],+ (1 - t) * r0[1] + t * r1[1],+ (1 - t) * r0[2] + t * r1[2],+ (1 - t) * r0[3] + t * r1[3],+ ];++ const curves: [ThreeDReducedInstruction, ThreeDReducedInstruction] = [+ { type: "C", point: s0, cp1: q0, cp2: r0 },+ { type: "C", point: instruction.point, cp1: r1, cp2: q2 },+ ];++ return curves;+};++const subdivideQInstruction = (+ from: Vector4D,+ instruction: ThreeDReducedInstruction+) => {+ if (instruction.type !== "Q") {+ throw new Error("Expected Q instruction");+ }++ const t = 0.5;+ const q0: Vector4D = [+ (1 - t) * from[0] + t * instruction.cp[0],+ (1 - t) * from[1] + t * instruction.cp[1],+ (1 - t) * from[2] + t * instruction.cp[2],+ (1 - t) * from[3] + t * instruction.cp[3],+ ];++ const q1: Vector4D = [+ (1 - t) * instruction.cp[0] + t * instruction.point[0],+ (1 - t) * instruction.cp[1] + t * instruction.point[1],+ (1 - t) * instruction.cp[2] + t * instruction.point[2],+ (1 - t) * instruction.cp[3] + t * instruction.point[3],+ ];++ const r0: Vector4D = [+ (1 - t) * q0[0] + t * q1[0],+ (1 - t) * q0[1] + t * q1[1],+ (1 - t) * q0[2] + t * q1[2],+ (1 - t) * q0[3] + t * q1[3],+ ];++ const newInstructions: [ThreeDReducedInstruction, ThreeDReducedInstruction] =+ [+ { type: "Q", point: r0, cp: q0 },+ { type: "Q", point: instruction.point, cp: q1 },+ ];++ return newInstructions;+};diff --git a/packages/docs/src/components/DoMoreHero/truthy.ts b/packages/docs/src/components/DoMoreHero/truthy.tsnew file mode 100644index 0000000000..d81eb0649b--- /dev/null+++ b/packages/docs/src/components/DoMoreHero/truthy.ts@@ -0,0 +1,4 @@+type Truthy<T> = T extends false | "" | 0 | null | undefined ? never : T;+export function truthy<T>(value: T): value is Truthy<T> {+ return Boolean(value);+}diff --git a/packages/docs/src/components/V4Countdown.tsx b/packages/docs/src/components/V4Countdown.tsxindex 6de9780112..fb73822180 100644--- a/packages/docs/src/components/V4Countdown.tsx+++ b/packages/docs/src/components/V4Countdown.tsx@@ -5,8 +5,7 @@ const style: React.CSSProperties = {flex: 1,justifyContent: "center",alignItems: "center",- border: "2px solid #000",- backgroundColor: "#F9F9F9",+ border: "2px solid var(--ifm-font-color-base)",borderRadius: 10,padding: 10,};diff --git a/packages/docs/src/pages/4/index.tsx b/packages/docs/src/pages/4/index.tsxindex c028b14494..cfbee9748f 100644--- a/packages/docs/src/pages/4/index.tsx+++ b/packages/docs/src/pages/4/index.tsx@@ -5,6 +5,7 @@ import Layout from "@theme/Layout";import React, { useCallback, useMemo, useState } from "react";import { Spacer } from "../../../components/layout/Spacer";import { CoolInput } from "../../../components/TextInput";+import { DoMoreHero } from "../../components/DoMoreHero/DoMoreHero";import { Seo } from "../../components/Seo";import { V4Countdown } from "../../components/V4Countdown";import styles from "./v4.module.css";@@ -85,9 +86,10 @@ const V4: React.FC = () => {{Seo.renderTitle("Do more with React | Remotion 4.0")}{Seo.renderImage("/img/remotion4.png", context.siteConfig.url)}</Head>+ <DoMoreHero /><div className={styles.container}><div className={styles.wrapper}>- <h1 className={styles.pagetitle}>Do more with React.</h1>+ <h1 className={styles.pagetitle}>Do more with React</h1><div style={{ display: "flex", justifyContent: "center" }}><p className={styles.title}>Learn about the new possibilities of Remotion 4.0 and celebratediff --git a/packages/docs/src/pages/4/v4.module.css b/packages/docs/src/pages/4/v4.module.cssindex dbd8046e23..63a755cec1 100644--- a/packages/docs/src/pages/4/v4.module.css+++ b/packages/docs/src/pages/4/v4.module.css@@ -1,11 +1,12 @@.container {- position: "relative";+ position: relative;+ margin-top: -5vw;+ z-index: 1;}.wrapper {max-width: 1000px;margin: auto;- padding-top: 100px;padding-left: 20px;padding-right: 20px;display: flex;