MediaWiki:CardToImage.js: mudanças entre as edições

De Wikincat
Ir para navegação Ir para pesquisar
m (uma edição)
Sem resumo de edição
 
(Uma revisão intermediária pelo mesmo usuário não está sendo mostrada)
Linha 1: Linha 1:
/* jshint esversion: 10 */
/**
/**
* Convert a base64 string in a Blob according to the data and contentType.
* Convert a base64 string in a Blob according to the data and contentType.
Linha 5: Linha 6:
* @param contentType {String} the content type of the file i.e (image/jpeg - image/png - text/plain)
* @param contentType {String} the content type of the file i.e (image/jpeg - image/png - text/plain)
* @param sliceSize {Int} SliceSize to process the byteCharacters
* @param sliceSize {Int} SliceSize to process the byteCharacters
* @see http://stackoverflow.com/questions/16245767/creating-a-blob-from-a-base64-string-in-javascript
* @see https://stackoverflow.com/questions/16245767
* @return Blob
* @return Blob
*/
*/
function cardToImage() {
function cardToImage() {
function b64toBlob(b64Data, contentType = '', sliceSize = 512) {
"use strict";
const byteCharacters = atob(b64Data);
function b64toBlob(b64Data, contentType, sliceSize) {
const byteArrays = [];
contentType = contentType || "";
sliceSize = sliceSize || 512;


let byteCharacters = atob(b64Data);
for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
const slice = byteCharacters.slice(offset, offset + sliceSize);
let byteArrays = [];


for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
const byteNumbers = new Array(slice.length);
let slice = byteCharacters.slice(offset, offset + sliceSize);
for (let i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}


let byteNumbers = new Array(slice.length);
const byteArray = new Uint8Array(byteNumbers);
for (let i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}


byteArrays.push(byteArray);
let byteArray = new Uint8Array(byteNumbers);
}


const blob = new Blob(byteArrays, { type: contentType });
byteArrays.push(byteArray);
}
return blob;
}


// Generate screenshot and download
let blob = new Blob(byteArrays, {type: contentType});
const opt = {
return blob;
}

// Generate screenshot and download
let opt = {
scrollX: -window.scrollX,
scrollX: -window.scrollX,
scrollY: -window.scrollY,
scrollY: -window.scrollY,
};
};
let element = document.getElementById("cardCatalog");
const element = document.getElementById('cardCatalog');
html2canvas(element, opt).then(function(canvas) {
html2canvas(element, opt).then(function (canvas) {
// Generate the base64 representation of the canvas
// Generate the base64 representation of the canvas
let base64image = canvas.toDataURL("image/png");
const base64image = canvas.toDataURL('image/png');


// Split the base64 string in data and contentType
// Split the base64 string in data and contentType
let block = base64image.split(";");
const block = base64image.split(';');
// Get the content type
// Get the content type
let mimeType = block[0].split(":")[1];// In this case "image/png"
const mimeType = block[0].split(':')[1]; // In this case "image/png"
// get the real base64 content of the file
// get the real base64 content of the file
let realData = block[1].split(",")[1];// For example: iVBORw0KGgouqw23....
const realData = block[1].split(',')[1]; // For example: iVBORw0KGgouqw23....


// Convert b64 to blob and store it into a variable (with real base64 as value)
// Convert b64 to blob and store it into a variable (with real base64 as value)
let canvasBlob = b64toBlob(realData, mimeType);
const canvasBlob = b64toBlob(realData, mimeType);


// Generate file download
// Generate file download
window.saveAs(canvasBlob, "Ficha.png");
window.saveAs(canvasBlob, 'Ficha.png');
});
});

}
}

Edição atual tal como às 12h22min de 17 de março de 2024

/* jshint esversion: 10 */
/**
 * Convert a base64 string in a Blob according to the data and contentType.
 *
 * @param b64Data {String} Pure base64 string without contentType
 * @param contentType {String} the content type of the file i.e (image/jpeg - image/png - text/plain)
 * @param sliceSize {Int} SliceSize to process the byteCharacters
 * @see https://stackoverflow.com/questions/16245767
 * @return Blob
 */
function cardToImage() {
  function b64toBlob(b64Data, contentType = '', sliceSize = 512) {
    const byteCharacters = atob(b64Data);
    const byteArrays = [];

    for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
      const slice = byteCharacters.slice(offset, offset + sliceSize);

      const byteNumbers = new Array(slice.length);
      for (let i = 0; i < slice.length; i++) {
        byteNumbers[i] = slice.charCodeAt(i);
      }

      const byteArray = new Uint8Array(byteNumbers);

      byteArrays.push(byteArray);
    }

    const blob = new Blob(byteArrays, { type: contentType });
    return blob;
  }

  // Generate screenshot and download
  const opt = {
    scrollX: -window.scrollX,
    scrollY: -window.scrollY,
  };
  const element = document.getElementById('cardCatalog');
  html2canvas(element, opt).then(function (canvas) {
    // Generate the base64 representation of the canvas
    const base64image = canvas.toDataURL('image/png');

    // Split the base64 string in data and contentType
    const block = base64image.split(';');
    // Get the content type
    const mimeType = block[0].split(':')[1]; // In this case "image/png"
    // get the real base64 content of the file
    const realData = block[1].split(',')[1]; // For example:  iVBORw0KGgouqw23....

    // Convert b64 to blob and store it into a variable (with real base64 as value)
    const canvasBlob = b64toBlob(realData, mimeType);

    // Generate file download
    window.saveAs(canvasBlob, 'Ficha.png');
  });
}