#define SCALE 8.0
#define R iResolution.xy
#define M iMouse

void mainImage( out vec4 RGBA, in vec2 XY )
{
    float t = iTime/2.0;
    vec2 mc = M.xy/R; // mouse coords
    vec2 sc = (XY-.5*R)/R.y*SCALE; // screen coords
    float x = sc.x;
    float y = sc.y;
    
    // transform
    float eq = (x*x+y*y)-1.0; // equation
    float x2 = eq/x; // x to this
    float y2 = eq/y; // y to this
    float ss = 4.0; // snap strength (less is more)
    float snap = round(mc.x*ss)/ss; // round near ints
    if (mod(snap, 1.0) < 0.01) mc.x = snap; // snap mouse x to int when close
    if (M.z < 1.0) mc.x = (cos(t)+1.0)/2.0; // animate when not clicking
    float mxt = 1.0-mc.x; // mouse x transform
    sc.x = (x*mxt)+(x2*mc.x); // transform x
    sc.y = (y*mxt)+(y2*mc.x); // transform y
    
    vec3 c = vec3(0);
    vec2 g = abs(fract(sc+0.5)-0.5)/fwidth(sc); // grid
    c += max(0.0, 1.0-min(g.x, g.y))*0.25; // lines
    c += max(0.0, 1.0-max(g.x, g.y)/3.0)*0.5; // points
    c += texture(iChannel0, sc, 1.0).rgb; // texture
    
    RGBA = vec4(c, 1.0);
}
