Git Product home page Git Product logo

warp.js's Introduction

Warp.js

A simple canvas warp deformation in JavaScript. A graphics effects doing "pinch and rotate" of the canvas. All the basic control parameters are available. Combine with animations to do cool effects. Here are some live demos and more documentation.

warp effect

Basic Usage

You will need two canvas elements, an input one, and viewport one. viewport should probably be smaller than input since you will want to have enough room around for the effect to grab the pixels from (in case the warping stretches beyond the size of viewport).

Create a new Warp object providing it with those canvas elements, plus top and left coordinates for the viewport (i.e. where it is virtually placed on the input canvas).

Draw your stuff on the input canvas, then call deform() supplying actual warping parameters: center, radius and angle.

// assuming you have jQuery
var warp = new Warp({
	input_canvas: $('#input_canvas').get(0),
	viewport_canvas: $('#viewport_canvas').get(0),
	top: 100,
	left: 100
});

warp.deform({
	center: {x: 100, y: 100},
	radius: 50,
	angle: 45
});

Additionally you can also provide the transformation function, which determines how the rotation angle changes with distance to the origin of deformation.

warp.deform({
	center: {x: 100, y: 100},
	radius: 50,
	angle: 90,
	func: function (d) { return 1 + Math.sin( (d + 2) * Math.PI / 2 ) }
});

The function takes one floating point argument from 0.0 to 1.0, being the distance from origin (0.0 is the center of warping, 1.0 the edge of it) and returns a value between 0.0 and 1.0 representing the relative angle of rotation at this distance (0.0 being no rotation at all, 1.0 being the full angle). That means you probably want a function f which satisfies the following conditions:

f(0) = 1 (meaning at the center of warping make full rotation)

f(1) = 0 (meaning at the edge of warping make no rotation at all)

Example custom functions

Here are a few example custom functions you can use. Refer to my blog post about this effect for a more visual explanation of custom functions: http://www.tinkeringwithstuff.com/posts/canvas-warping-with-javascript.html

  • Just rotate by angle. That will look like a cut-out since every pixel inside the radius will be rotated by the same angle, irrespective of its distance to the center of deformation:

    function(d) {
        return 1;
    }
  • Linear. This will linearly "interpolate" the rotation angle from no rotation at the edge to full rotation in the origin. That's the default if you don't provide a custom function and looks like a basic warp:

    function(d) {
        return 1.0 - d;
    }
  • Sine. Smooth start and smooth end, like an S-shaped curve:

    function(d) {
        return 0.5 + Math.sin( (d + 0.5) * Math.PI ) / 2;
    }
  • Sine 2. Smooth and slow start, sharp end:

    function(d) {
        return 1 + Math.sin( (d + 2) * Math.PI / 2 );
    }

License

Do-whatever-you-want-with-it license.

warp.js's People

Contributors

soquel avatar

Watchers

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