There won't be any official way to detect that. The thing is, even when the browser's Hardware Acceleration (HWA) setting is set, you can't be sure that the browser will actually use HWA for your rendering. A bunch of circumstances may prevent the use of HWA at a given moment and your graphic could be rendered through the software renderer even if HWA is normally supported (this is mainly true for Firefox, but other browsers may also have software rendering fallbacks for when HWA is temporarily unavailable).
What you can try though, is to render something you'd expect a software renderer and an HWA renderer would render differently, and force the software renderer to compare with the default one. The canvas 2D API offers a mean to force rendering through the software renderer: willReadFrequently
, and HWA is known to be pretty bad at rendering clean obliques.
const hasHWA = (() => {
// create a test function for both "default" drawing and forced software
const test = (force=false) => {
// Firefox (at lest on macOS) doesn't accelerate OffscreenCanvas
const canvas = document.createElement("canvas");
// willReadFrequently will force software rendering
const ctx = canvas.getContext("2d", { willReadFrequently: force });
ctx.moveTo(0, 0),
ctx.lineTo(120, 121); // HWA is bad at obliques
ctx.stroke();
return ctx.getImageData(0, 0, 200, 200).data.join();
};
// check that both return different results
return test(true) !== test(false);
})();
console.log({ hasHWA });
But while this may work in most cases (works in my FF and Chrome on macOS), it's certainly not bullet proof, since the ultimate goal is to have HWA renderers as precise as software ones, so use this test with caution.