on c19b2a8 help sign in
bridge/src/lib/SigilDownloader.js
45 lines 1.3 kB history raw
1export const initCanvas = (canvas, size) => {
2 const { x, y } = size;
3 const ctx = canvas.getContext('2d');
5 let ratio = window.devicePixelRatio || 1;
7 canvas.width = x * ratio;
8 canvas.height = y * ratio;
9 canvas.style.width = x + 'px';
10 canvas.style.height = y + 'px';
12 ctx.scale(ratio, ratio);
14 return canvas;
15};
17export const loadImg = src =>
18 new Promise((resolve, reject) => {
19 const img = new Image();
20 img.onload = () => resolve(img);
21 img.onerror = () => reject(`Error loading image. src: ${src}`);
22 img.src = src;
23 });
25export const dataURItoBlob = dataURI => {
26 // convert base64 to raw binary data held in a string
27 // doesn't handle URLEncoded DataURIs
28 const byteString = atob(dataURI.split(',')[1]);
29 // separate out the mime component
30 const mimeString = dataURI
31 .split(',')[0]
32 .split(':')[1]
33 .split(';')[0];
34 // write the bytes of the string to an ArrayBuffer
35 const ab = new ArrayBuffer(byteString.length);
36 // create a view into the buffer
37 let ia = new Uint8Array(ab);
38 // set the bytes of the buffer to the correct values
39 for (var i = 0; i < byteString.length; i++) {
40 ia[i] = byteString.charCodeAt(i);
41 }
42 // write the ArrayBuffer to a blob, and you're done
43 const blob = new Blob([ab], { type: mimeString });
44 return blob;
45};