Skip to main content
Camera Kit
WebAR

Creating a Camera Kit Web application with React & TypeScript

Camera Kit Web brings Snap's cutting-edge augmented reality technology to your mobile and desktop web applications.

In this tutorial, you'll learn how to build a Camera Kit Web application with React and TypeScript, allowing you to apply a Lens to your webcam.

In subsequent tutorials, we'll enhance the application by adding features like Lens selection, camera switching (if multiple cameras are available), photo capture, and video recording.

Prerequisites

  • Before we get started, it's important you familiarize yourself with the core prerequisites of building a web application.
  • Node.js and npm must be installed on your computer. npm comes bundled with Node.js by default and can be downloaded here.
  • Familiarity with HTML & CSS.
  • Basic knowledge of programming; in particular JavaScript (or even better, TypeScript).
  • General understanding of React.
  • For writing code, we will be using Visual Studio Code (VS Code).
Section 1

Getting Started

Let's prepare our local development environment to start building our app.

Step 1

Using VS Code, open the starting project which can be downloaded in the tutorial summary above.

Step 2

Use the following command to install node modules:

npm install

Step 3

Install Camera Kit Web:

npm i @snap/camera-kit

Step 4

Now, let's start the local dev server:

npm run dev

Loading
Loading
Loading
Next Section
Create a Camera Kit context
Section 2

Create a Camera Kit context

The CameraKitContext will encapsulate all the logic necessary to manage our Camera Kit session. This will ensure that Camera Kit is initialized only once but remains available for use throughout your application.

Step 1

In the Explorer view, create a new file in the src/contexts directory named CameraKitContext.tsx

Step 2

Import necessary modules

Import the necessary modules from the @snap/camera-kit and React packages.

Step 3

Define API token and Lens group ID

These can be found in My Lenses and will be used to bootstrap Camera Kit as well as fetch Lenses.

Step 4

Define CameraKitState interface

This represents the properties available to components within this context.

Step 5

Create the context

Initially, set the context value to null. After Camera Kit is initialized, the context will be populated with the appropriate data.

Step 6

Create a CameraKit wrapper component

This component can be used to wrap parts of the application where Camera Kit will be used.

Step 7

Create a reference to track initialization

To ensure Camera Kit is only bootstrapped once, create a reference with useRef to be used to track when Camera Kit has been initialized.

Step 8

Define state for the Camera Kit session and Lenses

These values will ultimately become accessible by consumers of the CameraKitContext.

Step 9

Create a Effect that calls a new initializeCameraKit function

This Effect will set all the logic to initialize the Camera Kit Sesson and fetch Lenses.

Step 10

Bootstrap Camera Kit

Using the API token defined at the top of this file, bootstrap Camera Kit.

Step 11

Create a Camera Kit Session

This session will be used to manage video sources and apply Lenses.

Step 12

Fetch Lenses

Using the Lens Group ID defined at the top of this file, fetch Lenses from the Camera Kit backend.

Step 13

Set the Camera Kit Session and Lenses to state.

Step 14

Implement the isInitialized reference

To prevent Camera Kit from being initialized multiple times, check if isInitialized is true and return early if it is; otherwise, set isInitialized to true.

Step 15

Render a loading state

While Camera Kit is initializing and there is no active session, render a loading state.

Step 16

Provide the Camera Kit context

Once Camera Kit is initialized and we have an active session, provide the context to the children components.

The children components will have access to both the Camera Kit session and the Lenses from the specified Lens group.

Loading
CameraKitContext.tsx
CameraKitContext.tsx
CameraKitContext.tsx
CameraKitContext.tsx
CameraKitContext.tsx
CameraKitContext.tsx
CameraKitContext.tsx
CameraKitContext.tsx
CameraKitContext.tsx
CameraKitContext.tsx
CameraKitContext.tsx
CameraKitContext.tsx
CameraKitContext.tsx
CameraKitContext.tsx
CameraKitContext.tsx
Next Section
Create a hook for the Camera Kit context
Section 3

Create a hook for the Camera Kit context

To make the Camera Kit context easily accessible in multiple components and ensure it's only used within its provider, create a custom hook to encapsulate the logic.

Step 1

Create a file in the src/hooks directory named useCameraKit.ts

Step 2

Import necessary modules

Import the useContext method from React along with the recently created CameraKitContext.

Step 3

Define the custom hook

Create a new custom hook named useCameraKit.

Step 4

Access the CameraKitContext value

This value contains the Camera Kit session and array of Lenses.

Step 5

Add a guard clause

Ensure that the hook is used within a component that is wrapped by the CameraKitContext provider. If the context value is null, throw an error.

Step 6

Return the context value

If the context value is not null, return the current state.

Loading
useCameraKit.ts
useCameraKit.ts
useCameraKit.ts
useCameraKit.ts
useCameraKit.ts
Next Section
Using Camera Kit to apply Lenses
Section 4

Using Camera Kit to apply Lenses

With the Camera Kit context and custom hook in place, it's time to attach a webcam as a source, apply a Lens, and render the output canvas.

Step 1

Provide the Camera Kit context

In main.tsx, use the CameraKit component to provide the context to the App component and its children. This ensures that all components within App can access the Camera Kit context.

Step 2

Using the Camera Kit context

Open App.tsx and import the necessary modules. This is the component where the Camera Kit session and Lenses will be used.

Step 3

Access Camera Kit State

Inside the App component, use the useCameraKit hook to access the Camera Kit session and Lenses.

Step 4

Create a reference to the HTMLDivElement where Camera Kit will render

Use the useRef React hook to create a reference called canvasContainerRef and attach it to the returned HTMLDivElement.

Step 5

Create a callback function to initialize Camera Kit and invoke it within an effect

This function will contain all the logic to obtain a MediaStream from the webcam and apply a Lens.

Step 6

Obtain a MediaStream from the webcam

Additional options like resolution, frame rate and facing mode can be set here as well.

Step 7

Create a media stream source

Using the createMediaStreamSource method from the @snap/camera-kit package, create a media stream source and attach it to your Camera Kit session.

This will also mirror the input feed by utilizing the transform option.

Step 8

Apply a Lens

Using the lenses object from the custom hook, apply the first Lens to the Camera Kit session.

Step 9

Play the live output canvas

This will immediately start rendering frames from the webcam, with the Lens applied, to the output canvas.

Step 10

Render the output canvas

Inside of an effect, replace the canvasContainerRef element with the live output canvas from the Camera Kit session.

main.tsx
App.tsx
App.tsx
App.tsx
App.tsx
App.tsx
App.tsx
App.tsx
App.tsx
App.tsx
Was this page helpful?
Yes
No