A Freeform-style, SwiftData-backed visual canvas for SwiftUI — pan, zoom, drag, multi-select, rubber-band select, connect things with arrows, drop free-form text notes, and export the whole thing as a shareable PNG.
Originally built for MyDeck to diagram Magic: The Gathering combo lines (cards connected by labeled arrows), then pulled out into its own package because the canvas engine itself has nothing to do with cards — it's a general-purpose "place things on a board and connect them" interaction layer.
- Pan & zoom on a large fixed-extent canvas, with a host-driven zoom toolbar (+/−/reset/fit-to-content).
- Drag, multi-select, and rubber-band select across a mix of your own content and free-form text boxes.
- Group move — drag one selected element and the rest of the selection moves with it, rigidly.
- Duplicate, reorder (front/back), and delete for both single and multi-element selections.
- Arrows connecting two elements (auto-follow when either end moves), or free-floating — with color, line weight, and an optional relationship icon badge, all editable from a floating selection bar.
- Inline text editing for text boxes, positioned as a real, undistorted editor over the zoomed/panned canvas (a plain
TextEditorunder a non-1.0.scaleEffecthas broken cursor placement — this works around that SwiftUI limitation). - PNG export & sharing of the board's content (transparent background, cropped to content), wired into
ShareLinkon iOS and a nativeNSSharingServicePickeron macOS (SwiftUI'sShareLinkcan't get Photos into the destination list on macOS — seeStoryboardImageExporter's doc comment for why). - SwiftData-native.
Storyboard,StoryboardElement, andStoryboardArroware@Modeltypes your app adds directly to its own schema — no separate store, no sync layer.
The package doesn't know what a "card" (or a photo, a recipe step, or whatever else you want to place on the board) is. A StoryboardElement of kind .payload carries only an opaque payloadID: String? that your app defines the meaning of. You supply:
- How a payload renders — a
payloadContentview builder, given the element, current scale, selection state, and tap/group-move callbacks. - How a payload gets added — the contents of the toolbar's "Add" menu, as an
addPayloadMenuContentview builder that receives the canvas point to insert at. Your menu buttons create theStoryboardElementand set itspayloadIDthemselves.
Everything else — arrows, text boxes, selection, zoom, export — needs no input from you.
- iOS 17+ / macOS 14+
- Swift 6.4+
- SwiftData
Add it to your Package.swift or via Xcode's File → Add Package Dependencies…:
.package(url: "https://github.com/dyerlab/StoryBoards", from: "1.0.0")Add the three model types to your app's own Schema:
import StoryBoards
let schema = Schema([
// ...your own models...
Storyboard.self,
StoryboardElement.self,
StoryboardArrow.self,
])Storyboard— one canvas.title,summary,snapToGrid, and theelements/arrowsit owns (both cascade-deleted with it).ownerIDis an opaque string for whatever your app considers this board's owner (a deck, a project, a recipe) — SwiftData@Modeltypes can't be generic, so there's no typed relationship here. Deleting the owner does not cascade-delete its storyboards automatically; fetch and delete them byownerIDas part of your own delete path.StoryboardElement— one placed item,.payloador.text, with a center point (x/y), explicitwidth/height, and azIndex. A.payloadelement carries yourpayloadID; a.textelement carriestext,textColor, andtextSize. The package applies no aspect-ratio logic — if your payload wants a fixed aspect ratio (a playing card, say), setheightaccordingly when you create it.StoryboardArrow— a straight line between two elements (or two free points), witharrowColor,arrowWeight, and an optionalarrowVerbbadge. Fully generic; no changes needed to use it as-is.StoryboardConfiguration— sizing/behavior knobs (canvas extent, grid spacing, zoom bounds, minimum text size, duplicate offset, corner radius, selection-bar button size). Apply your own via.storyboardConfiguration(_:)anywhere above aStoryboardView; every field has a sensible default if you don't.
import SwiftUI
import SwiftData
import StoryBoards
struct RecipeBoardView: View {
@Bindable var storyboard: Storyboard
@Environment(\.modelContext) private var context
// Your own lookup from a payloadID back to your own model.
var ingredients: [Ingredient]
var body: some View {
StoryboardView(
storyboard: storyboard,
isAddPayloadMenuEmpty: ingredients.isEmpty
) { element, scale, isSelected, onTap, onGroupMove in
// Render a `.payload` element — implement drag yourself here,
// the same way StoryboardTextElementView does, if you want
// draggable payloads (see the package's source for the pattern).
if let ingredient = ingredients.first(where: { $0.id.uuidString == element.payloadID }) {
IngredientTile(ingredient: ingredient)
}
} addPayloadMenuContent: { insertionPoint in
ForEach(ingredients) { ingredient in
Button(ingredient.name) {
let element = StoryboardElement(
storyboard: storyboard,
kind: .payload,
x: insertionPoint.x,
y: insertionPoint.y,
zIndex: storyboard.nextZIndex,
width: 140,
height: 140
)
element.payloadID = ingredient.id.uuidString
context.insert(element)
try? context.save()
}
}
}
}
}- A grid/list view of a host's storyboards (e.g. "all boards for this deck") — that's a thin, app-specific navigation shell with little to reuse; build it against
Storyboard/StoryboardConfigurationdirectly. - Any persistence beyond SwiftData itself — no
SchemaMigrationPlan, no CloudKit configuration. That's your app'sModelContainersetup. - Payload-specific selection-bar controls —
StoryboardSelectionBarshows no style controls for.payloadelements (only reorder/duplicate/delete), matching how card elements behaved in the original app. A host wanting payload-specific controls needs its own selection bar for now.
Copyright © Dyerlab LLC. All rights reserved.