MCPcopy
hub / github.com/chartjs/Chart.js / initCanvas

Function initCanvas

src/platform/platform.dom.js:41–91  ·  view source on GitHub ↗

* Initializes the canvas style and render size without modifying the canvas display size, * since responsiveness is handled by the controller.resize() method. The config is used * to determine the aspect ratio to apply in case no explicit height has been specified. * @param {HTMLCanvasElement} ca

(canvas, aspectRatio)

Source from the content-addressed store, hash-verified

39 * @param {number} [aspectRatio]
40 */
41function initCanvas(canvas, aspectRatio) {
42 const style = canvas.style;
43
44 // NOTE(SB) canvas.getAttribute('width') !== canvas.width: in the first case it
45 // returns null or '' if no explicit value has been set to the canvas attribute.
46 const renderHeight = canvas.getAttribute('height');
47 const renderWidth = canvas.getAttribute('width');
48
49 // Chart.js modifies some canvas values that we want to restore on destroy
50 canvas[EXPANDO_KEY] = {
51 initial: {
52 height: renderHeight,
53 width: renderWidth,
54 style: {
55 display: style.display,
56 height: style.height,
57 width: style.width
58 }
59 }
60 };
61
62 // Force canvas to display as block to avoid extra space caused by inline
63 // elements, which would interfere with the responsive resize process.
64 // https://github.com/chartjs/Chart.js/issues/2538
65 style.display = style.display || 'block';
66 // Include possible borders in the size
67 style.boxSizing = style.boxSizing || 'border-box';
68
69 if (isNullOrEmpty(renderWidth)) {
70 const displayWidth = readUsedSize(canvas, 'width');
71 if (displayWidth !== undefined) {
72 canvas.width = displayWidth;
73 }
74 }
75
76 if (isNullOrEmpty(renderHeight)) {
77 if (canvas.style.height === '') {
78 // If no explicit render height and style height, let's apply the aspect ratio,
79 // which one can be specified by the user but also by charts as default option
80 // (i.e. options.aspectRatio). If not specified, use canvas aspect ratio of 2.
81 canvas.height = canvas.width / (aspectRatio || 2);
82 } else {
83 const displayHeight = readUsedSize(canvas, 'height');
84 if (displayHeight !== undefined) {
85 canvas.height = displayHeight;
86 }
87 }
88 }
89
90 return canvas;
91}
92
93// Default passive to true as expected by Chrome for 'touchstart' and 'touchend' events.
94// https://github.com/chartjs/Chart.js/issues/4287

Callers 1

acquireContextMethod · 0.85

Calls 2

isNullOrEmptyFunction · 0.85
readUsedSizeFunction · 0.85

Tested by

no test coverage detected