issue-sticker 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -172,18 +172,51 @@ function svgToDataUrl(svg, removeControlCharacter) {
172
172
  }
173
173
  return `data:image/svg+xml;charset=utf-8,${encodeURIComponent(xhtml)}`;
174
174
  }
175
+ async function canvasToBlob(canvas, type = "image/png", quality = 1) {
176
+ try {
177
+ return await new Promise((resolve, reject) => {
178
+ canvas.toBlob((blob) => {
179
+ if (blob) {
180
+ resolve(blob);
181
+ } else {
182
+ reject(new Error("Blob is null"));
183
+ }
184
+ }, type, quality);
185
+ });
186
+ } catch (error) {
187
+ if (SUPPORT_ATOB) {
188
+ return dataUrlToBlob(canvas.toDataURL(type, quality));
189
+ }
190
+ throw error;
191
+ }
192
+ }
193
+ function dataUrlToBlob(dataUrl) {
194
+ var _a2;
195
+ const [header, base64] = dataUrl.split(",");
196
+ const type = ((_a2 = header.match(/data:(.+);/)) == null ? void 0 : _a2[1]) ?? void 0;
197
+ const decoded = window.atob(base64);
198
+ const length = decoded.length;
199
+ const buffer = new Uint8Array(length);
200
+ for (let i = 0; i < length; i += 1) {
201
+ buffer[i] = decoded.charCodeAt(i);
202
+ }
203
+ return new Blob([buffer], { type });
204
+ }
175
205
  function readBlob(blob, type) {
176
206
  return new Promise((resolve, reject) => {
177
207
  const reader = new FileReader();
178
208
  reader.onload = () => resolve(reader.result);
179
209
  reader.onerror = () => reject(reader.error);
180
210
  reader.onabort = () => reject(new Error(`Failed read blob to ${type}`));
181
- {
211
+ if (type === "dataUrl") {
182
212
  reader.readAsDataURL(blob);
213
+ } else if (type === "arrayBuffer") {
214
+ reader.readAsArrayBuffer(blob);
183
215
  }
184
216
  });
185
217
  }
186
218
  const blobToDataUrl = (blob) => readBlob(blob, "dataUrl");
219
+ const blobToArrayBuffer = (blob) => readBlob(blob, "arrayBuffer");
187
220
  function createImage(url, ownerDocument) {
188
221
  const img = getDocument(ownerDocument).createElement("img");
189
222
  img.decoding = "sync";
@@ -1520,6 +1553,26 @@ async function domToCanvas(node, options) {
1520
1553
  const image = createImage(dataUrl, svg.ownerDocument);
1521
1554
  return await imageToCanvas(image, context);
1522
1555
  }
1556
+ async function domToBlob(node, options) {
1557
+ const context = await orCreateContext(node, options);
1558
+ const { log, type, quality, dpi } = context;
1559
+ const canvas = await domToCanvas(context);
1560
+ log.time("canvas to blob");
1561
+ const blob = await canvasToBlob(canvas, type, quality);
1562
+ if (["image/png", "image/jpeg"].includes(type) && dpi) {
1563
+ const arrayBuffer = await blobToArrayBuffer(blob.slice(0, 33));
1564
+ let uint8Array = new Uint8Array(arrayBuffer);
1565
+ if (type === "image/png") {
1566
+ uint8Array = changePngDpi(uint8Array, dpi);
1567
+ } else if (type === "image/jpeg") {
1568
+ uint8Array = changeJpegDpi(uint8Array, dpi);
1569
+ }
1570
+ log.timeEnd("canvas to blob");
1571
+ return new Blob([uint8Array, blob.slice(33)], { type });
1572
+ }
1573
+ log.timeEnd("canvas to blob");
1574
+ return blob;
1575
+ }
1523
1576
  async function domToDataUrl(node, options) {
1524
1577
  const context = await orCreateContext(node, options);
1525
1578
  const { log, quality, type, dpi } = context;
@@ -1555,6 +1608,44 @@ async function domToDataUrl(node, options) {
1555
1608
  log.timeEnd("canvas to data url");
1556
1609
  return dataUrl;
1557
1610
  }
1611
+ async function domToSvg(node, options) {
1612
+ const context = await orCreateContext(node, options);
1613
+ const { width, height, ownerDocument } = context;
1614
+ const dataUrl = await domToDataUrl(context);
1615
+ const svg = createSvg(width, height, ownerDocument);
1616
+ const svgImage = svg.ownerDocument.createElementNS(svg.namespaceURI, "image");
1617
+ svgImage.setAttributeNS(null, "href", dataUrl);
1618
+ svgImage.setAttributeNS(null, "height", "100%");
1619
+ svgImage.setAttributeNS(null, "width", "100%");
1620
+ svg.appendChild(svgImage);
1621
+ return svgToDataUrl(svg, context.isEnable("removeControlCharacter"));
1622
+ }
1623
+ async function domToImage(node, options) {
1624
+ const context = await orCreateContext(node, options);
1625
+ const { ownerDocument, width, height, scale, type } = context;
1626
+ const url = type === "image/svg+xml" ? await domToSvg(context) : await domToDataUrl(context);
1627
+ const image = createImage(url, ownerDocument);
1628
+ image.width = Math.floor(width * scale);
1629
+ image.height = Math.floor(height * scale);
1630
+ image.style.width = `${width}px`;
1631
+ image.style.height = `${height}px`;
1632
+ return image;
1633
+ }
1634
+ async function domToJpeg(node, options) {
1635
+ return domToDataUrl(
1636
+ await orCreateContext(node, { ...options, type: "image/jpeg" })
1637
+ );
1638
+ }
1639
+ async function domToPixel(node, options) {
1640
+ const context = await orCreateContext(node, options);
1641
+ const canvas = await domToCanvas(context);
1642
+ return canvas.getContext("2d").getImageData(0, 0, canvas.width, canvas.height).data;
1643
+ }
1644
+ async function domToPng(node, options) {
1645
+ return domToDataUrl(
1646
+ await orCreateContext(node, { ...options, type: "image/png" })
1647
+ );
1648
+ }
1558
1649
  async function domToWebp(node, options) {
1559
1650
  return domToDataUrl(
1560
1651
  await orCreateContext(node, { ...options, type: "image/webp" })
@@ -1563,9 +1654,15 @@ async function domToWebp(node, options) {
1563
1654
  export {
1564
1655
  createContext,
1565
1656
  destroyContext,
1657
+ domToBlob,
1566
1658
  domToCanvas,
1567
1659
  domToDataUrl,
1568
1660
  domToForeignObjectSvg,
1661
+ domToImage,
1662
+ domToJpeg,
1663
+ domToPixel,
1664
+ domToPng,
1665
+ domToSvg,
1569
1666
  domToWebp,
1570
1667
  loadMedia,
1571
1668
  waitUntilLoad