Coder Social home page Coder Social logo

alessandrostone / trianglifyswift Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bohdanorlov/trianglifyswift

0.0 1.0 0.0 2.93 MB

Triangle background and view generator. This library generates container view full of configurable triangles which are regular UIViews and can be animated.

License: MIT License

Swift 93.13% Ruby 4.95% Objective-C 1.92%

trianglifyswift's Introduction

TrianglifySwift

Version License Platform

Algorithmically generated triangle art:

Quickstart

Adding default random triangles view to your View Controller

let trianglesView = TriangleContainer()
self.view.addSubview(trianglesView)

Adding configurable triangles to your View

let verticesConfig = VerticesGenerator.Configuration(size: self.bounds.size)
let vertices = VerticesGenerator.generate(configuration: verticesConfig)
let triangles = Delaunay.triangulate(vertices)
let style = self.trianglesStyle ?? Style() // provide options to style here
for triangle in triangles {
    let triangleView = TriangleView(triangle:triangle, style: style)
    self.triangleViews.append(triangleView)
    self.addSubview(triangleView)
}

See example project for animation and styling examples.

Example

To run the example project, clone the repo, and run as usual.

Configuration

Triangles Generation

Vertices

You can supply your own vertices for triangles generation or use VericesGenerator and supply appropriate Configuration. Configuration accepts next arguments:

size

Width and height of the area which is used to generate vertices.

cellSize

Float, defaults to 80.0. Specify the size of the mesh used to generate triangles.

Larger values will result in coarser patterns, smaller values will result in finer patterns. Note that very small values may dramatically increase generation time.

variance

Decimal value between 0 and 1 (inclusive), defaults to 0.75. Specify the amount of randomness used when generating triangles.

seed

Unsigned integer, defaults to arc4random(). Seeds the random number generator to create repeatable patterns.

When set to arc4random(), the random number will be seeded with random values. Pass the same value all the time if you want triangles to not change between runs.

Triangles Styling

colorsX, colorsY

Array of UIColors used for color gradient on the x/y axis. Default is 3 random colors.

Valid array values should specify the color stops (i.e.[UIColor.cyan, UIColor.yellow, UIColor.orange, UIColor.blue]).

fillColorClosure, strokeColorClosure

Specify a custom closure for coloring triangles, defaults is closure producing gradient. Accepts a function to override the standard gradient coloring that takes the triangle and returns a color that triangle should have.

let rainbowColorClosure: (TriangleView) -> UIColor = { triangleView in
    let point = triangleView.relativePositionInSuperview()
    let color = UIColor(hue: (point.x + point.y) / 2.0, saturation: 0.8, brightness: 0.8, alpha: 1)
    return color
}
TrianglifySwift.Style(fillColorClosure:rainbowColorClosure,
                     strokeColorClosure:rainbowColorClosure)

strokeLineWidth

Float, defaults to 0.51. Specify the width of the stroke on triangle shapes in the pattern. The default value is used for eliminating antialiasing artifacts (gaps) when shape layers are rendered.

Installation

Requirements

iOS 9+

Swift 3

Carthage

To use latest release add this to Cartfile:

git "https://github.com/BohdanOrlov/TrianglifySwift"

Then run in terminal:

$ carthage update

CocoaPods

TrianglifySwift is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "TrianglifySwift"

Due to Swift 3, you have to add this to the end of Podfile:

post_install do |installer|
    installer.pods_project.targets.each do |target|
        target.build_configurations.each do |config|
            config.build_settings['SWIFT_VERSION'] = '3.0'
        end
    end
end 

Author

Bohdan Orlov, [email protected]

Credits

Emerged from:

Original Trianglify JS

Swift Delaunay Triangulation Trianglify JS

License

TrianglifySwift is available under the MIT license. See the LICENSE file for more info.

trianglifyswift's People

Contributors

bohdanorlov avatar

Watchers

James Cloos 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.