Skip to content

Commit

Permalink
README update
Browse files Browse the repository at this point in the history
  • Loading branch information
gksander committed Jul 27, 2022
1 parent 417cb56 commit 48adbbd
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 29 deletions.
53 changes: 29 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,43 @@
# create-svelte
# Mandelbruh

Everything you need to build a Svelte project, powered by [`create-svelte`](https://github.com/sveltejs/kit/tree/master/packages/create-svelte).
This repo houses the code for [mandelbruh](https://mandelbruh.dev), a Mandelbrot set visualizer built using WebGL and Svelte.

## Creating a project
![Image of the site](./docs/sample.png)

If you're seeing this, you've probably already done this step. Congrats!
The UI allows you to adjust the number of "iterations" used to determine if a point "escapes". You can also adjust the colors for the visualization Furthermore, you can scroll to zoom, and click-and-drag to pan the view.

```bash
# create a new project in the current directory
npm init svelte
## How does it work?

# create a new project in my-app
npm init svelte my-app
```

## Developing
The visualization is powered by WebGL (mostly just shaders), and the UI is powered by Svelte. At a high-level, this is how things work:

Once you've created a project and installed dependencies with `npm install` (or `pnpm install` or `yarn`), start a development server:
- The UI is powered by Svelte, using Svelte stores etc. to manage input values. For example, there's an `iterations` value that can be adjusted in the UI – the value for this is stored in a Svelte store, but is essentially just an integer value.
- There is a WebGL program running inside of a canvas element. The WebGL program render cycle is running on a `requestAnimationFrame` loop, and uses various JS variables/store values that get passed along to WebGL as "uniform values".
- As JS variables/store values are updated, the next animation frame will pick up the value and pass it along to the WebGL context to be used in the next render.

```bash
npm run dev
The code for these WebGL shaders can be found in [`src/shaders`](./src/shaders/).In [the `fs.frag`](./src/shaders/fs.frag) file, you'll notice some declarations like:

# or start the server and open the app in a new browser tab
npm run dev -- --open
```glsl
uniform int iterations;
uniform vec2 center;
uniform float scale;
uniform float resolution;
uniform vec3 boundColor;
uniform vec3 transitionColor;
uniform vec3 escapeColor;
```

## Building
which are essentially the input variables to the shader process, whose values are provided from the JS side of things.

To create a production version of your app:
In [the `FractalDisplay.svelte` file](./src/components/FractalDisplay.svelte) you'll see something like this:

```bash
npm run build
```ts
gl.uniform1f(locations.resolution, width);
gl.uniform1i(locations.iterations, Math.round($iterations));
gl.uniform1f(locations.scale, $scale);
gl.uniform2f(locations.center, $center.x + centerDelta.x, $center.y + centerDelta.y);
gl.uniform3f(locations.boundColor, ...$boundColor);
gl.uniform3f(locations.transitionColor, ...$transitionColor);
gl.uniform3f(locations.escapeColor, ...$escapeColor);
```

You can preview the production build with `npm run preview`.

> To deploy your app, you may need to install an [adapter](https://kit.svelte.dev/docs/adapters) for your target environment.
These `gl.uniform1f` etc calls are what send our JS values into the WebGL context for our shader to use.
Binary file added docs/sample.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 18 additions & 4 deletions src/components/FractalDisplay.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@
escapeColor: null as WebGLUniformLocation | null
};
// Options for rendering
// Some local interaction state
let centerDelta = { x: 0, y: 0 };
let isResizing = false;
let resizeTimeout;
// Interactions
let mouseDownStartPos = null as { x: number; y: number; ts: number } | null;
Expand Down Expand Up @@ -139,8 +140,10 @@
};
const animate = () => {
measure();
render();
if (!isResizing) {
measure();
render();
}
requestAnimationFrame(animate);
};
Expand Down Expand Up @@ -210,3 +213,14 @@
on:wheel|preventDefault|stopPropagation={handleWheel}
/>
</div>

<!-- Throttle the RAF rendering when resizing window -->
<svelte:window
on:resize={() => {
isResizing = true;
clearTimeout(resizeTimeout);
resizeTimeout = setTimeout(() => {
isResizing = false;
}, 150);
}}
/>
1 change: 0 additions & 1 deletion src/shaders/fs.frag
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ uniform int iterations;
uniform vec2 center;
uniform float scale;
uniform float resolution;

uniform vec3 boundColor;
uniform vec3 transitionColor;
uniform vec3 escapeColor;
Expand Down

1 comment on commit 48adbbd

@vercel
Copy link

@vercel vercel bot commented on 48adbbd Jul 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.