Skip to content
Advertisement

Pinch/pucker an image in canvas

How can I pinch/pucker some area of an image in canvas?

I’ve made a solar system animation some time ago, and I started rewriting it. Now, I want to add gravity effect to masses. To make the effect visible, I turned the background into a grid and I’ll be modifying it.

Desired effect is something like this (made in PS)

enter image description here

enter image description here


JavaScript

jsFiddle


Update: I’ve done some googling and found some resources, but since I’ve never done pixel manipulation before, I can’t put these together.

Pixel Distortions with Bilinear Filtration in HTML5 Canvas | Splashnology.com (functions only)

glfx.js (WebGL library with demos)

JSFiddle (spherize, zoom, twirl examples)

The spherize effect in inverted form would be good for the job, I guess.

Advertisement

Answer

I’ve had time to revisit this problem and came up with a solution. Instead of solving the problem directly, first, I needed to understand how the math behind the calculation and pixel manipulation works.

So, instead of using an image/pixels, I decided to use particles. A JavaScript object is something I’m much more familiar with, so it was easy to manipulate.

I’ll not try to explain the method because I think it’s self-explanatory, and I tried to keep it as simple as it can get.

JavaScript
JavaScript
JavaScript

I might try to create twirl effect some other time, and move these into WebGL for better performance.


Update:

Now, I’m working on the twirl effect, and I’ve made it work to some degree.

JavaScript
JavaScript
JavaScript

There is a slight issue with the mapping of strength of the twirl. I’ve used the same function map that I’ve used with pinch effect, but I think twirl doesn’t use linear mapping, but eased mapping. Compare the JS version with the PS filter. PS filter is smoother. I need to rewrite the map function.

enter image description here

Update 2:

I’ve managed to make it work the same way PS filter does. Using an ease function, i.e., easeOutQuad solved the problem. Enjoy 🙂

JavaScript
JavaScript
JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement