Showing posts with label core graphics. Show all posts
Showing posts with label core graphics. Show all posts

Tuesday, December 10, 2019

Painting with Trigonometry

I'm excited to share today's image generated from the application under development:


It's a pretty organic background-pattern sort of texture, but it highlights a few cool things about the app, which is focused on creating beautiful images with mathematical techniques.

Saturday, November 30, 2019

Dev notebook: brush-like drawing in Swift, without CGPattern

I'm spending a lot of time in Cocoa drawing and Core Graphics lately, working in Swift.

The API around the Core Graphics CGPattern object in Swift is a little challenging - it requires C callbacks and unsafe pointers for basic pattern-creation and drawing functionality. It also doesn't work exactly as I'd like it to; I want to have more of a 'brush' metaphor where I can vary the 'paint' flow of the pattern being used to draw a line or a curve. But I definitely want a pattern-like construct that I can apply different stroke and fill colors to in order to draw.

My solution is to use NSImage, which is really convenient because it's a high-level object in Swift that can easily be created in memory or loaded from a .png resource in the app bundle.

The approach: given an NSImage that represents your brush pattern, create a copy NSImage that is rendered in the desired stroke color, then use that copy to draw your desired lines and curves, or tile it along with clipping to fill an area. I'll include an example of drawing a line with varying density, and of filling an area with clipping and offset adjustment.