Skip to content

Conversation

@maazrk
Copy link

@maazrk maazrk commented Sep 9, 2025

Previous unmerged PR: jnicol/particleground#34

On window resize, the previous canvas object was not being cleared

Summary by CodeRabbit

  • Bug Fixes

    • Teardown now fully clears particle state, preventing lingering artifacts or performance issues after resize or reinitialization.
  • Refactor

    • Resize behavior now rebuilds the drawing surface, ensuring visuals adapt correctly to new dimensions and styles.
    • Particle population is recalculated on resize for accurate density and consistent appearance across screen sizes.

@vercel
Copy link

vercel bot commented Sep 9, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
daisyjs-demo Ready Ready Preview Comment Sep 9, 2025 5:46am

@coderabbitai
Copy link

coderabbitai bot commented Sep 9, 2025

Walkthrough

On window resize, the module now performs a full teardown and reinitialization: it calls destroy(), clears particles, removes/recreates the canvas, rebinds context, reapplies styling, and recalculates layout and particle density. destroy() explicitly resets the particle array. Public API signatures remain unchanged.

Changes

Cohort / File(s) Summary
Resize and teardown rework
daisy.js
Replaced in-place resize handling with full destroy-and-reinit flow. Added explicit particles = [] in destroy() to clear state. On resize: remove old canvas, create new canvas with class pg-canvas, set display, insert into DOM, get new 2D context, reapply styles, recalc density and reindex particles.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor User as Window
  participant D as Daisy Module
  participant DOM as Canvas DOM
  participant P as Particles

  User->>D: resize event
  activate D
  D->>D: destroy()
  note right of D: Log teardown
  D->>P: particles = []
  D->>DOM: remove existing canvas (if present)

  D->>DOM: create new canvas (.pg-canvas, display:block)
  D->>D: ctx = canvas.getContext("2d")
  D->>DOM: insert canvas into container
  D->>D: apply styling and layout calculations
  D->>P: recalc density, reindex/reseed particles
  deactivate D
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Pre-merge checks (2 passed, 1 warning)

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title Check ✅ Passed The title directly describes the key change—clearing the previous canvas object on window resize to fix the buffer overflow—and concisely summarizes the main intent of the pull request without extraneous details.
Description Check ✅ Passed The description refers to the relevant previous pull request and succinctly explains the specific issue addressed by this changeset—namely, that the canvas was not being cleared on resize—making it clearly related to the code changes.

Poem

I hopped through code, a tidy sight,
Reset the field on window’s might—
Canvas anew, pixels align,
Particles cleared, then re-design.
With every breeze of resize air,
I plant fresh stars with careful care.
Thump-thump: a clean, reactive lair.

Tip

👮 Agentic pre-merge checks are now available in preview!

Pro plan users can now enable pre-merge checks in their settings to enforce checklists before merging PRs.

  • Built-in checks – Quickly apply ready-made checks to enforce title conventions, require pull request descriptions that follow templates, validate linked issues for compliance, and more.
  • Custom agentic checks – Define your own rules using CodeRabbit’s advanced agentic capabilities to enforce organization-specific policies and workflows. For example, you can instruct CodeRabbit’s agent to verify that API documentation is updated whenever API schema files are modified in a PR. Note: Upto 5 custom checks are currently allowed during the preview period. Pricing for this feature will be announced in a few weeks.

Example:

reviews:
  pre_merge_checks:
    custom_checks:
		  - name: "Undocumented Breaking Changes"
			  mode: "warning"
			  instructions: |
				  Flag potential breaking changes that are not documented:
				  1. Identify changes to public APIs/exports, CLI flags, environment variables, configuration keys, database schemas, or HTTP/GraphQL endpoints (including removed/renamed items and changes to types, required params, return values, defaults, or behavior).
				  2. Ignore purely internal/private changes (e.g., code not exported from package entry points or marked internal).
				  3. Verify documentation exists: a "Breaking Change" section in the PR description and updates to CHANGELOG.md.

Please share your feedback with us on this Discord post.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
daisy.js (1)

365-373: Make destroy() safe for internal use and null-guard canvas removal

Add an internal flag so resize can reuse destroy without nuking hooks or $.data. Also guard canvas removal to avoid NPEs.

-function destroy() {
-      console.log('destroy');
-      particles = [];
-      canvas.parentNode.removeChild(canvas);
-      hook('onDestroy');
-      if ($) {
-        $(element).removeData('plugin_' + pluginName);
-      }
-    }
+function destroy(internal) {
+      if (!internal) { console.log('destroy'); }
+      particles = [];
+      if (canvas && canvas.parentNode) {
+        canvas.parentNode.removeChild(canvas);
+      }
+      if (!internal) {
+        hook('onDestroy');
+        if ($) {
+          $(element).removeData('plugin_' + pluginName);
+        }
+      }
+    }
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b282900 and 0ca756b.

📒 Files selected for processing (1)
  • daisy.js (2 hunks)
🔇 Additional comments (1)
daisy.js (1)

367-367: Clearing particle state on teardown is correct

Resetting particles prevents stale references and ensures density is recalculated cleanly after resize.

Comment on lines +151 to +158
destroy();
//Create canvas
canvas = document.createElement('canvas');
canvas.className = 'pg-canvas';
canvas.style.display = 'block';
element.insertBefore(canvas, element.firstChild);
ctx = canvas.getContext('2d');

Copy link

@coderabbitai coderabbitai bot Sep 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Avoid full destroy() on resize — it breaks the jQuery plugin handle and fires onDestroy on every resize

Calling destroy() here removes $.data('plugin_daisyjs') and triggers the onDestroy hook, so $(elem).daisyjs('pause'|'start'|...) will stop working after the first resize. Use an internal teardown variant instead.

Apply:

-      destroy();
+      // Internal teardown: keep plugin handle and skip hooks
+      destroy(true);

Optional: if you see flicker or transient errors while the canvas is detached, pause around the rebuild (cancelAnimationFrame/resume) or debounce resize with rAF.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
destroy();
//Create canvas
canvas = document.createElement('canvas');
canvas.className = 'pg-canvas';
canvas.style.display = 'block';
element.insertBefore(canvas, element.firstChild);
ctx = canvas.getContext('2d');
// Internal teardown: keep plugin handle and skip hooks
destroy(true);
//Create canvas
canvas = document.createElement('canvas');
canvas.className = 'pg-canvas';
canvas.style.display = 'block';
element.insertBefore(canvas, element.firstChild);
ctx = canvas.getContext('2d');

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maazrk kindly take a look.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!

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.

2 participants