DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • GraphQL Revisited: Adoption in Blockchains
  • 4 Best Practices for IoT OTA Updates
  • Kalman Filters With Apache Spark Structured Streaming and Kafka
  • How to Prevent Your Java Collections From Wasting Memory

Trending

  • Harnessing the Power of In-Memory Databases: Unleashing Real-Time Data Processing
  • Essential Complexity Is the Developer's Unique Selling Point
  • Freedom to Code on Low-Code Platforms
  • No Spark Streaming, No Problem
  1. DZone
  2. Data Engineering
  3. Data
  4. FurrySketch: Hirsute Drawing with an Apple Pencil

FurrySketch: Hirsute Drawing with an Apple Pencil

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

Simon Gladman user avatar by
Simon Gladman
·
Dec. 05, 15 · Tutorial
Like (1)
Save
Tweet
Share
3.03K Views

Join the DZone community and get the full member experience.

Join For Free

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:

    CGContextMoveToPoint(cgContext,
        touchLocation.x + innerRandomX,
        touchLocation.y + innerRandomY)

    CGContextAddLineToPoint(cgContext,
        touchLocation.x + outerRandomX,
        touchLocation.y + outerRandomY)


    CGContextStrokePath(cgContext)

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()

    UIGraphicsEndImageContext()

    compositeFilter.setValue(CIImage(image: drawnImage),
        forKey: kCIInputImageKey)
    compositeFilter.setValue(imageAccumulator.image(),
        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!








ANGLE (software) Brush IPad Pro Filter (software) Data structure app application

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

Opinions expressed by DZone contributors are their own.

Related

  • GraphQL Revisited: Adoption in Blockchains
  • 4 Best Practices for IoT OTA Updates
  • Kalman Filters With Apache Spark Structured Streaming and Kafka
  • How to Prevent Your Java Collections From Wasting Memory

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: