const scene = game.scene.getScene('default');
const canvas = scene.game.canvas;
// set size of the screenshot
const cropWidth = 350;
const cropHeight = 400;
// save the whole canvas as screenshot
const screenshotCanvas = document.createElement('canvas');
screenshotCanvas.width = canvas.width;
screenshotCanvas.height = canvas.height;
const context = screenshotCanvas.getContext('2d');
context.drawImage(canvas, 0, 0);
// crop center cropWidth x cropHeight area
const cropCanvas = document.createElement('canvas');
cropCanvas.width = cropWidth;
cropCanvas.height = cropHeight;
const cropContext = cropCanvas.getContext('2d');
// center cooridante
const sx = Math.round((canvas.width - cropWidth) / 2);
const sy = Math.round((canvas.height - cropHeight) / 2) - 50;
const sWidth = cropWidth;
const sHeight = cropHeight;
cropContext.drawImage(
screenshotCanvas,
sx,
sy,
sWidth,
sHeight,
0,
0,
cropWidth,
cropHeight,
);
// Base64 convert
const base64 = cropCanvas.toDataURL('image/png');
// convert base64 to HTMLImageElement
const image = new Image();
image.src = base64;
// add as Phaser texture after being loaded
image.onload = () => {
const index = localStorage.length + 1;
scene.textures.addImage(`preset_${index}`, image);
console.log(`Screenshot saved to cache as preset_${index}.`);
// information will be stored in local storage
const screenshotInfo = {
index: index,
data: base64,
nameTag: nameTag,
human: human,
wing: wing,
aura: aura,
nickname: nickname,
};
// save in local storage
localStorage.setItem(`preset_${index}`, JSON.stringify(screenshotInfo));
// check saved data
console.log('Saved image info:', screenshotInfo);
};
I use this code to save a screenshot of the phaser canvas in my local storage.
I could view the image by simply inserting the generated url into the browser link box.
But the problem is, I don’t know how long is this url valid.
Whether I clear the local storage or test it on a Incognito tab, the link was still valid.
Doesn’t this mean that the image information is being stored somewhere other than the local storage?
How can this link be expired?