wxlpp / MaLiang

iOS painting drawing library based on Metal. 神笔马良有一支神笔(基于 Metal 的涂鸦绘图库)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

MaLiang

CI Status Version Language codebeat badge License Platform

🎉 MaLiang is on Metal now!

MaLiang is a painting framework based on Metal. The name of "MaLiang" comes from a boy who had a magical brush in Chinese ancient fairy story.

Simplified Chinese

Requirements

iOS 9.0+, Swift 4.2+

The core painting module is based on Metal

You can simply make it compatible with lower version of iOS and swift by changing only serval lines of code.

Installation

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

# 2.0.0 is currently in beta testing
pod 'MaLiang', '~> 2.0.0-beta-2'

To use the old OpenGL ES verion:

pod 'MaLiang', '~> 1.1'

Usage

MaLiang is simple to use.

  1. import MaLiang
  2. enjoy painting!

Canvas

open class Canvas: MetalView

A Canvas is the basic component of MaLiang. You will paint all things on it. Canvas extends from MetalView, whitch extends from MTKView. MetalView handles all the logic with MetalKit and hides them from you.

Canvas can be simply created with xib or code.

  • with xib or storyboard, simply drag and drop an UIView object into your view controller and change it's class to Canvas and module to MaLiang
  • with code, just create with init(frame:) as any UIView you do before.

Now, all things necessary is done!

Snapshot

You can take snapshot on canvas now. Just call snapshot function on Canvas and you will get an optional UIImage object.

Brush

With all things done, you can do more with Brush!

Brush is the key feature to MaLiang. It holds textures and colors, whitch makes it possiable to paint amazing things.

Register a Brush with image data or file to Canvas and paint with it:

let path = Bundle.main.path(forResource: "pencil", ofType: "png")!
let pencil = try? canvas.registerBrush(with: URL(fileURLWithPath: path))
pencil?.use()

Brush have serval properties for you to custmize:

// opacity of texture, affects the darkness of stroke
// set opacity to 1 may cause heavy aliasing
open var opacity: CGFloat = 0.3

// width of stroke line in points
open var pointSize: CGFloat = 4

// this property defines the minimum distance (measureed in points) of nearest two textures
// defaults to 1, this means erery texture calculated will be rendered, dictance calculation will be skiped
open var pointStep: CGFloat = 1

// sensitive of pointsize changed from force, from 0 - 1
open var forceSensitive: CGFloat = 0

/// color of stroke
open var color: UIColor = .black

With all these properties, you can create you own brush as your imagination.

Force & 3D Touch

MaLiang supports automatically adjustment of stroke size with painting force. 3D Touch is supported by default, and simulated force will be setup on devices those are not supporting this.

forceSensitive is the property that force affects the storke size. It should be set between 0 to 1. the smaller the value is, the less sensitive will be. if sets to 0, then force will not affects the stroke size.

Document

Document is now configured by default. It holds all the data on the Canvas, and makes the undo and redo actions to be possiable.
And you can implement your own saving logic with the data holds by Document.

TODO

  • Port to Metal
  • Undo & Redo
  • Export to image
  • Optimize for scale
  • Optimize for saving logic
  • Glow style line
  • Text element
  • Image element
  • Texture rotation

License

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

About

iOS painting drawing library based on Metal. 神笔马良有一支神笔(基于 Metal 的涂鸦绘图库)

License:MIT License


Languages

Language:Swift 94.8%Language:Metal 4.2%Language:Ruby 1.0%