@applitools/utils
Version:
38 lines (37 loc) • 1.77 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.enableCanvasPreserveDrawingBuffer = void 0;
function enableCanvasPreserveDrawingBuffer() {
function enablePreserveDrawingBuffer() {
if (HTMLCanvasElement.prototype.__isPatchedForPreserveDrawingBuffer) {
// eslint-disable-next-line no-console
console.log('Canvas preserveDrawingBuffer patch has already been applied.');
return;
}
// eslint-disable-next-line no-console
console.log('Applying canvas preserveDrawingBuffer patch...');
const originalGetContext = HTMLCanvasElement.prototype.getContext;
function getContextPatched(contextId, contextAttributes) {
if (contextId === 'webgl' || contextId === 'webgl2') {
if (typeof contextAttributes !== 'object' || contextAttributes === null) {
contextAttributes = {};
}
contextAttributes.preserveDrawingBuffer = true;
}
return originalGetContext.apply(this, [contextId, contextAttributes]);
}
HTMLCanvasElement.prototype.getContext = getContextPatched;
Object.defineProperty(HTMLCanvasElement.prototype, '__isPatchedForPreserveDrawingBuffer', {
value: true,
writable: false,
configurable: true,
});
// eslint-disable-next-line no-console
console.log('✅ Patch applied. New WebGL contexts will now preserve their drawing buffer.');
}
if (typeof window !== 'undefined') {
Object.assign(window, { enablePreserveDrawingBuffer });
}
enablePreserveDrawingBuffer();
}
exports.enableCanvasPreserveDrawingBuffer = enableCanvasPreserveDrawingBuffer;
;