Coder Social home page Coder Social logo

depict's Introduction

DePict - A simple, declarative, functional drawing framework.

To produce a drawing, call the Draw function (just type Draw and let autocomplete do the rest!).

Pass Draw a Colorer. There are currently two ways to color something. Type either: • Filled, for filled-in shapes, or • Outlined, for outlined or “stroked” shapes.

And then provide a color -- Black, Yellow, Red, or create anything you like.

Now you can specify the Shape to be drawn: • LineRectangleCircle • & more

The end result of this is that your code reads like the thing you're actually drawing: Draw(colorer: Filled(color: Blue, shape: Circle(…))) “Draw a Filled Blue Circle”


Drawing a Face

We can draw a face in just a single line of readable, declarative code. See how the pieces reflect the final result.

		Draw(colorer:
			// Hair (behind head)
			Filled(color: Black, shape: Circle(centerX: 50, Y: 58, radius: 40))
			
			// Face
			+ Filled(color: Brown, shape: Circle(centerX: 50, Y: 50, radius: 40))
			
			// Eyes and pupils
			+ Filled(color: White, shape: Rectangle(x: 20, y: 50, width: 20, height: 8))
			+ Filled(color: Blue, shape: Circle(centerX: 30, Y: 54, radius: 2))
			+ Filled(color: White, shape: Rectangle(x: 60, y: 50, width: 20, height: 8))
			+ Filled(color: Blue, shape: Circle(centerX: 70, Y: 54, radius: 2))
			
			// Nose
			+ Outlined(color: Light(Brown), shape:
				Line(fromX: 50, y: 54, toX: 56, y: 44)
				+ Line(fromX: 56, y: 44, toX: 52, y: 42)
			)
			
			// Mouth
			+ Outlined(color: Light(Red), shape:
				Line([(40, 30), (46, 25), (54, 25)])
			)
		)

A Face

Drawing Information graphics

It’s trivial to use DePict to turn data into information graphics, and the declarative style allows you to do so in an obvious, clear way.

  1. Start with some data you want to graph…
    let data = [11, 87, 98, 48, 41, 88, 63, 69, 8, 79]

  2. For each datum, create a filled rectangle with height relative to its value. let bars = Array(0 ..< data.count).map({ Filled(color: Magenta, shape: Rectangle(x: $0 * 10, y: 0, width: 5, height: data[$0])) })

  3. Combine the rectangles. let graphData = bars.reduce(EmptyColorer(), combine: +)

Bar Charts


DePict is a super-simple wrapper for CoreGraphics that makes your drawing easy to ready and modify. It works great for iOS and Mac apps. Anything you would use CoreGraphics for, you can express more simply and easily with DePict.

If you come up with something fun, fork the code, or tweet me @davidcairns!

depict's People

Contributors

davidcairns avatar texastoland 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.