on master help sign in
bridge/src/lib/SigilDownloader.js
50 lines 1.4 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 cleanupCanvas = canvas => {
18 canvas.width = 0;
19 canvas.height = 0;
20};
22export const loadImg = src =>
23 new Promise((resolve, reject) => {
24 const img = new Image();
25 img.onload = () => resolve(img);
26 img.onerror = () => reject(`Error loading image. src: ${src}`);
27 img.src = src;
28 });
30export const dataURItoBlob = dataURI => {
31 // convert base64 to raw binary data held in a string
32 // doesn't handle URLEncoded DataURIs
33 const byteString = atob(dataURI.split(',')[1]);
34 // separate out the mime component
35 const mimeString = dataURI
36 .split(',')[0]
37 .split(':')[1]
38 .split(';')[0];
39 // write the bytes of the string to an ArrayBuffer
40 const ab = new ArrayBuffer(byteString.length);
41 // create a view into the buffer
42 let ia = new Uint8Array(ab);
43 // set the bytes of the buffer to the correct values
44 for (var i = 0; i < byteString.length; i++) {
45 ia[i] = byteString.charCodeAt(i);
46 }
47 // write the ArrayBuffer to a blob, and you're done
48 const blob = new Blob([ab], { type: mimeString });
49 return blob;
50};