MediaWiki:CardToImage.js
Ir para navegação
Ir para pesquisar
Nota: Após publicar, você pode ter que limpar o "cache" do seu navegador para ver as alterações.
- Firefox / Safari: Pressione Shift enquanto clica Recarregar, ou pressione Ctrl-F5 ou Ctrl-R (⌘-R no Mac)
- Google Chrome: Pressione Ctrl-Shift-R (⌘-Shift-R no Mac)
- Internet Explorer/Edge: PressioneCtrl enquanto clica Recarregar, ou Pressione Ctrl-F5
- Opera: Pressione Ctrl-F5.
/* 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');
});
}