# Integrating Aspectfern

Include `component/aspectfern.css` and the classic script `component/aspectfern.js`. Mount after the document exists, using defer or DOMContentLoaded. Supply an empty element for each independent field. There are no shared crop/image settings.

```html
<link rel="stylesheet" href="component/aspectfern.css">
<div id="image-field"></div>
<script defer src="component/aspectfern.js"></script>
<script defer src="your-form.js"></script>
```

```js
const field = document.getElementById('image-field');
let attachment = null;
const cropper = new Aspectfern.Cropper(field, {
  onResult({blob, width, height, mime, crop}) {
    // Real newly encoded Blob. Do not trust these values on a server.
    const ext = {'image/png':'png', 'image/jpeg':'jpg', 'image/webp':'webp'}[mime];
    attachment = new File([blob], 'cover.' + ext, {type:mime});
  }
});
field.addEventListener('aspectfern:change', () => { attachment = null; });
// In your explicit form-submit handler, after verifying attachment exists:
const data = new FormData();
// data.set('cover', attachment, attachment.name);
// Send only to your own authorized, validated backend. This package sends nothing.
```

`samples/form.js` is a full working local example: two independently mounted fields, actual attachments, invalidation and FormData entries. Its submit handler prevents network submission. Keep image contents, filenames and project JSON out of analytics. Host-side previews need their object URLs revoked when replaced.

## API

- `new Aspectfern.Cropper(emptyElement, {onResult})`: build an accessible labelled picker/control set. Duplicate mount or nonempty content is refused. Callback receives `{blob,width,height,mime,crop}` only after the normal Create image action finishes. A bubbling `aspectfern:result` event carries the same detail. Do not attach analytics that serialize it.
- `await instance.loadFile(file)`: validate/decode one real File. Supported MIME/resource limits apply. Rejects without replacing the previous successful image on error. When multiple valid loads race, the last started load wins and obsolete bitmaps close.
- `instance.getProject()`: deep copy of the current full version1 project, including normalized PNG data. Throws before loading or during a pending load. Contains sensitive user image content; do not publish it automatically.
- `await instance.setProject(project)`: strict validation and local decode, atomic commit only after actual image dimensions agree. It does not evaluate HTML/scripts or fetch image URLs. For your own parser, also enforce the32MiB project byte limit as the supplied file picker does.
- `await instance.exportBlob()`: return a newly encoded result without automatic download/event/callback. The crop must remain unchanged during encoding or this rejects. Build your own explicit form action around the returned Blob.
- `await instance.createResult()`: prepare the visible result/download and emit result event/callback. The UI Create image button uses this method. Callback errors belong to host integration; keep callbacks small and handle your own backend errors.
- `instance.destroy()`: abort input listeners, disconnect observer, close current bitmap, revoke current prepared URL and clear only that mounted root. Host callback previews are the host's cleanup responsibility. Idempotent; API calls afterward reject. Remount into the cleared element if needed.

`aspectfern:change` bubbles after a successful source/configuration edit and gives only `{crop,outputWidth}`. Listen to invalidate any external attachment. There is no implicit upload/delete/storage or network retry API. Use your backend's normal authorization, MIME/content inspection, size limits, CSRF defenses, virus scanning and safe filenames. Client validation is user feedback, never a server security boundary. Sending this image is a separate action controlled by your application.

Classic script works offline, including on file:// for user-picked files. Same-origin sample-button loading needs HTTP. On a strict CSP host, allow this script/style and `img-src blob: data:` for prepared preview; canvas work itself needs no remote origin. Source colours follow browser canvas conversion. Transparent JPEG output is white, not transparent; choose PNG for alpha.
