Code Monkey home page Code Monkey logo

pathology's Introduction

Pathology

Build Status CocoaPods Compatible Carthage Compatible codecov.io

Pathology is a library for encoding/decoding CGPath data.

Install

pod 'Pathology'

Extracting CGPath data

let bezierPath = UIBezierPath(
    roundedRect: CGRect(x: 50, y: 50, width: 200, height: 200),
    cornerRadius: 10)

let pathData = Pathology.extract(bezierPath.CGPath)
for el in pathData.elements {
    print("\(el.type)")
    print("\t\(el.points)")
}

Result ↓

MoveToPoint
  [(65.28665, 50.0)]
AddLineToPoint
  [(234.71335, 50.0)]
AddCurveToPoint
  [(243.300654247945, 50.654958930244), (239.115070423815, 50.0), (241.31593055937, 50.0)]
AddLineToPoint
  [(243.68506207169, 50.7491138784702)]
AddCurveToPoint
  [(249.25088612153, 56.3149379283099), (246.271761733743, 51.6905955604437), (248.309404439556, 53.7282382662575)]
AddCurveToPoint
  [(250.0, 65.2866498472958), (250.0, 58.68406944063), (250.0, 60.8849295761853)]
AddLineToPoint
  [(250.0, 234.71335)]
AddCurveToPoint
  [(249.345041069756, 243.300654247945), (250.0, 239.115070423815), (250.0, 241.31593055937)]

etc...

Encode JSON

let bezierPath = UIBezierPath(rect: CGRect(x: 50, y: 50, width: 200, height: 200))
let pathData = Pathology.extract(bezierPath.CGPath)

do {
  let json = try pathData.toJSON(NSJSONWritingOptions(rawValue: 0))
} catch {
  print("\(error)")
}

Result ↓

[
  {"points":[[50,50]],"type":"moveToPoint"},
  {"points":[[250,50]],"type":"addLineToPoint"},
  {"points":[[250,250]],"type":"addLineToPoint"},
  {"points":[[50,250]],"type":"addLineToPoint"},
  {"points":[],"type":"closeSubpath"}
]

Decode JSON

Expects array payload like encoded result above ↑

if let pathData = Path(JSON: someJSONdata) {
  let bezierPath = UIBezierPath(CGPath: pathData.CGPath())
}

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.