//By Sean Irby-2015
//sean.t.irby@gmail.com

void mainImage( out vec4 fragColor, in vec2 fragCoord )
{

    //normalize x and y to [-1, 1]
    vec2 r =  2.0*vec2(fragCoord.xy - 0.5*iResolution.xy)/iResolution.y;


    //rotate the coordinate according to its distance from origin and time
    float dist = distance(r, vec2(0, 0));
	float angle = dist*(iTime/3.0)*sin(iTime/3.0)/(3.0 + 3.0*sin(iTime));
    mat2 rotationMatrix = mat2(cos(angle), -sin(angle),
                               sin(angle),  cos(angle));
    r = r*rotationMatrix;

    //mirror coordinates/scale them up
    float scale = 11.0 + 10.0*cos(iTime/5.0);
    vec2 rScaled = vec2(abs(r.x)*scale, abs(r.y)*scale);

    //truncate to grid, mess with coordinates
    vec2 rGrid = vec2(mod(rScaled.x - iTime/2.0, 1.0), mod(rScaled.y, 1.0));
    vec3 col1 = vec3(0.216, 0.471*(1.0-dist)*(1.0 + sin(iTime/2.5)), 0.698);
	vec3 col2 = vec3(1.00, 0.329, 0.6*(1.0-dist));

    //draw diagonal lines in each grid at the defined thickness
    float thickness = 1.1*dist + dist*sin(iTime/3.0);
    float line = smoothstep(rGrid.x - thickness, rGrid.x, 1.0 - rGrid.y);
    line = min(line, 1.0-smoothstep(rGrid.x, rGrid.x + thickness, 1.0 - rGrid.y));

    //erase past a certain distance
    if(rScaled.y > ((50.0 + 11.0*sin(iTime*3.0)) - rScaled.x)){
        line = 0.0;
    }

    //blend colors
    vec3 ret = mix(col1, col2, mix(line, 1.0-line, 1.5 + 0.5*cos(iTime/3.0)));

    fragColor = vec4(ret, 1.0);
}
