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 itFree crop #
Crop the photo%22%2F%3E%3Ccircle%20cx%3D%22610%22%20cy%3D%22150%22%20r%3D%2270%22%20fill%3D%22hsl(45%2095%25%2065%25)%22%2F%3E%3Cpath%20d%3D%22M0%20460%20L230%20250%20L420%20430%20L560%20320%20L800%20500%20V600%20H0%20Z%22%20fill%3D%22hsl(150%2030%25%2045%25)%22%2F%3E%3C%2Fsvg%3E)
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%22%2F%3E%3Ccircle%20cx%3D%22610%22%20cy%3D%22150%22%20r%3D%2270%22%20fill%3D%22hsl(45%2095%25%2065%25)%22%2F%3E%3Cpath%20d%3D%22M0%20460%20L230%20250%20L420%20430%20L560%20320%20L800%20500%20V600%20H0%20Z%22%20fill%3D%22hsl(150%2030%25%2045%25)%22%2F%3E%3C%2Fsvg%3E)
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%22%2F%3E%3Ccircle%20cx%3D%22610%22%20cy%3D%22150%22%20r%3D%2270%22%20fill%3D%22hsl(45%2095%25%2065%25)%22%2F%3E%3Cpath%20d%3D%22M0%20460%20L230%20250%20L420%20430%20L560%20320%20L800%20500%20V600%20H0%20Z%22%20fill%3D%22hsl(150%2030%25%2045%25)%22%2F%3E%3C%2Fsvg%3E)
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.