MediaWiki:CardToImage.js: mudanças entre as edições
Ir para navegação
Ir para pesquisar
Sem resumo de edição |
Sem resumo de edição |
||
Linha 1: | Linha 1: | ||
/* jshint esversion: |
/* 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 10: | Linha 10: | ||
*/ |
*/ |
||
function cardToImage() { |
function cardToImage() { |
||
⚫ | |||
"use strict"; |
|||
const byteCharacters = atob(b64Data); |
|||
⚫ | |||
⚫ | |||
contentType = contentType || ""; |
|||
sliceSize = sliceSize || 512; |
|||
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++) { |
|||
⚫ | |||
⚫ | |||
const byteArray = new Uint8Array(byteNumbers); |
|||
for (let i = 0; i < slice.length; i++) { |
|||
⚫ | |||
⚫ | |||
⚫ | |||
let byteArray = new Uint8Array(byteNumbers); |
|||
} |
|||
⚫ | |||
⚫ | |||
return blob; |
|||
} |
|||
⚫ | |||
⚫ | |||
⚫ | |||
return blob; |
|||
} |
|||
⚫ | |||
⚫ | |||
scrollX: -window.scrollX, |
scrollX: -window.scrollX, |
||
scrollY: -window.scrollY, |
scrollY: -window.scrollY, |
||
}; |
}; |
||
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 |
||
const base64image = canvas.toDataURL('image/png'); |
|||
// Split the base64 string in data and contentType |
// Split the base64 string in data and contentType |
||
const block = base64image.split(';'); |
|||
// Get the content type |
// Get the content type |
||
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 |
||
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) |
||
const canvasBlob = b64toBlob(realData, mimeType); |
|||
// Generate file download |
// Generate file download |
||
window.saveAs(canvasBlob, |
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');
});
}