Code Monkey home page Code Monkey logo

webgl-react's Introduction

webgl-react

webgl-react is a React library for 2D rendering in WebGL with screen coordinates.

npm version Node.js CI

Why?

In react, drawing large numbers of shapes with divs, etc. is a very heavy process. webgl-react provides a React component that uses WebGL to draw large numbers of shapes in a single draw call.

webgl-react uses different shaders for different shapes. If you do not find the shape you need, you can write your own shaders, please read the Custom Shape section.

Example

import { BorderedCircles, GLCanvas, Rectangles } from "@ryohey/webgl-react"

export const App = () => {
  const rects = [
    {
      x: 100,
      y: 200,
      width: 30,
      height: 50,
    },
  ]
  const circleRects = [
    {
      x: 10,
      y: 50,
      width: 10,
      height: 10,
    },
  ]
  return (
    <>
      <h1>WebGL React</h1>
      <GLCanvas
        height={SIZE}
        width={SIZE}
        style={{ border: "1px solid black" }}
      >
        <Rectangles rects={rects} color={[0.5, 1, 0.5, 1.0]} />
        <BorderedCircles
          rects={circleRects}
          fillColor={[0, 0, 0.5, 0.5]}
          strokeColor={[0, 0, 0, 1]}
          zIndex={0}
        />
      </GLCanvas>
    </>
  )
}

GLCanvas

GLCanvas is a wrapper for the canvas component. You can use any of the canvas props.

Shapes

  • Rectangles
  • BorderedRectangles
  • BorderedCircles
  1. Write a GLSL shader and pass it to new Shader()
  2. Create a subclass of InstancedBuffer that matches the shader attributes
  3. Create a wrapper for the GLNode component by passing them to createShader and createBuffer

Transform

Specifies the transformation matrix to be applied to the child component. The specified transformation matrix will be multiplied by the projection matrix provided by useProjectionMatrix.

Hooks

useProjectionMatrix

Gets the projection matrix that the figure component reads internally. GLCanvas provides by default a matrix of orthographic projections that translates to the screen coordinate system.

useRenderer (internal)

Gets the Renderer class responsible for WebGL rendering. Usually not used outside of the library.

webgl-react's People

Contributors

ryohey avatar

Stargazers

Dmitry Greblyuk avatar Paulo Artur Pinheiro Viana Villaça avatar  avatar Vitaly Pavlenko avatar Little devil avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.