0.1.1 • Published 4 years ago

kubefont v0.1.1

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

Kubefont

Example image

A simple React component (written in Typescript) using Three.js to show a text and a number of random-positioned cubes. It exploits the normal hover event to move the camera while on desktop, and the gyroscope on mobile devices.

This is an experiment I made to test the integration between React and Three.js. It is not remotely tested enough or fit for a production environment.

Live example here!

Usage

Install the component using your favourite package manager:

# npm
npm install kubefont

# yarn
yarn add kubefont

Then use it as simple as:

import React from 'react';
import Kubefont from 'kubefont';

const App = () => (
  <div style={{ width: '100vw', height: '100vh' }}>
    <Kubefont text="Hello World" textFontUrl="/font.json" />
  </div>
);

The textFontUrl prop takes the path to your JSON font. This font must be served as a static file from your app. To create a JSON font you can use Facetype.js.

This component takes the same size of its parent container.

API

These are the props accepted by the Kubefont component:

Prop nameTypeDescriptionRequiredDefault
textStringThe string to displayYes-
textFontUrlStringThe path to the JSON font file (served statically from the webserver)Yes-
textColorStringThe text color in HEXNo"#dddddd"
cameraDistanceNumberThe text distance from the cameraNo400
cubesColorStringThe cubes color in HEXNo"#dddddd"
particlesNumberNumberThe number of cubes to displayNo50
scatteringNumberThe cubes' scattering levelNo1.5
backgroundColorStringThe environment background color in HEXNo"#000000"
useGyroscopeBooleanWhether to use the gyroscope or the hover eventNofalse
GyroscopeRequestComponentFunctionTakes a function as a parameter and return a new React node (see below)No-

Gyroscope

To use the gyroscope you HAVE to set this prop!

This component supports the DeviceOrientationEvent (W3C Documentation). In iOS13 you have to ask for permission to use the gyroscope and this permission must be granted "manually" (E.g. from a onClick event) from the user. To handle this particular case this component supports the GyroscopeRequestComponent prop, which takes a function as the only parameter and return a new React node. This new React node will be added over the Three.js canvas (you can even position it absolute) and triggering the passed function from there will try to obtain the permission.

import React from 'react';
import Kubefont from 'kubefont';

const App = () => (
  <div style={{ width: '100vw', height: '100vh' }}>
    <Kubefont
      text="Hello World"
      textFontUrl="/font.json"
      useGyroscope={true}
      GyroscopeRequestComponent={request => (
        <a onClick={request} style={{ color: '#ffffff' }}>
          Enable Gyroscope
        </a>
      )}
    />
  </div>
);
0.1.1

4 years ago

0.1.0

4 years ago