react-browser-videos
v1.0.5
Published
Create mp4 videos using React and export it right in the browser (client-side) to avoid server costs
Downloads
2
Readme
render-video
Made with create-react-library
Install
npm install --save react-browser-videos
Usage
Setup
If using Next.js remember "use client"
// A page wrapping the canvas
import React, { Component } from 'react'
import { CanvasProvider } from 'react-browser-videos'
export function Page() {
<CanvasProvider FPS={30} duration={100}>
* Your other page/component *
</CanvasProvider>
}
Basic Example
// Page/component for everything else
import React from 'react';
import { HtmlCanvas, DisplayCanvas, PlayPauseButton, TimeSlider, RenderButton, useCurrentFrame } from "react-browser-videos";
function App() {
const currentFrame = useCurrentFrame();
return (
<div className="App">
<p>Current Frame: {currentFrame}</p>
<DisplayCanvas width={1000}/>
<HtmlCanvas width={1000}>
<div style={{
width: "100%",
height: "100%",
display: "grid",
placeItems: "center"
}}>
<p style={{ fontSize: "60px" }}>Frame {currentFrame}</p>
</div>
</HtmlCanvas>
<div style={{display: "flex"}}>
<PlayPauseButton/>
<TimeSlider/>
<RenderButton/>
</div>
</div>
);
}
export default App;
Components
CanvasProvider
The CanvasProvider is required for the package to work
The page or components that are gonna have anything to do with the video creation needs to be wrapped in this.
| Props | Description | Default | | ------------- | ------------- |- | | FPS | Video Frame Rate | 30 | | Duration | Video Duration | 100 |
import { CanvasProvider } from "react-browser-videos";
<CanvasProvider FPS={30} duration={100}>
* More code *
</CanvasProvider>
DisplayCanvas
The DisplayCanvas is used to display the canvas version of the HtmlCanvas
| Props | Description | Default | | ------------- | ------------- |- | | Width | Width of the canvas in pixels | 500 |
import { DisplayCanvas } from "react-browser-videos";
<DisplayCanvas width={500}/>
HtmlCanvas
The HtmlCanvas is used to convert your html to a canvas drawing. It only works if there is a DisplayCanvas present
| Props | Description | Default | | ------------- | ------------- |- | | Width | Width of the canvas in pixels | 500 | | Hidden | Hides the display canvas but the render still works fine | false |
import { HtmlCanvas } from "react-browser-videos";
<HtmlCanvas width={500}>
* Html code *
</HtmlCanvas>
Canvas Components
CanvasImage
Use CanvasImage to display and render images without errors
| Props | Description | | ------------- | ------------- | | src | Source of the image |
import { CanvasImage } from "react-browser-videos";
<CanvasImage src="<image-url>"/>
CanvasVideo
Use CanvasVideo to display and render videos without errors
| Props | Description | | ------------- | ------------- | | src | Source of the video |
import { CanvasVideo } from "react-browser-videos";
<CanvasVideo src="<video-url>"/>
Video Controls
Play Button
A simple button that starts the animation. It can be styled to preference
import { PlayButton } from "react-browser-videos";
<PlayButton/>
Pause Button
A simple button that stops the animation. It can be styled to preference
import { PauseButton } from "react-browser-videos";
<PauseButton/>
Play/Pause Button
This is the play and pause button in 1. It changes state and function based on if the video is currently playing
import { PlayPauseButton } from "react-browser-videos";
<PlayPauseButton/>
Time Slider
A range slider that can be used to controlling the timestamp of the video. It can be styled to preference
import { TimeSlider } from "react-browser-videos";
<TimeSlider/>
Render Button
A button that renders the video and downloads it. It can be styled to preference
| Props | Description | Default | | ------------- | ------------- | - | | fileName | Takes in a string that will be used to download the video | video.mp4 |
import { RenderButton } from "react-browser-videos";
<RenderButton fileName="MyVideo"/>
Functions
useCurrentFrame()
This function is used to get the current frame. It can be used to create dynamic video elements
import { useCurrentFrame } from "react-browser-videos";
const currentFrame = useCurrentFrame()
interpolate()
This function is used to get the current frame. It can be used to create dynamic video elements
| Parameters | Description | | ------------- | ------------- | | frame | The current frame | | startFrame | What frame the interpolation should start at | | endFrame | What frame the interpolation should be finished | | startValue | What the value should go from - Negative numbers are also accepted | | endValue | What the value should end on | | mode | linear, easeIn, easeOut |
import { interpolate } from "react-browser-videos";
const currentFrame = useCurrentFrame()
const opacity = interpolate(frame, 0, 1, 0, 100, "linear")
License
MIT © @esbenischeap