Skip to content

🎥 Make videos programmatically with React

develop transforms page

Jonny Burger9604557fcf2c
diff --git a/packages/docs/docs/transforms.md b/packages/docs/docs/transforms.md
index 8cf0e69e34..77a4e375f2 100644
--- a/packages/docs/docs/transforms.md
+++ b/packages/docs/docs/transforms.md
@@ -4,10 +4,17 @@ title: Transforms
---
import {Transforms} from '../components/DocsDark'
+import {MuxVideo} from '../src/components/MuxVideo';
+import {TransformsTutorial} from '../src/components/tutorials';
-Before we get into animations, let's explore what we can animate.
-The 5 most common transforms that are used in animation are: **Opacity**, **Scale**, **Skew**, **Translate** and **Rotate**.
-If you are already familiar with how to apply CSS transforms in React, you can skip this page.
+Animations occur by transforming the properties of an element over time.
+Let's take a look at five common transforms that we can apply to any element.
+
+If you are already familiar with how to apply CSS transforms in React, you can [skip this page](/docs/animating-properties).
+
+<TransformsTutorial />
+
+## The 5 basic transformations
<Transforms />
@@ -17,8 +24,138 @@ If you are already familiar with how to apply CSS transforms in React, you can s
marginTop: 10
}}>From left to right: Opacity, Scale, Skew, Translate, Rotate</p>
-<MuxVideo muxId="t00Mj7y1CLLGjaMvQyoBqcjdy4PIzVrFaT4317fKbzx00" style={{
- width: '100%'
-}} controls />
+### Opacity
+
+The opacity determines how visible the element is. `0` means fully invisible, `1` means fully visible. Values inbetween will make the element semi-transparent, and elements underneath will be visible.
+
+You can set the opacity of an element using the `opacity` property.
+
+```tsx twoslash {6} title="MyComponent.tsx"
+<div
+ style={{
+ height: 100,
+ width: 100,
+ backgroundColor: "red",
+ opacity: 0.5,
+ }}
+/>
+```
+
+Values below `0` and above `1` are accepted, but have no further effect.
+
+### Scale
+
+The scale determines how big an element is. `1` is the natural size, `2` will make the element twice as tall and wide.
+Values below `1` will make the element smaller. `0` makes the element invisible. Values below `0` are accepted and will make the element bigger again, but mirrored.
+
+You can set the scale of an element using the `transform` property.
+
+```tsx twoslash {6} title="MyComponent.tsx"
+<div
+ style={{
+ height: 100,
+ width: 100,
+ backgroundColor: "red",
+ transform: `scale(0.5)`,
+ }}
+/>
+```
+
+The difference to changing the size of the element using `height` and `width` is that using `scale()` will not change the layout of the other elements.
+
+### Skew
+
+Skewing an element will lead to a distorted appearance as if the the element has been stretched on two corners of the element. Skew takes an angle that can be specified using `rad` (radians) and `deg` (degrees).
+
+See the explorer below to see how skewing affects an element.
+
+You can set the skew of an element using the `transform` property.
+
+```tsx twoslash {6} title="MyComponent.tsx"
+<div
+ style={{
+ height: 100,
+ width: 100,
+ backgroundColor: "red",
+ transform: `skew(20deg)`,
+ }}
+/>
+```
+
+### Translate
+
+Translating an element means moving it. A translation can be done on the X, Y or even Z axis. The transformation can be specified in `px`.
+
+You can set the translation of an element using the `transform` property.
+
+```tsx twoslash {6} title="MyComponent.tsx"
+<div
+ style={{
+ height: 100,
+ width: 100,
+ backgroundColor: "red",
+ transform: `translateX(100px)`,
+ }}
+/>
+```
+
+As opposed to changing the position of an element using `margin-top` and `margin-left`, using `translate()` will not change the position of the other elements.
+
+### Rotate
+
+By rotating an element, you can make it appear as if it has been turned around its center. The rotation can be specified in `rad` (radians) or `deg` (degrees) and you can rotate an element around the Z axis (the default) but also around the X and Y axis.
+
+You can set the translation of an element using the `transform` property.
+
+```tsx twoslash {6} title="MyComponent.tsx"
+<div
+ style={{
+ height: 100,
+ width: 100,
+ backgroundColor: "red",
+ transform: `rotate(45deg)`, // the same as rotateZ(45deg)
+ }}
+/>
+```
+
+If you want to rotate an element around the X or Y axis, you should apply the [`perspective`](https://developer.mozilla.org/en-US/docs/Web/CSS/perspective) property to the parent element.
+
+If you don't want to rotate around the center, you can use the [`transform-origin`](https://developer.mozilla.org/en-US/docs/Web/CSS/transform-origin) property to change the origin of the rotation.
+
+Note that when rotating SVG elements, the transform origin is the top left corner by default. You can get the same behavior as for the other elements by adding `style={{transformBox: 'fill-box', transformOrigin: 'center center'}}`.
+
+## Multiple transformations
+
+Oftentimes, you want to combine multiple transformations. If they use different CSS properties like `transform` and `opacity`, simply specify both properties in the `style` object.
+
+If both transformations use the `transform` property, specify multiple transformations separated by a space.
+
+```tsx twoslash {6} title="MyComponent.tsx"
+<div
+ style={{
+ height: 100,
+ width: 100,
+ backgroundColor: "red",
+ transform: `translateX(100px) scale(2)`,
+ }}
+/>
+```
+
+Note that the order matters. The transformations are applied in the order they are specified.
+
+## More ways to transform objects
+
+These are just some of the basic transformations. Here are some more transformations that are possible:
+
+- The height and width of a `<div>`
+- The rounded edges of an element using `border-radius`
+- The shadow of an element using `box-shadow`
+- The color of something using `color` and [`interpolateColors()`](/docs/interpolate-colors)
+- The evolution of a SVG path using [`evolvePath()`](/docs/paths/evolve-path)
+- The weight and slant of a [dynamic font](https://twitter.com/JNYBGR/status/1598983409367683072)
+- The stops of a `linear-gradient`
+- The values of a CSS `filter()`
+
+<hr/>
-## Opacity
+Ready for the next step? Let's animate some properties!
diff --git a/packages/docs/learn/2022-12-22-apple-wow.md b/packages/docs/learn/2022-12-22-apple-wow.md
index f7acebaac3..2c23b7eb45 100644
--- a/packages/docs/learn/2022-12-22-apple-wow.md
+++ b/packages/docs/learn/2022-12-22-apple-wow.md
@@ -14,7 +14,7 @@ authors:
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
-import {AppleFireworksTutorial} from '../src/components/AppleFireworksTutorial';
+import {AppleFireworksTutorial} from '../src/components/tutorials';
Learn how to create an animation that Apple has on its website during the holiday season with this beginner-friendly Remotion tutorial!
<img src="/img/apple-wow-tutorial/Final.gif"/>
diff --git a/packages/docs/src/components/AlsoAvailableAsVideo.tsx b/packages/docs/src/components/AlsoAvailableAsVideo.tsx
new file mode 100644
index 0000000000..e40018d45f
--- /dev/null
+++ b/packages/docs/src/components/AlsoAvailableAsVideo.tsx
@@ -0,0 +1,89 @@
+import BrowserOnly from "@docusaurus/BrowserOnly";
+import React, { useCallback } from "react";
+import { VideoPlayerWithControls } from "./VideoPlayerWithControls";
+
+const link: React.CSSProperties = {
+ color: "inherit",
+ all: "unset",
+ display: "block",
+ cursor: "pointer",
+};
+
+const container: React.CSSProperties = {
+ border: "1px solid #888",
+ borderRadius: 10,
+ overflow: "hidden",
+ marginBottom: 30,
+ display: "flex",
+ flexDirection: "row",
+};
+
+const thumbnail: React.CSSProperties = {
+ height: 100,
+ display: "inline",
+ marginBottom: 0,
+ borderRight: "1px solid #888",
+};
+
+const right: React.CSSProperties = {
+ padding: 16,
+ lineHeight: 1.5,
+ display: "flex",
+ flex: 1,
+ flexDirection: "column",
+ justifyContent: "center",
+};
+
+const wouldYouRather: React.CSSProperties = {
+ fontSize: ".9em",
+};
+
+export const AlsoAvailableAsVideo: React.FC<{
+ minutes: number;
+ playbackId: string;
+ title: string;
+ thumb: string;
+}> = ({ minutes, title, thumb, playbackId }) => {
+ const [showVideo, setShowVideo] = React.useState(false);
+
+ const onClick = useCallback(() => {
+ setShowVideo(true);
+ }, []);
+
+ if (showVideo) {
+ return (
+ <div style={{ marginBottom: 30 }}>
+ <BrowserOnly>
+ {() => (
+ <VideoPlayerWithControls
+ playbackId={playbackId}
+ poster={thumb}
+ onError={(error) => {
+ console.log(error);
+ }}
+ onLoaded={() => undefined}
+ onSize={() => undefined}
+ autoPlay
+ />
+ )}
+ </BrowserOnly>
+ </div>
+ );
+ }
+
+ return (
+ <button type="button" style={link} onClick={onClick}>
+ <div style={container}>
+ <img style={thumbnail} src={thumb} />
+ <div style={right}>
+ <div style={wouldYouRather}>
+ Also available as a {minutes}min video
+ </div>
+ <div>
+ <strong>{title}</strong>
+ </div>
+ </div>
+ </div>
+ </button>
+ );
+};
diff --git a/packages/docs/src/components/VideoPlayerWithControls.tsx b/packages/docs/src/components/VideoPlayerWithControls.tsx
index d7f8b809bf..c07a677946 100644
--- a/packages/docs/src/components/VideoPlayerWithControls.tsx
+++ b/packages/docs/src/components/VideoPlayerWithControls.tsx
@@ -67,6 +67,8 @@ type Props = {
onLoaded: () => void;
onError: (error: ErrorEvent) => void;
onSize: (dim: { width: number; height: number }) => void;
+ // eslint-disable-next-line react/require-default-props
+ autoPlay?: boolean;
};
type SizedEvent = {
@@ -79,103 +81,114 @@ type SizedEvent = {
export const VideoPlayerWithControls = forwardRef<
HTMLVideoElementWithPlyr,
Props
->(({ playbackId, poster, currentTime, onLoaded, onError, onSize }, ref) => {
- const videoRef = useRef<HTMLVideoElementWithPlyr>(null);
- const metaRef = useCombinedRefs(ref, videoRef);
- const playerRef = useRef<Plyr | null>(null);
- const [playerInitTime] = useState(Date.now());
-
- const videoError = useCallback(
- (event: ErrorEvent) => onError(event),
- [onError]
- );
-
- const onImageLoad = useCallback(
- (event: SizedEvent) => {
- const [w, h] = [event.target.width, event.target.height];
- if (w && h) {
- onSize({ width: w, height: h });
- onLoaded();
- } else {
- onLoaded();
- console.error("Error getting img dimensions", event); // eslint-disable-line no-console
- }
- },
- [onLoaded, onSize]
- );
-
- /*
- * See comment above -- we're loading the poster image just so we can grab the dimensions
- * which determines styles for the player
- */
- useEffect(() => {
- const img = new Image();
- img.onload = (evt) => onImageLoad(evt as unknown as SizedEvent);
- img.src = poster;
- }, [onImageLoad, poster]);
-
- useEffect(() => {
- const video = videoRef.current;
- const src = `https://stream.mux.com/${playbackId}.m3u8`;
- let hls: Hls | null;
- hls = null;
- if (video) {
- video.addEventListener("error", videoError);
- const Plyr = require("plyr");
- playerRef.current = new Plyr(video, {
- previewThumbnails: {
- enabled: true,
- src: `https://image.mux.com/${playbackId}/storyboard.vtt`,
- },
- storage: { enabled: false },
- fullscreen: {
- iosNative: true,
- },
- captions: { active: true, language: "auto", update: true },
- });
-
- if (video.canPlayType("application/vnd.apple.mpegurl")) {
- // This will run in safari, where HLS is supported natively
- video.src = src;
- } else if (Hls.isSupported()) {
- // This will run in all other modern browsers
- hls = new Hls();
- hls.loadSource(src);
- hls.attachMedia(video);
- hls.on(Hls.Events.ERROR, (_event, data) => {
- if (data.fatal) {
- videoError(new ErrorEvent("HLS.js fatal error"));
- }
+>(
+ (
+ { playbackId, poster, currentTime, onLoaded, onError, onSize, autoPlay },
+ ref
+ ) => {
+ const videoRef = useRef<HTMLVideoElementWithPlyr>(null);
+ const metaRef = useCombinedRefs(ref, videoRef);
+ const playerRef = useRef<Plyr | null>(null);
+ const [playerInitTime] = useState(Date.now());
+
+ const videoError = useCallback(
+ (event: ErrorEvent) => onError(event),
+ [onError]
+ );
+
+ const onImageLoad = useCallback(
+ (event: SizedEvent) => {
+ const [w, h] = [event.target.width, event.target.height];
+ if (w && h) {
+ onSize({ width: w, height: h });
+ onLoaded();
+ } else {
+ onLoaded();
+ console.error("Error getting img dimensions", event); // eslint-disable-line no-console
+ }
+ },
+ [onLoaded, onSize]
+ );
+
+ /*
+ * See comment above -- we're loading the poster image just so we can grab the dimensions
+ * which determines styles for the player
+ */
+ useEffect(() => {
+ const img = new Image();
+ img.onload = (evt) => onImageLoad(evt as unknown as SizedEvent);
+ img.src = poster;
+ }, [onImageLoad, poster]);
+
+ useEffect(() => {
+ const video = videoRef.current;
+ const src = `https://stream.mux.com/${playbackId}.m3u8`;
+ let hls: Hls | null;
+ hls = null;
+ if (video) {
+ video.addEventListener("error", videoError);
+ const Plyr = require("plyr");
+ playerRef.current = new Plyr(video, {
+ previewThumbnails: {
+ enabled: true,
+ src: `https://image.mux.com/${playbackId}/storyboard.vtt`,
+ },
+ storage: { enabled: false },
+ fullscreen: {
+ iosNative: true,
+ },
+ captions: { active: true, language: "auto", update: true },
});
- } else {
- console.error(
- // eslint-disable-line no-console
- "This is an old browser that does not support MSE https://developer.mozilla.org/en-US/docs/Web/API/Media_Source_Extensions_API"
- );
- }
- }
- return () => {
- if (video) {
- video.removeEventListener("error", videoError);
+ if (video.canPlayType("application/vnd.apple.mpegurl")) {
+ // This will run in safari, where HLS is supported natively
+ video.src = src;
+ } else if (Hls.isSupported()) {
+ // This will run in all other modern browsers
+ hls = new Hls();
+ hls.loadSource(src);
+ hls.attachMedia(video);
+ hls.on(Hls.Events.ERROR, (_event, data) => {
+ if (data.fatal) {
+ videoError(new ErrorEvent("HLS.js fatal error"));
+ }
+ });
+ } else {
+ console.error(
+ // eslint-disable-line no-console
+ "This is an old browser that does not support MSE https://developer.mozilla.org/en-US/docs/Web/API/Media_Source_Extensions_API"
+ );
+ }
}
- if (hls) {
- hls.destroy();
+ return () => {
+ if (video) {
+ video.removeEventListener("error", videoError);
+ }
+
+ if (hls) {
+ hls.destroy();
+ }
+ };
+ }, [playbackId, playerInitTime, videoError, videoRef]);
+
+ useEffect(() => {
+ const video = videoRef.current;
+ if (currentTime && video) {
+ video.currentTime = currentTime;
}
- };
- }, [playbackId, playerInitTime, videoError, videoRef]);
-
- useEffect(() => {
- const video = videoRef.current;
- if (currentTime && video) {
- video.currentTime = currentTime;
- }
- }, [currentTime]);
-
- return (
- <div className="video-container">
- <video ref={metaRef} poster={poster} controls playsInline />
- </div>
- );
-});
+ }, [currentTime]);
+
+ return (
+ <div className="video-container">
+ <video
+ ref={metaRef}
+ autoPlay={autoPlay}
+ poster={poster}
+ controls
+ playsInline
+ />
+ </div>
+ );
+ }
+);
diff --git a/packages/docs/src/components/AppleFireworksTutorial.tsx b/packages/docs/src/components/tutorials.tsx
similarity index 57%
rename from packages/docs/src/components/AppleFireworksTutorial.tsx
rename to packages/docs/src/components/tutorials.tsx
index 4de9794716..176281eded 100644
--- a/packages/docs/src/components/AppleFireworksTutorial.tsx
+++ b/packages/docs/src/components/tutorials.tsx
@@ -1,5 +1,6 @@
import BrowserOnly from "@docusaurus/BrowserOnly";
import React from "react";
+import { AlsoAvailableAsVideo } from "./AlsoAvailableAsVideo";
import { VideoPlayerWithControls } from "./VideoPlayerWithControls";
export const AppleFireworksTutorial = () => {
@@ -19,3 +20,18 @@ export const AppleFireworksTutorial = () => {
</BrowserOnly>
);
};
+
+export const TransformsTutorial = () => {
+ return (
+ <BrowserOnly>
+ {() => (
+ <AlsoAvailableAsVideo
+ playbackId="T0015eD012a4pB26i8veyHA88IRTro2BxVGtGCY3Eq81o"
+ thumb="https://image.mux.com/T0015eD012a4pB26i8veyHA88IRTro2BxVGtGCY3Eq81o/thumbnail.png"
+ minutes={9}
+ title="The 5 basic transformations"
+ />
+ )}
+ </BrowserOnly>
+ );
+};