ComfyUI Node Runs on cloud

Layer Fit

Scale a layer to cover, fit, or match another

By WASasquatch·Created 3 years ago·Updated 4 days ago· 1,844
Layer Fit
  • layers
  • layers
  • width
  • height
resize_mode
against
index-1
layer_name
width1024
height1024
alignmiddle center
scale1.00
target_index0
target_name

Two of the most common composition jobs are, underneath, the same operation: "make this image cover the whole canvas" (a background plate) and "make this badge a fixed height no matter what it arrived as" (an asset on a plate). Both are scale-to-fit - and WAS Layer Fit is the layer-system node that does it without touching the layer's underlying resolution.

Here's the mechanism worth understanding first, because it's the node's whole reason to exist: "Only the drawn size is written, so the layer keeps its full-resolution picture and Create Layered Image resamples it once at the end." A layer in the WAS stack has both a source image and a drawn size. Layer Fit changes the drawn size and leaves the source alone, which means nothing is upscaled or downscaled until the final composite renders - no wasted intermediate resampling, no accumulated blur if you fit, then fit again, then edit.

The three choices that matter:

  • resize_mode - how the layer reaches its box. fit inside keeps the whole layer visible with space left over (letterboxing), fill and overflow covers the box completely and runs past its edges (the background-plate setting - pair it with a crop or let the overflow hide), and stretch takes the box exactly and distorts the picture. For aspect ratios that don't match, fit inside and fill and overflow are the two honest options; stretch is there for when you truly don't care.
  • against - what it's fitted to. the canvas is the document's own size; a size uses the width/height fields you type (the badge-at-fixed-height case); another layer fits to the box that target_index or target_name draws in - making one element match another's footprint without knowing either's dimensions.
  • align - where the scaled layer sits inside the box, from middle center to the corners and edges.

index or layer_name picks the layer that moves (0 from the back, -1 the front), and scale is a multiplier applied after the fit: 1.0 is exactly the fit, 0.9 leaves a margin around the whole thing, 1.1 overshoots it. That's the field that turns "fit to canvas" into "fit to canvas with breathing room" without a second node.

Outputs are layers (the stack with the layer scaled and placed, for Create Layered Image or another edit) plus width and height - the pixel size the layer is now drawn at. Those two numbers are the ones worth wiring onward when something else has to match the result, like a shadow sized to the fitted layer rather than to whatever it was before.

This is the natural partner to Layer Align: Align positions, Fit sizes, Edit fine-tunes. Fit a generated background to cover the canvas, fit a badge to a fixed height, or fit one element to another's box - and because nothing resamples until the end, you can chain several of these without paying for it.

Install

Part of WAS Node Suite v3. ComfyUI Manager, search "WAS Node Suite", or:

cd ComfyUI/custom_nodes
git clone https://github.com/WASasquatch/was-node-suite-comfyui

Restart after. Needs ComfyUI 0.14.0+ and Python 3.10+, no pip packages or model downloads (v3's install is clean - the old pack's dependency pile is gone).

Common issues

  • Layer doesn't fill the frame. resize_mode is on fit inside - switch to fill and overflow for a cover.
  • Everything got distorted. stretch forces the box exactly. Use fit inside or fill and overflow to keep the aspect ratio.
  • Sizing against the wrong thing. Check against: a size uses your typed width/height, the canvas and another layer ignore those fields entirely.
CategoryWAS Suite/Image/Layers

Inputs (11)

NameTypeDefaultDescription
layersLAYERSThe stack holding the layer to scale. Wire in Add Layer, Layer Edit or anything else answering a LAYERS document.
resize_modeCOMBOHow the layer reaches the box. `fit inside` keeps the whole layer visible with space left over; `fill and overflow` covers the box and runs past its edges; `stretch` takes the box exactly and distorts.
againstCOMBOWhat the layer is fitted to. `the canvas` is the document's own size; `a size` is the width and height below; `another layer` is the box the layer target_index or target_name draws in.
indexINT-1-16384–16384Which layer is scaled, counting 0 from the back of the stack. -1 is the front layer. Ignored while layer_name names one.
layer_nameSTRINGName of the layer to scale instead of index. Blank uses index. 'sky' finds a layer called Sky, and finds Sky Backdrop where nothing is called exactly Sky.
widthINT10241–16384Width of the box on `a size`, in pixels. 1024 fits the layer into a 1024-wide box. Ignored by the other two targets.
heightINT10241–16384Height of the box on `a size`, in pixels. 1024 fits the layer into a 1024-tall box. Ignored by the other two targets.
alignCOMBOmiddle centerWhere the scaled layer sits inside the box. `middle center` centres it, `top left` puts its corner on the box's.
scaleFLOAT1.000.01–16Multiplies the fitted size afterwards. 1.0 = exactly the fit, 0.9 = a tenth smaller with a margin round it, 1.1 = a tenth larger.
target_indexINT0-16384–16384Which layer is fitted to on `another layer`, counting 0 from the back. 0 is the back layer, which is usually the plate.
target_nameSTRINGName of the layer to fit to instead of target_index. Blank uses target_index. Read only on `another layer`.

Outputs (3)

NameTypeDescription
layersLAYERSThe stack with the layer scaled and placed, for Create Layered Image or another edit.
widthINTThe width the layer is now drawn at, in pixels.
heightINTThe height the layer is now drawn at, in pixels.