Git Product home page Git Product logo

canvas-sketch's Introduction

canvas-sketch

Transform an image into a pencil-sketch, using the <canvas> element.

The implemenation is not exactly efficient, so when you check out the demo page, try starting with a small image (a couple of hundred pixels across).

The algorithm

The principle is fairly simple. At each point in the image, the hue is converted into an angle of shading (so blue and yellow are at right-angles to each other). The saturation is converted into consistency of shading angles (so grey will be cross-hatched, grey-blue will be a bit rough-looking, and pure blue will be completely in line). Brightness is obviously represented by density of lines.

The actual method is a bit of a hack - we create image-size pencil textures (using randomly drawn pencil lines with a specified thickness, length and density) for a few different RGB values. The spacing of these values is determined by the "Level steps" control (so, 2 means that it will cover (0, 0, 0), (1, 0, 0) and so on - 3 means it will cover (0, 0, 0), (0.5, 0, 0) and so on).

Then, we simply composite these textures together (fading between them), and add some basic edge-detection to simulate outlines.

Batch processing

I hacked together a batch-processing system as a PHP script. It downloads one image at a time, processes it, and POSTs it back to the server.

The config is in batch-animation.json. Note the lineAlphaVariation and edgeAmountVariation properties, which randomise their corresponding parameters, resulting in a more or less coarse image.

The goal is to replicate the effect of tracing video frames using pencil sketches, where each frame will not turn out completely consistently:

GIF animation, from a video of a fruit bowl

License

The code is available as "public domain", meaning that it is completely free to use, without any restrictions at all (including re-licensing under your own terms). Read the full license here.

It's also available under an MIT license.

canvas-sketch's People

Contributors

geraintluff avatar

Watchers

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