Drop Shadow

← All Butter documentation

Adds a shadow to an image, with presets like the ones in Butter's own shadow effect.

Choosing a style sets the blur, the offset and the color to match it, and the fields that only matter for a custom shadow stay hidden until you pick Custom. This uses .changed(), .value(), .show() and .hide().

let img
let style
let blur
let offsetX
let offsetY
let shadowColor

async function setup() {
  img = await loadImage('').updatesSize()

  style = createSelect()
  style.option('Soft', 'soft')
  style.option('Regular', 'regular')
  style.option('Retro', 'retro')
  style.option('Custom', 'custom')
  style.selected('regular')

  // These start out as the values of the Regular style. How far the shadow
  // reaches decides how much room it needs, so they update the size.
  blur = createSlider(0, 20, 10).updatesSize()
  offsetX = createSlider(-100, 100, 0).updatesSize()
  offsetY = createSlider(-100, 100, 8).updatesSize()
  shadowColor = createColorPicker('#000000')

  style.changed(() => {
    const choice = style.value()

    if (choice === 'soft') {
      blur.value(20)
      offsetX.value(0)
      offsetY.value(6)
      shadowColor.value('#000000')
    } else if (choice === 'regular') {
      blur.value(10)
      offsetX.value(0)
      offsetY.value(8)
      shadowColor.value('#000000')
    } else if (choice === 'retro') {
      blur.value(0)
      offsetX.value(8)
      offsetY.value(8)
      shadowColor.value('#000000')
    }

    // Only a custom shadow needs the details
    for (const field of [blur, offsetX, offsetY, shadowColor]) {
      if (choice === 'custom') {
        field.show()
      } else {
        field.hide()
      }
    }
  })

  // Fit the image into a 200 by 200 box, and make the canvas that size
  const fit = min(200 / img.width, 200 / img.height)
  createCanvas(img.width * fit, img.height * fit)

  // Leave room outside the canvas for the shadow to be drawn in
  canvasPadding(
    abs(offsetX.value()) + blur.value(),
    abs(offsetY.value()) + blur.value(),
  )
}

function draw() {
  clear()

  // Canvas shadows aren't scaled with the pixel density, so do it here
  const density = pixelDensity()
  drawingContext.shadowColor = shadowColor.value()
  drawingContext.shadowBlur = blur.value() * density
  drawingContext.shadowOffsetX = offsetX.value() * density
  drawingContext.shadowOffsetY = offsetY.value() * density

  image(img, 0, 0, width, height)
}
The Butter logo in black with a pink shadow below it

A few things to notice:

  • Regular is the style the sketch starts on, so the sliders start with its values. A style only sets the fields when you pick it.
  • The canvas is the size of the image, and canvasPadding leaves room outside it for the shadow. That way the component's bounds fit the image and not its shadow. Changing the blur or the offset changes how much room is needed, so those fields use .updatesSize().
  • The blur, offset and color fields are always read in draw, whether or not they are hidden, so a hidden field still affects the image.