Skip to content

Commit 1648aa5

Browse files
authored
Merge pull request #4 from react18-tools/use-glsl-files
Use glsl files
2 parents 0fce4c2 + 8b61336 commit 1648aa5

File tree

12 files changed

+80
-46
lines changed

12 files changed

+80
-46
lines changed

.vscode/settings.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,8 @@
1313
"editor.formatOnSave": true,
1414
"editor.formatOnPaste": true,
1515
"editor.formatOnSaveMode": "file",
16-
"mayank1513.trello-kanban.Workspace.filePath": ".tkb"
16+
"mayank1513.trello-kanban.Workspace.filePath": ".tkb",
17+
"[glsl]": {
18+
"editor.defaultFormatter": "raczzalan.webgl-glsl-editor"
19+
}
1720
}

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# React Mouse Trail <img src="https://github.com/react18-tools/react-mouse-trail/blob/main/popper.png?raw=true" style="height: 40px"/>
1+
# React WebGL Trails <img src="https://github.com/react18-tools/react-mouse-trail/blob/main/popper.png?raw=true" style="height: 40px"/>
22

33
[![test](https://github.com/react18-tools/react-mouse-trail/actions/workflows/test.yml/badge.svg)](https://github.com/react18-tools/react-mouse-trail/actions/workflows/test.yml) [![Maintainability](https://api.codeclimate.com/v1/badges/4bb3a3316ff4ecd2a9eb/maintainability)](https://codeclimate.com/github/react18-tools/react-mouse-trail/maintainability) [![codecov](https://codecov.io/gh/react18-tools/react-mouse-trail/graph/badge.svg)](https://codecov.io/gh/react18-tools/react-mouse-trail) [![Version](https://img.shields.io/npm/v/react-mouse-trails.svg?colorB=green)](https://www.npmjs.com/package/react-mouse-trails) [![Downloads](https://img.jsdelivr.com/img.shields.io/npm/d18m/react-mouse-trails.svg)](https://www.npmjs.com/package/react-mouse-trails) ![npm bundle size](https://img.shields.io/bundlephobia/minzip/react-mouse-trails) [![Gitpod ready-to-code](https://img.shields.io/badge/Gitpod-ready--to--code-blue?logo=gitpod)](https://gitpod.io/from-referrer/)
44

@@ -21,19 +21,19 @@ A lightweight WebGL-based React component for creating an interactive mouse trai
2121
### Installation
2222

2323
```bash
24-
$ pnpm add react-mouse-trails
24+
$ pnpm add react-webgl-trails
2525
```
2626

2727
**_or_**
2828

2929
```bash
30-
$ npm install react-mouse-trails
30+
$ npm install react-webgl-trails
3131
```
3232

3333
**_or_**
3434

3535
```bash
36-
$ yarn add react-mouse-trails
36+
$ yarn add react-webgl-trails
3737
```
3838

3939
## Usage

lib/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# react-mouse-trails
22

3+
## 0.0.4
4+
5+
### Patch Changes
6+
7+
- b3e0277: Refactor: use glsl shader files.
8+
39
## 0.0.3
410

511
### Patch Changes

lib/package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
"name": "react-mouse-trails",
33
"author": "Mayank Kumar Chaudhari <https://mayank-chaudhari.vercel.app>",
44
"private": false,
5-
"version": "0.0.3",
5+
"version": "0.0.4",
66
"description": "A lightweight WebGL-based React component for creating a mouse trail effect.",
77
"license": "MPL-2.0",
88
"main": "./dist/index.js",
99
"module": "./dist/index.mjs",
1010
"types": "./dist/index.d.ts",
11-
"repository": "github:react18-tools/react-mouse-trail",
12-
"bugs": "https://github.com/react18-tools/react-mouse-trail/issues",
13-
"homepage": "https://github.com/react18-tools/react-mouse-trail/#readme",
11+
"repository": "github:react18-tools/react-webgl-trails",
12+
"bugs": "https://github.com/react18-tools/react-webgl-trails/issues",
13+
"homepage": "https://github.com/react18-tools/react-webgl-trails/#readme",
1414
"sideEffects": false,
1515
"files": [
1616
"dist/**"
@@ -35,6 +35,7 @@
3535
"esbuild-plugin-rdi": "^0.0.0",
3636
"esbuild-plugin-react18": "0.2.4",
3737
"esbuild-plugin-react18-css": "^0.0.4",
38+
"esbuild-plugin-webgl": "^0.0.1",
3839
"jsdom": "^24.1.0",
3940
"react": "^18.3.1",
4041
"react-dom": "^18.3.1",

lib/src/declaration.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
11
declare module "*.module.css";
22
declare module "*.module.scss";
3+
declare module "*.glsl" {
4+
const value: string;
5+
export default value;
6+
}
7+
declare module "*.glsl?raw" {
8+
const value: string;
9+
export default value;
10+
}

lib/src/shaders/frag.glsl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
precision mediump float;
2+
3+
uniform vec3 c;
4+
varying float v;
5+
void main() {
6+
gl_FragColor = vec4(c, v);
7+
}

lib/src/shaders/vert.glsl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
attribute vec2 pos;
2+
attribute float a;
3+
varying float v;
4+
void main() {
5+
gl_Position = vec4(pos, 0.0, 1.0);
6+
v = a;
7+
}

lib/src/utils.ts

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,8 @@
1-
const vertexShaderSource = `
2-
attribute vec2 pos;
3-
attribute float a;
4-
varying float v;
5-
void main() {
6-
gl_Position = vec4(pos, 0.0, 1.0);
7-
v = a;
8-
}
9-
`;
10-
11-
/** Create fragmentSource */
12-
const fragmentShaderSource = (rgb = [1, 0, 0]) => `
13-
precision mediump float;
14-
varying float v;
15-
void main() {
16-
gl_FragColor = vec4(${rgb.join()}, v);
17-
}
18-
`;
1+
import vertexShaderSource from "./shaders/vert.glsl?raw";
2+
import fragmentShaderSource from "./shaders/frag.glsl?raw";
193

204
/** Setup trails */
21-
export const trails = (gl: WebGLRenderingContext, rgb?: [number, number, number]) => {
5+
export const trails = (gl: WebGLRenderingContext, rgb: [number, number, number] = [1, 0, 0]) => {
226
gl.enable(gl.BLEND);
237
gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
248

@@ -47,7 +31,7 @@ export const trails = (gl: WebGLRenderingContext, rgb?: [number, number, number]
4731
return buffer;
4832
};
4933
const vertexShader = createShader(gl.VERTEX_SHADER, vertexShaderSource);
50-
const fragmentShader = createShader(gl.FRAGMENT_SHADER, fragmentShaderSource(rgb));
34+
const fragmentShader = createShader(gl.FRAGMENT_SHADER, fragmentShaderSource);
5135
const program = gl.createProgram();
5236
if (!program) throw new Error("Failed to create program");
5337
gl.attachShader(program, vertexShader);
@@ -76,6 +60,8 @@ export const trails = (gl: WebGLRenderingContext, rgb?: [number, number, number]
7660
const positionBuffer = createBuffer();
7761
const timeBuffer = createBuffer();
7862

63+
gl.uniform3f(gl.getUniformLocation(program, "c"), ...rgb);
64+
7965
let runningAnim = false;
8066
let positions: number[] = [];
8167
let fades: number[] = [];

lib/tsup.config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { defineConfig, type Options } from "tsup";
33
import react18Plugin from "esbuild-plugin-react18";
44
import cssPlugin from "esbuild-plugin-react18-css";
55
import { rdiPlugin } from "esbuild-plugin-rdi";
6+
import { webglPlugin } from "esbuild-plugin-webgl";
67

78
export default defineConfig(
89
(options: Options) =>
@@ -24,12 +25,12 @@ export default defineConfig(
2425
const contents = text
2526
.replace(/if \(!gl[^}]*}/gm, "")
2627
.replace(/;\s*if \(![^;]*;/gm, "!;")
27-
.replace(/`[^`]*`/gm, match => match.replace(/\s+/g, " ").trim())
2828
.trim();
2929
return { contents, loader: "ts" };
3030
});
3131
},
3232
},
33+
webglPlugin(),
3334
react18Plugin({ disableJSXRequireDedup: true }),
3435
cssPlugin({ generateScopedName: "rt_[local]" }),
3536
rdiPlugin(),

packages/shared/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# @repo/shared
22

3+
## 0.0.4
4+
5+
### Patch Changes
6+
7+
- Updated dependencies [b3e0277]
8+
9+
310
## 0.0.3
411

512
### Patch Changes

0 commit comments

Comments
 (0)