* @param {HTMLCanvasElement} canvas * @param {number} [aspectRatio] * @return {CanvasRenderingContext2D|null}
(canvas, aspectRatio)
| 265 | * @return {CanvasRenderingContext2D|null} |
| 266 | */ |
| 267 | acquireContext(canvas, aspectRatio) { |
| 268 | // To prevent canvas fingerprinting, some add-ons undefine the getContext |
| 269 | // method, for example: https://github.com/kkapsner/CanvasBlocker |
| 270 | // https://github.com/chartjs/Chart.js/issues/2807 |
| 271 | const context = canvas && canvas.getContext && canvas.getContext('2d'); |
| 272 | |
| 273 | // `instanceof HTMLCanvasElement/CanvasRenderingContext2D` fails when the canvas is |
| 274 | // inside an iframe or when running in a protected environment. We could guess the |
| 275 | // types from their toString() value but let's keep things flexible and assume it's |
| 276 | // a sufficient condition if the canvas has a context2D which has canvas as `canvas`. |
| 277 | // https://github.com/chartjs/Chart.js/issues/3887 |
| 278 | // https://github.com/chartjs/Chart.js/issues/4102 |
| 279 | // https://github.com/chartjs/Chart.js/issues/4152 |
| 280 | if (context && context.canvas === canvas) { |
| 281 | // Load platform resources on first chart creation, to make it possible to |
| 282 | // import the library before setting platform options. |
| 283 | initCanvas(canvas, aspectRatio); |
| 284 | return context; |
| 285 | } |
| 286 | |
| 287 | return null; |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * @param {CanvasRenderingContext2D} context |
no test coverage detected