vec2 fitImage(vec2 st, vec2 imageResolution, vec2 frameResolution, int fit) {
    float imageAspect = imageResolution.x / imageResolution.y;
    float frameAspect = frameResolution.x / frameResolution.y;
    float imageFrameRatio = imageAspect / frameAspect;

    float scaleX = 1., scaleY = 1.;

    // Cover
    if(fit == 0) {
        // Portrait
        if(imageAspect < frameAspect) {
            scaleY = imageFrameRatio;
        } 
        // Landscape
        else if(imageAspect > frameAspect) {
            scaleX = 1. / imageFrameRatio;
        }
    } 
    // Contain
    else if(fit == 1) {
        if(imageAspect < frameAspect) {
            scaleX = 1. / imageFrameRatio;
        } 
        // Landscape
        else if(imageAspect > frameAspect) {
            scaleY = imageFrameRatio;
        }
    }

    vec2 imageScale = vec2(scaleX, scaleY);
    vec2 coordinates = imageScale * (st - 0.5) + 0.5;

    if(coordinates.x < 0. || coordinates.x > 1. || coordinates.y < 0. || coordinates.y > 1.) {
        discard;
    }

    return coordinates;
}