GithubHelp home page GithubHelp logo

cgscratch's Introduction

CGScratch: CoreGraphics scratchcard effect example

What?

An example of using CoreGraphics to achieve a scratch-card effect

Why?

I had to code a scratch-card effect for an iPhone app and started looking around for an example. Did not find any. I did find, however, that this was a frequently asked feature.

So here we are, with a simple implementation of how this could be done.

How?

The ScratchView class implementation loads the background and the ScratchableView as subviews.

The ScratchableView sets the CoreGraphics context upon initialization, with what is appropriate for rendering a scratched image. This entails creating a mask, which is a grayscale CGImage. In a mask, the black color represents whatever is gonna stay opaque, and the white color is what is going to get cut out in the scratchable image, i.e. will become transparent.

We need to be able to change this mask based on user input, and this is done by creating a CGBitmapContext, which I called alphaPixel. This is named thus because this context acts as the alpha channel of the scratchable image.

Touch events are handled to update alphaPixels. In renderLineFromPoint, a line is drawn, and the screen is redrawn.

Now what?

It is the simplest way you could achieve this, I think. Of course, your needs may differ, but it should get you started.

Other ways to do this? Use a CAEAGLLayer, load image as texture, track input, store points, draw.

Make sure to keep track of memory; I did not pay particular attention to memory management in writing this.

Credits for the images go to:

cgscratch's People

Stargazers

 avatar

Watchers

James Cloos avatar  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.