Git Product home page Git Product logo

metaballs's Introduction

Blob effect in iOS

Swift 5.0 iOS 9+ swiftlint

The project demonstrates implementation of a 2D blob effect (or metaballs) written using UIKit. Project contains example setup and separated MetaballsView with the implementation of an effect.

Overview

Mostly inspired by these two articles I implemented the effect utilizing Bezier curves with the help of UIKit. During the R&D process, I tried a couple of approaches (like building pixel-by-pixel, marching squares and some more), but due to the nature of the rendering process on a mobile platform selected one is the most efficient. Each time a user move a ball (changes its position), the host view recalculates curves for each pair of balls.

Implementation details

There's a host view class MetaballsView which is responsible for building, configuring and interaction.

It holds a list of interactable UIViews (yep, it holds it twice due to the hierarchy, but in that case, it doesn't create any retain issues).

blobLayers is a two-dimensional array of CAShapeLayers that used to display blob connection between balls. To be able to directly access layer between i-th and j-th balls, the array is made two-dimensional so it could be accessed like blobLayers[i][j]. Due to mirroring, blobLayers[j][i] should be drawn the same as blobLayers[i][j], but I haven't optimized it yet, just ignoring the path.

The Blob entity calculates and holds information regarding tangent points and handle curves points.

The Metaball entity holds actual ball information and utilizes Blob to build a path with another Metaball and translate it into UIBezierPath. Basically, these two classes could be used to implement the same effect independently from UIKit.

The following image demonstrates the principle behind math.

There're two bezier curves: from point p1 to p3 with control points in h1 and h3, and inversed one from points p2 to p4 through h2, h4. Refer to debugging branch to view details.

For pixel-by-pixel implementation refer to this branch.

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.