Over a million developers have joined DZone.

FurrySketch: Hirsute Drawing with an Apple Pencil

DZone's Guide to

FurrySketch: Hirsute Drawing with an Apple Pencil

How to use orientation data for the pencil to affect brush strokes.

· Mobile Zone
Free Resource

After my recent experiments (mis)using my Apple Pencil for nefarious and alternative uses, I thought it was about time to play with the Pencil for its intended purpose, sketching, and see how I could use its orientation data to affect brush strokes. 

FurrySketch is a Pencil powered drawing application that draws a sort of multicoloured hair and, most excitingly, the hair's direction matches the angle of the Pencil. It was super simple to write and, at least in my opinion, gives really nice results.

The basic mechanics are lifted from my ForceSketch project: I use a CIImageAccumulator with each new bitmap (created inside touchesMoved) composited over the previously accumulated images with a Core Image CISourceOverCompositing filter.  

The interesting part for Pencil fans is creating the hairy brush strokes:

Hirsute Brush Mechanics

Inside touchesMoved, I loop over the coalesced touches (you can read about coalesced touches in my Advanced Touch Handling blog post). For each of the intermediate touches, I want to find its location in the view, its altitude angle and its azimuth vector. This vector points in the direction of the Pencil's azimuth angle and by multiplying it with the normalised altitude angle, I get an offset I can use when drawing my little hairs:

for coalescedTouch in coalescedTouces
        let touchLocation = coalescedTouch.locationInView(view)

        let normalisedAlititudeAngle =  (halfPi - touch.altitudeAngle) / halfPi
        let dx = coalescedTouch.azimuthUnitVectorInView(view).dx * 20 * normalisedAlititudeAngle
        let dy = coalescedTouch.azimuthUnitVectorInView(view).dy * 20 * normalisedAlititudeAngle

I then use the touch's force to decide how many hairs to draw...

    let count = 10 + Int((coalescedTouch.force / coalescedTouch.maximumPossibleForce) * 100)

Now I iteration count times. With each iteration, I create a random angle and create constants for the inner radius and start point for each hair:

    let innerRandomRadius = drand48() * 20
    let innerRandomX = CGFloat(sin(randomAngle) * innerRandomRadius)

    let innerRandomY = CGFloat(cos(randomAngle) * innerRandomRadius)

Although the start point of the hair isn't affected by the Pencil's orientation, the end point is. Here, I create another, larger, random radius, use the same angle and offset the end point by dx and dy I created above:

 randomRadius = innerRandomRadius + drand48() * 40 * Double(normalisedAlititudeAngle)
    let outerRandomX = CGFloat(sin(randomAngle) * outerRandomRadius) - dx

    let outerRandomY = CGFloat(cos(randomAngle) * outerRandomRadius) - dy

With those values, I can draw the hairs to my context and repeat over:

        touchLocation.x + innerRandomX,
        touchLocation.y + innerRandomY)

        touchLocation.x + outerRandomX,
        touchLocation.y + outerRandomY)


A quick reminder on CIImageAccumulator: once the big, hairy loop is finished, I can get the newly generated image from the context and use the CISourceOverCompositing filter to composite that image for that touch move over the previous and finally display it in an UIImageView:

 let drawnImage = UIGraphicsGetImageFromCurrentImageContext()


    compositeFilter.setValue(CIImage(image: drawnImage),
        forKey: kCIInputImageKey)
        forKey: kCIInputBackgroundImageKey)

    imageAccumulator.setImage(compositeFilter.valueForKey(kCIOutputImageKey) as! CIImage)

    imageView.image = UIImage(CIImage: imageAccumulator.image())

In Conclusion

If you are writing drawing apps, adding Pencil support is not only super easy, it adds real value for your users. The technique I've used here to draw hair is only a few lines of code way from spray cans and air brushes and I genuinely believe the iPad Pro will prove to be an amazing device for creatives.

As always, the source code for this project is available at my GitHub repository here. Enjoy and happy Thanksgiving fromFurry Sketch!

apple pencil ,swift ,ios

Published at DZone with permission of Simon Gladman, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}