Giter Site home page Giter Site logo

ultimatedoge5 / metaballs Goto Github PK

View Code? Open in Web Editor NEW
5.0 1.0 0.0 6.12 MB

TypeScript implementation of metaballs, organic-like shapes, using canvas api.

Home Page: https://metaballs.pkozak.org/

HTML 5.89% TypeScript 90.70% CSS 3.41%
metaballs typescript canvas marching-squares linear-interpolation

metaballs's Introduction

Metaballs

In this project, I wanted to make my implementation of Metaballs in TypeScript. You can see it for yourself here.

Metaballs gif

What are those metaballs?

A metaball is an isoline1 shape that has an organic cell-like ability to merge with other metaballs.

How does it work?

First, we calculate the value for every cell using a function. The function takes contributions from each circle. The cells which value is greater than 1 are inside the metaball.

We could change the colour of every pixel if its value is greater than one and call it a day. But if we do some quick maths and calculate how many pixels there are in Full HD resolution - 2 073 600 pixels. We would have to check every pixel in a single frame, and we would have to render 60 frames per second which equals 124 416 000 operations per second. Yeah, that's a little too much, even for today's processors. So how can we optimize it?

Marching squares

Enter the marching squares algorithm. What it does is - it takes the calculated value of every corner of our cell and assigns it a state. There are 16 possible states (2⁴). Based on a state, we draw a line for every cell.

Marching squares We get some fast but very blocky metaballs. Of course, we can pump up the resolution, but that would result in only a little less blocky metaballs.

Linear interpolation

At the moment, for every state, we have a hardcoded line. And if that line doesn't start or end on one of the corners, it does that from the middle of the cell. If there only was a way to calculate those positions on the go. The linear interpolation algorithm comes to the rescue! By adding the lerp output to the starting/ending points of the lines. And decreasing the cell size to 10px. We get this!

Marching squares with linear interpolation

Sources

Footnotes

  1. Isoline - a curve of a mathematical function.

metaballs's People

Contributors

ultimatedoge5 avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 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.