https://github.com/fwcd/swift-graphics
Cross-platform 2D graphics library for Swift
https://github.com/fwcd/swift-graphics
cairo graphics hacktoberfest linux swift
Last synced: about 1 year ago
JSON representation
Cross-platform 2D graphics library for Swift
- Host: GitHub
- URL: https://github.com/fwcd/swift-graphics
- Owner: fwcd
- License: mit
- Created: 2020-09-29T20:18:19.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-09-29T20:58:36.000Z (over 1 year ago)
- Last Synced: 2025-04-10T15:23:50.617Z (about 1 year ago)
- Topics: cairo, graphics, hacktoberfest, linux, swift
- Language: Swift
- Homepage:
- Size: 74.2 KB
- Stars: 27
- Watchers: 5
- Forks: 6
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Swift Graphics
[](https://github.com/fwcd/swift-graphics/actions/workflows/build.yml)
2D drawing library for Swift with multiple backends, currently including
- Cairo (cross-platform)
- Core Graphics (Apple platforms)
## Example
```swift
import PlatformGraphics
import Utils
// Create a new image and a graphics context
let ctx = try PlatformGraphicsContext(width: 300, height: 300)
// Draw some shapes
ctx.draw(line: LineSegment(fromX: 20, y: 20, toX: 50, y: 30))
ctx.draw(rect: Rectangle(fromX: 80, y: 90, width: 10, height: 40, color: Colors.yellow))
ctx.draw(text: Text("Test", at: Vec2(x: 0, y: 15)))
ctx.draw(ellipse: Ellipse(centerX: 150, y: 80, radius: 40))
ctx.draw(polygon: Polygon(points: [
Vec2(x: 210.0, y: 150.0),
Vec2(x: 100.0, y: 200.0),
Vec2(x: 170.0, y: 250.0)
], isFilled: false))
// Encode the image to a byte buffer
let image = try ctx.makeImage()
let data = try image.pngEncoded()
```
The full example can be found in [`Snippets/DrawShapes.swift`](Snippets/DrawShapes.swift). To run it, invoke
```sh
swift run DrawShapes Output/shapes.png
```
The resulting PNG will be written to `Output/shapes.png`.
## System Dependencies
* Swift 5.7+
* For the Cairo backend (required on Linux, optional on macOS):
* Debian: `apt-get install libcairo2-dev`
* macOS: `brew install cairo`
## Building
```sh
swift build
```
## Testing
```sh
swift test
```