Skip to content

Comments

fix: lazily initialize canvas element in ProgressGauge service#89

Closed
Kunal1522 wants to merge 1 commit intom-lab:mainfrom
Kunal1522:fix/gauge-service-dom-access
Closed

fix: lazily initialize canvas element in ProgressGauge service#89
Kunal1522 wants to merge 1 commit intom-lab:mainfrom
Kunal1522:fix/gauge-service-dom-access

Conversation

@Kunal1522
Copy link
Contributor

@Kunal1522 Kunal1522 commented Feb 19, 2026

Fixes #90

gaugeService.js called document.getElementById('activeProgress') at factory creation time — before Angular's router rendered the view containing that element. This caused a TypeError: Cannot read properties of null (reading 'getContext') crash.

Fix

Introduced a getCanvas() helper that initializes aProgress and barCTX on first use. All public functions call getCanvas() before accessing the canvas.

-  var aProgress = document.getElementById('activeProgress');
-  var barCTX = aProgress.getContext("2d");
+  var aProgress = null;
+  var barCTX = null;
+
+  function getCanvas() {
+    if (aProgress.getContext('2d') with a TypeError.) {
+      aProgress = document.getElementById('activeProgress');
+      barCTX = aProgress.getContext('2d');
+    }
+  }

By the time any of the public functions (create, reset, progress) are called from the controller, Angular has already rendered the view and #activeProgress exists in the DOM.

Testing

Verified by reviewing call sites in measure.jsProgressGauge.create() is called inside the controller after Angular bootstraps, ensuring the element is present.

The ProgressGauge factory accessed document.getElementById('activeProgress')
at service creation time, before Angular's router had rendered the view
containing that element. This caused aProgress to be null and crashed on
aProgress.getContext('2d') with a TypeError.

Fix by introducing a getCanvas() helper that initializes aProgress and
barCTX on first use. All public functions call getCanvas() before
accessing the canvas, ensuring the DOM element exists by the time it
is needed.
@robertodauria
Copy link
Contributor

robertodauria commented Feb 20, 2026

Thanks for the contribution. Closing this as it is superseded by #83, which rewrites the codebase removing Angular, jQuery, Gulp, and the old app/ directory structure. The files modified by this PR either no longer exist after that change or the change has already been made.

We aim to merge #83 next week, which will give contributors a stable, modern base to work from. Thoughtful, targeted contributions that build on where the project is heading are much more valuable. We'd encourage you to review #83 and open any future PRs on top of that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: gaugeService.js accesses #activeProgress before Angular renders the view

2 participants