Reputation: 205
I'm have a function to transform text in a base64 image on my Cordova App. It's working fine but in a few devices there's an Unexpected token error on the script.
Here's the function:
function socialShare(message, font) {
var y = 12;
var x = 18;
var canvas = document.getElementById("receipt");
var context = canvas.getContext("2d");
// calcula a largura da string mais larga
context.font = font;
var maxStrWidth = message.map(e => {
return context.measureText(e).width;
}).sort((a, b) => {
return b - a;
});
// configura a largura do canvas dinamicamente
canvas.width = maxStrWidth[0] + 9;
canvas.height = x * message.length;
// seta a cor do background do canvas
context.fillStyle = "#ffffe6";
context.fillRect(0, 0, canvas.width, canvas.height);
// escreve o texto
context.font = font;
context.fillStyle = "#000";
message.forEach(e => {
context.fillText(e, 3, y);
y += x;
});
// gera a string base64
let base64 = canvas.toDataURL("image/jpeg", 1);
// chamada do plugin social share
window.plugins.socialsharing.share(
null,
'Comprovante de Aposta',
base64,
null
);
}
The error is thrown on the var maxStrWidth line. Do you see anything wrong with it?
Upvotes: 0
Views: 1947
Reputation: 995
Not all devices support the ES6 arrow functions, this is especially true for older android versions that use an older chrome webview version.
If you intend on supporting older devices it may be better to stick with standard function declarations.
Further reading here:
https://strongloop.com/strongblog/an-introduction-to-javascript-es6-arrow-functions/
Upvotes: 1