/* BitAddy sharing helpers, used by app.js (your new link, My links) and page.js (a page or short link): baShare.can true when this browser has a share menu (phones, some computers) baShare.link(url, title, text) opens the share menu for a link baShare.qrBlob(data, l1, l2) draws a QR picture (PNG) with two caption lines under it -> Promise of the picture baShare.saveQr(data, l1, l2, n) saves that picture as a file named n Everything happens in the browser: nothing is sent to BitAddy. Needs qrcode.js (loaded before this file). */ (function () { "use strict"; var W = 1000, PAD = 70, QR = W - 2 * PAD; function fit(ctx, text, max, size, weight) { /* the biggest font (down to 22px) at which the text fits on one line; longer text is shortened in the middle */ var t = String(text || ""); for (var s = size; s >= 22; s -= 2) { ctx.font = weight + " " + s + "px system-ui, -apple-system, 'Segoe UI', Roboto, Arial, sans-serif"; if (ctx.measureText(t).width <= max) return t; } while (t.length > 12 && ctx.measureText(t).width > max) t = t.slice(0, Math.ceil(t.length / 2) - 2) + "…" + t.slice(Math.floor(t.length / 2) + 2); return t; } function qrCanvas(data, line1, line2) { var q = qrcode(0, "M"); q.addData(String(data)); q.make(); var n = q.getModuleCount(), cell = Math.floor(QR / (n + 4)), size = cell * (n + 4), x0 = Math.round((W - size) / 2) + 2 * cell, y0 = PAD + 2 * cell; var H = PAD + size + (line1 ? 96 : 0) + (line2 ? 64 : 0) + 110; var c = document.createElement("canvas"); c.width = W; c.height = H; var ctx = c.getContext("2d"); ctx.fillStyle = "#ffffff"; ctx.fillRect(0, 0, W, H); ctx.fillStyle = "#000000"; for (var r = 0; r < n; r++) for (var k = 0; k < n; k++) if (q.isDark(r, k)) ctx.fillRect(x0 + k * cell, y0 + r * cell, cell, cell); ctx.textAlign = "center"; ctx.textBaseline = "alphabetic"; var y = PAD + size + 70; if (line1) { ctx.fillStyle = "#14102b"; ctx.fillText(fit(ctx, line1, W - 2 * PAD, 58, "800"), W / 2, y); y += 66; } if (line2) { ctx.fillStyle = "#3d3a52"; ctx.fillText(fit(ctx, line2, W - 2 * PAD, 38, "600"), W / 2, y); y += 60; } ctx.fillStyle = "#6b6880"; ctx.fillText(fit(ctx, "Check the ending in your wallet before you send", W - 2 * PAD, 30, "500"), W / 2, H - 46); return c; } function qrBlob(data, line1, line2) { return new Promise(function (res, rej) { try { qrCanvas(data, line1, line2).toBlob(function (b) { b ? res(b) : rej(new Error("The picture couldn't be made.")); }, "image/png"); } catch (e) { rej(e); } }); } function saveQr(data, line1, line2, name) { return qrBlob(data, line1, line2).then(function (b) { var a = document.createElement("a"), u = URL.createObjectURL(b); a.href = u; a.download = String(name || "bitaddy-qr").replace(/[^A-Za-z0-9._-]+/g, "-").slice(0, 60) + ".png"; document.body.appendChild(a); a.click(); a.remove(); setTimeout(function () { URL.revokeObjectURL(u); }, 4000); return b; }); } function link(url, title, text) { var d = { url: url }; if (title) d.title = title; if (text) d.text = text; return navigator.share(d); } window.baShare = { can: typeof navigator.share === "function", link: link, qrBlob: qrBlob, qrCanvas: qrCanvas, saveQr: saveQr }; })();