Painterro requires a saveHandler to persist images. You can save via binary multipart/form-data (most efficient) or base64 JSON.
Binary Saving (Recommended)
Use image.asBlob() to send data via FormData.
var ptro = Painterro({
saveHandler: function (image, done) {
var formData = new FormData();
formData.append('image', image.asBlob());
var xhr = new XMLHttpRequest();
xhr.open('POST', 'http://127.0.0.1:5000/save-as-binary/', true);
xhr.onload = xhr.onerror = function () {
done(true); // true hides painterro, false keeps it open
};
xhr.send(formData);
}
});
ptro.show();
Base64 Saving
Use image.asDataURL() to send a base64 string via JSON.
var ptro = Painterro({
saveHandler: function (image, done) {
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://127.0.0.1:5000/save-as-base64/");
xhr.setRequestHeader("Content-Type", "application/json");
xhr.send(JSON.stringify({
image: image.asDataURL()
}));
xhr.onload = function (e) {
done(true);
}
}
});
ptro.show();
Smart Format Selection
To optimize file size, you can check for an alpha channel and choose between PNG and JPEG:
var ptro = Painterro({
saveHandler: function (image, done) {
const type = image.hasAlphaChannel() ? 'image/png' : 'image/jpeg';
const blob = image.asBlob(type);
// upload blob...
}
});
var ptro = Painterro({
saveHandler: function (image, done) {
var formData = new FormData();
formData.append('image', image.asBlob());
var xhr = new XMLHttpRequest();
xhr.open('POST', 'http://127.0.0.1:5000/save-as-binary/', true);
xhr.onload = xhr.onerror = function () {
done(true);
};
xhr.send(formData);
}
});
ptro.show();