Components

Image cropper

A crop of an image dragged and resized by handles and keys, zoomed, turned, locked to an aspect or round, and exported from a canvas.

Previews are drawn in the theme you made.

Edit it
guides
handles
viewport
scheme
direction

Free crop #

Crop the photo
Zoom
import { Bundles, ImageCropper, ImageCropperSlots, Recipes } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { LANDSCAPE } from '../../snippets/landscape.js'

// The zoom is a Slider you pass, so a cropper given none ships none; its
// label is the cropper's to say.
const Zoom = Recipes.Slider.style({ label: 'hidden' })

// The crop, zoom and turn are the cropper's Model, placed by
// `Bundles.ImageCropper`; the image is the page's. Drag the crop or its
// handles, or move it with the arrow keys once it has focus.
export default example({
  title: 'Free crop',
  slots: ImageCropperSlots,
  recipe: Recipes.ImageCropper,
  state: Bundles.ImageCropper.with({ id: 'photo' }),
  view: ({ style, model, onMessage, h }) =>
    ImageCropper.view(
      {
        model,
        onMessage,
        label: 'Crop the photo',
        src: LANDSCAPE,
        zoom: ImageCropper.zoomSlider(Zoom),
        style,
      },
      h,
    ),
})

In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.

Round, square, exported #

Profile picture
import { Option, Schema } from 'effect'
import { Bundle } from 'foldkit-bundle'
import {
  Avatar,
  Bundles,
  Button,
  ImageCropper,
  ImageCropperSlots,
  Recipes,
} from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { row, stack } from '../../snippets/layout.js'
import { LANDSCAPE } from '../../snippets/landscape.js'

const Use = Recipes.Button.style({ variant: 'outline', size: 'sm' })
const Picture = Recipes.Avatar.style({ size: 'lg' })

// A round crop locked square. The page asks for the crop with
// `RequestedExport`, sent to the placed child, and the Bundle reports
// `Exported` with a data URL, which the page keeps to show.
const App = Bundle.compose({ picture: Schema.Option(Schema.String) }).pipe(
  Bundle.withChild('crop', Bundles.ImageCropper, {
    args: { id: 'avatar-crop', shape: 'round', aspect: 1 },
    onOut: out => model => ({
      model: ImageCropper.OutMessage.match(out, {
        Exported: ({ url }) => ({ ...model, picture: Option.some(url) }),
        FailedExport: () => model,
      }),
    }),
  }),
)

export default example({
  title: 'Round, square, exported',
  slots: ImageCropperSlots,
  recipe: Recipes.ImageCropper,
  pins: { viewport: 'square', handles: 'corners' },
  state: App.placements.runtime({ Model: App.Model, initial: { picture: Option.none() } }),
  view: ({ style, model, onMessage, h }) => {
    const toCrop = (message: ImageCropper.Message) =>
      onMessage(App.Message.GotCropMessage({ message }))
    return stack(h, [
      ImageCropper.view(
        {
          model: model.crop,
          onMessage: toCrop,
          label: 'Profile picture',
          src: LANDSCAPE,
          controls: { rotate: false },
          style,
        },
        h,
      ),
      row(h, [
        Button.view(
          {
            label: 'Use this crop',
            onClick: toCrop(
              ImageCropper.Message.RequestedExport({ type: 'image/png', quality: 1 }),
            ),
            style: Use,
          },
          h,
        ),
        ...Option.match(model.picture, {
          onNone: () => [],
          onSome: src => [Avatar.view({ src, name: 'The cropped picture', style: Picture }, h)],
        }),
      ]),
    ])
  },
})

In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.

Choose the aspect #

Crop the photo
import { Equal, Option } from 'effect'
import { Bundle } from 'foldkit-bundle'
import { Bundles, ImageCropper, ImageCropperSlots, Recipes, Segmented } from 'foldkit-mixins-ui'
import { example } from '../../gallery/example.js'
import { stack } from '../../snippets/layout.js'
import { LANDSCAPE } from '../../snippets/landscape.js'

const Ratios = Recipes.Segmented.style({ size: 'sm' })
const ratios = [
  { label: 'Free', aspect: Option.none<number>() },
  { label: '1:1', aspect: Option.some(1) },
  { label: '4:3', aspect: Option.some(4 / 3) },
  { label: '16:9', aspect: Option.some(16 / 9) },
]

// The lock is the cropper's: the page's buttons send `ChangedAspect` to the
// placed child, which fits the crop to it at once, about its centre.
const App = Bundle.compose({}).pipe(
  Bundle.withChild('crop', Bundles.ImageCropper, {
    args: { id: 'ratio-crop' },
    onOut: Bundle.ignore,
  }),
)

export default example({
  title: 'Choose the aspect',
  slots: ImageCropperSlots,
  recipe: Recipes.ImageCropper,
  state: App.placements.runtime({ Model: App.Model, initial: {} }),
  view: ({ style, model, onMessage, h }) => {
    const toCrop = (message: ImageCropper.Message) =>
      onMessage(App.Message.GotCropMessage({ message }))
    return stack(h, [
      Segmented.view(
        {
          id: 'crop-ratio',
          label: 'Aspect',
          options: ratios.map(({ label }) => ({ value: label, label })),
          pressed: ratios
            .filter(({ aspect }) => Equal.equals(aspect, model.crop.aspect))
            .map(({ label }) => label),
          onPress: value =>
            toCrop(
              ImageCropper.Message.ChangedAspect({
                aspect: Option.flatMap(
                  Option.fromUndefinedOr(ratios.find(({ label }) => label === value)),
                  ({ aspect }) => aspect,
                ),
              }),
            ),
          style: Ratios,
        },
        h,
      ),
      ImageCropper.view(
        { model: model.crop, onMessage: toCrop, label: 'Crop the photo', src: LANDSCAPE, style },
        h,
      ),
    ])
  },
})

In your application: recipe goes to forSlots, state to Bundle.withChild, and the body of view is the call. example is this site’s harness.

Usage

Each example above is a module of its own, shown whole and run as shown, over the theme module Create a theme writes. The page hands it the style picked above.

The recipe ↗This code ↗