Skip to content

Commit 61f1e24

Browse files
committed
asBlob()
1 parent 20a9340 commit 61f1e24

3 files changed

Lines changed: 18 additions & 23 deletions

File tree

README.md

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -226,20 +226,10 @@ You can also post data with binary `multipart/form-data` request which is more e
226226
The same image took `301949` bytes with `multipart/form-data`.
227227

228228
```js
229-
function dataURItoBlob(dataURI) {
230-
var byteString = atob(dataURI.split(',')[1]);
231-
var ab = new ArrayBuffer(byteString.length);
232-
var ia = new Uint8Array(ab);
233-
for (var i = 0; i < byteString.length; i++) {
234-
ia[i] = byteString.charCodeAt(i);
235-
}
236-
return new Blob([ab], {type: 'image/png'});
237-
}
238-
239229
var ptro = Painterro({
240230
saveHandler: function (image, done) {
241231
var formData = new FormData()
242-
formData.append('image', dataURItoBlob(image.asDataURL()))
232+
formData.append('image', image.asBlob())
243233
var xhr = new XMLHttpRequest();
244234
xhr.open('POST', 'http://127.0.0.1:5000/save-as-binary/', true);
245235
xhr.onload = xhr.onerror = function () {
@@ -318,3 +308,5 @@ ToDo list
318308
- Add recent image sizes in resize tool
319309
- Blur region
320310
- Ability to save loacaly
311+
- Save settings in loaclstorage
312+
- line arrows

example/templates/paste_as_bin.html

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,10 @@
66
{% include 'common.html' %}
77
{% include 'images_list.html' %}
88
<script>
9-
function dataURItoBlob(dataURI) {
10-
var byteString = atob(dataURI.split(',')[1]);
11-
var ab = new ArrayBuffer(byteString.length);
12-
var ia = new Uint8Array(ab);
13-
for (var i = 0; i < byteString.length; i++) {
14-
ia[i] = byteString.charCodeAt(i);
15-
}
16-
return new Blob([ab], {type: 'image/png'});
17-
}
18-
199
var ptro = Painterro({
2010
saveHandler: function (image, done) {
2111
var formData = new FormData()
22-
formData.append('image', dataURItoBlob(image.asDataURL()))
12+
formData.append('image', image.asBlob());
2313

2414
var xhr = new XMLHttpRequest();
2515
xhr.open('POST', 'http://127.0.0.1:5000/save-as-binary/', true);

js/main.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,8 +493,21 @@ class PainterroProc {
493493
} else {
494494
return this.canvas.toDataURL(type);
495495
}
496+
},
497+
asBlob: (type) => {
498+
if (type === undefined) {
499+
type = 'image/png';
500+
}
501+
const uri = this.canvas.toDataURL(type);
502+
const byteString = atob(uri.split(',')[1]);
503+
const ab = new ArrayBuffer(byteString.length);
504+
let ia = new Uint8Array(ab);
505+
for (let i = 0; i < byteString.length; i++) {
506+
ia[i] = byteString.charCodeAt(i);
507+
}
508+
return new Blob([ab], {type: type});
496509
}
497-
}
510+
};
498511

499512
this.initEventHandlers();
500513
this.clear();

0 commit comments

Comments
 (0)