onSaveRequest

API reference for the "onSaveRequest" method.

Registers a callback that runs when the user persists the changes to their image.

The onImageUpdate callback runs immediately before the onSaveRequest callback.

Usage

const { imageHelpers } = window.canva;
const canva = window.canva.init();
canva.onReady(async (opts) => {
// Convert the CanvaElement into a CanvaImageBlob
const image = await imageHelpers.fromElement(opts.element);
// Convert the CanvaImageBlob into an HTMLCanvasElement
const canvas = await imageHelpers.toCanvas(image);
// Render the HTMLCanvasElement
document.body.appendChild(canvas);
});
canva.onSaveRequest(async () => {
const canvas = document.querySelector("canvas");
return await imageHelpers.fromCanvas("image/jpeg", canvas);
});

Parameters

Name

Type

Required

Description

handler

SaveRequestHandler

Yes

The callback that runs when Canva emits a save request.

Returns

void

Types

SaveRequestHandler

The callback that runs when Canva emits a save request. If the callback returns a CanvaImageBlob, Canva persists the changes to the user's image. If the callback returns a CanvaElement, Canva calls the updateLayout method before persisting the changes to the user's image. If the method returns undefined, Canva doesn't persist any changes to the image.

Signature

() => CanvaImageBlob | Promise<CanvaImageBlob> | CanvaElement | Promise<CanvaElement> | undefined | Promise<undefined>

Returns