Skip to content

Commit c78dae4

Browse files
committed
format and quality support
1 parent 61f1e24 commit c78dae4

2 files changed

Lines changed: 27 additions & 12 deletions

File tree

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Table of contents
3737
* [Base64 saving](#base64-saving)
3838
* [Binary saving](#binary-saving)
3939
* [Saving to WYSIWYG](#saving-to-wysiwyg)
40+
* [Format and quality](#format-and-quality)
4041
* [Development](#development)
4142
* [Building painterro](#building-painterro)
4243
* [Dev-server](#dev-server)
@@ -194,7 +195,7 @@ var ptro = Painterro({
194195
xhr.open("POST", "http://127.0.0.1:5000/save-as-base64/");
195196
xhr.setRequestHeader("Content-Type", "application/json");
196197
xhr.send(JSON.stringify({
197-
image: image.asDataURL('image/png')
198+
image: image.asDataURL()
198199
}));
199200
xhr.onload = function (e) {
200201
// after saving is done, call done callback
@@ -266,6 +267,16 @@ You can just insert image as data urlto any WYSIWYG editor, e.g. TinyMCE. Image
266267
})
267268
```
268269

270+
Format and quality
271+
------------------
272+
273+
When you call `image.asDataURL()` or `image.asBlob()`, you can also specify image format, e.g.
274+
`image.asDataURL('image/jpeg')`. Default format is `'image/png'`.
275+
If type is `image/jpeg` or `image/webp`, you can also define image quality from `0.0` to `1.0`, default is `0.92`,
276+
example: `image.asDataURL('image/jpeg', 0.5)`
277+
278+
279+
269280
Development
270281
===========
271282

js/main.js

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -486,19 +486,13 @@ class PainterroProc {
486486
/**
487487
* Returns image as base64 data url
488488
* @param {string} type - type of data url, default image/png
489+
* @param {string} quality - number from 0 to 1, works for `image/jpeg` or `image/webp`
489490
*/
490-
asDataURL: (type) => {
491-
if (type === undefined) {
492-
return this.canvas.toDataURL();
493-
} else {
494-
return this.canvas.toDataURL(type);
495-
}
491+
asDataURL: (type, quality) => {
492+
return this.getAsUri(type, quality);
496493
},
497-
asBlob: (type) => {
498-
if (type === undefined) {
499-
type = 'image/png';
500-
}
501-
const uri = this.canvas.toDataURL(type);
494+
asBlob: (type, quality) => {
495+
const uri = this.getAsUri(type, quality);
502496
const byteString = atob(uri.split(',')[1]);
503497
const ab = new ArrayBuffer(byteString.length);
504498
let ia = new Uint8Array(ab);
@@ -514,6 +508,16 @@ class PainterroProc {
514508
this.hide();
515509
}
516510

511+
getAsUri (type, quality) {
512+
if (type === undefined) {
513+
type = 'image/png';
514+
}
515+
if (quality === undefined) {
516+
quality = 0.92;
517+
}
518+
return this.canvas.toDataURL(type, quality);
519+
}
520+
517521
save () {
518522
if (this.saving) {
519523
return

0 commit comments

Comments
 (0)