Created
November 8, 2023 13:22
-
-
Save devcem/80f2ed05a29c0d1d3321c8722a2e3935 to your computer and use it in GitHub Desktop.
Playcanvas get thumbnail of any object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function defineThumbnailGenerator(canvas){ | |
if(!canvas){ | |
return false; | |
} | |
if(canvas.hasThumbnailGenerator){ | |
return false; | |
} | |
canvas.hasThumbnailGenerator = true; | |
canvas.addEventListener('contextmenu', function (e) { | |
e.preventDefault(); // prevent the default context menu from showing | |
// Create a temporary canvas to draw the flipped image | |
var tempCanvas = document.createElement('canvas'); | |
var tempCtx = tempCanvas.getContext('2d'); | |
tempCanvas.width = canvas.width; | |
tempCanvas.height = canvas.height; | |
// Translate and scale to flip the context | |
tempCtx.translate(0, canvas.height); | |
tempCtx.scale(1, -1); // Flip horizontally on Y axis | |
// Draw the original canvas onto the temporary canvas | |
tempCtx.drawImage(canvas, 0, 0); | |
// Convert the temporary canvas to a data URL | |
var flippedImageURL = tempCanvas.toDataURL(); | |
// Create an anchor for downloading | |
var downloadLink = document.createElement('a'); | |
downloadLink.href = flippedImageURL; | |
downloadLink.download = 'flipped_canvas_image.png'; | |
downloadLink.click(); | |
// Clean up the temporary canvas | |
tempCanvas = null; | |
}, false); | |
}; | |
setInterval(function(){ | |
var canvas = document.querySelector('.pcui-asset-preview-container-large > .pcui-asset-preview-canvas-flip'); | |
if(canvas){ | |
defineThumbnailGenerator(canvas); | |
} | |
}, 1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment