Skip to content

Improve fire effect with mouse interaction#2

Open
peignoir wants to merge 1 commit into
masterfrom
codex/improve-mouse-interaction-effect
Open

Improve fire effect with mouse interaction#2
peignoir wants to merge 1 commit into
masterfrom
codex/improve-mouse-interaction-effect

Conversation

@peignoir
Copy link
Copy Markdown
Owner

@peignoir peignoir commented May 20, 2025

Summary

  • add mouse interaction to fire effect
  • colorize flames and reuse canvas size
  • style canvas with crosshair cursor and black background

Testing

  • git status --short

Summary by CodeRabbit

  • New Features

    • Added interactive fire effect: moving the mouse over the canvas injects heat, allowing users to influence the animation in real time.
  • Style

    • Updated the canvas appearance with a crosshair cursor and a black background for improved visual clarity.
  • Refactor

    • Improved the structure and readability of the fire animation for better performance and maintainability.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 20, 2025

Walkthrough

The updates enhance the fire simulation by refining the buffer logic, introducing mouse-driven heat injection, and improving code clarity. Canvas styling is adjusted via inline CSS for a crosshair cursor and black background. The JavaScript refactors variable declarations, optimizes fire propagation, and adds interactive mouse controls while removing unused code.

Changes

File(s) Change Summary
fire.html Added inline CSS to the <canvas> element: sets cursor to crosshair and background to black.
fire.js Refactored code structure, clarified variable declarations, improved fire propagation logic, added mouse-driven heat injection, updated color mapping, removed unused code, and adjusted the fire update interval.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Canvas
    participant JS_FireSim

    User->>Canvas: Moves mouse over canvas
    Canvas->>JS_FireSim: Triggers mousemove event with coordinates
    JS_FireSim->>JS_FireSim: Updates mouse coordinates
    JS_FireSim->>JS_FireSim: Injects heat around mouse in buffer
    JS_FireSim->>JS_FireSim: Computes fire propagation
    JS_FireSim->>Canvas: Renders updated fire effect
    loop Every 30ms
        JS_FireSim->>JS_FireSim: Repeat fire simulation and rendering
    end
Loading

Poem

In the land of pixel flame and heat,
A rabbit hops with nimble feet.
Now the fire dances where you steer,
Mouse in paw, the sparks appear!
With crosshair gaze and canvas night,
The code now blazes, bold and bright.
🐇🔥

Note

⚡️ AI Code Reviews for VS Code, Cursor, Windsurf

CodeRabbit now has a plugin for VS Code, Cursor and Windsurf. This brings AI code reviews directly in the code editor. Each commit is reviewed immediately, finding bugs before the PR is raised. Seamless context handoff to your AI code agent ensures that you can easily incorporate review feedback.
Learn more here.


Note

⚡️ Faster reviews with caching

CodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure Review - Disable Cache at either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off the Data Retention setting under your Organization Settings.
Enjoy the performance boost—your workflow just got faster.

✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown

@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: 3

🧹 Nitpick comments (1)
fire.html (1)

11-11: Maintain consistent indentation.

The closing HTML tag has inconsistent indentation compared to the opening tag. While this doesn't affect functionality, consistent indentation improves code readability.

-</html>
+  </html>
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
Cache: Disabled due to data retention organization setting
Knowledge Base: Disabled due to data retention organization setting

📥 Commits

Reviewing files that changed from the base of the PR and between 11a54b4 and 9a60f94.

📒 Files selected for processing (2)
  • fire.html (1 hunks)
  • fire.js (1 hunks)
🧰 Additional context used
🪛 Biome (1.9.4)
fire.js

[error] 47-47: Shouldn't redeclare 'x'. Consider to delete it or rename it.

'x' is defined here:

(lint/suspicious/noRedeclare)


[error] 64-64: Shouldn't redeclare 'x'. Consider to delete it or rename it.

'x' is defined here:

(lint/suspicious/noRedeclare)


[error] 65-65: Shouldn't redeclare 'y'. Consider to delete it or rename it.

'y' is defined here:

(lint/suspicious/noRedeclare)

🔇 Additional comments (8)
fire.html (1)

9-9: Better visual experience with canvas styling!

The added inline styles improve the user experience by providing a crosshair cursor (indicating interactivity) and setting a black background that will enhance the visibility of the fire effect.

fire.js (7)

4-11: Good practice: Explicit variable declarations.

Properly declaring all global variables at the top of the file improves code organization and makes dependencies clear. The mouse coordinate tracking variables will be essential for the new interactive features.


13-23: Improved formatting of the Array.matrix function.

The reformatting makes the function more readable while maintaining its original functionality.


26-32: Streamlined heat source logic.

Creating the imageData inside the function and simplifying the heat source logic at the bottom rows makes the code more maintainable. The approach of setting heat values only for the last two rows is more efficient.


34-45: Great interactive feature: Mouse-based heat injection!

This new feature adds interactivity by creating heat around the mouse position, matching the PR objective. The implementation correctly:

  1. Calculates mouse position in canvas coordinates
  2. Creates a 5x5 square of heat around the cursor
  3. Validates the coordinates to prevent out-of-bounds errors

72-72: Improved variable scoping in setPixel.

Good job making the index variable locally scoped to the function.


84-95: Enhanced initialization with mouse event handling.

The initialization is now more robust with proper canvas setup and buffer initialization. The mouse event listener is well-implemented, correctly calculating relative coordinates within the canvas.

The fire interval change from 1ms to 30ms (approximately 33fps) is a good performance optimization that will reduce CPU usage while maintaining smooth animation.


50-54: Conditional fire propagation logic based on x position.

The code has different propagation logic for edge cells versus inner cells. The inner cell calculation uses a 4-point average, while the edge uses a simpler 2-point average with a faster cooling rate (-4 instead of -1).

This approach makes sense as edge cells have fewer neighbors and need special handling.

Comment thread fire.js
Comment on lines +64 to +68
for (var x = 0; x < width; x++){
for (var y = 1; y < height; y++) {
Buffer1[x][y] = Buffer2[x][y];
}
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Buffer update logic needs variable scope fixing.

The buffer update operation correctly copies the new state back to the original buffer for the next iteration.

However, there's a variable redeclaration issue here as well.

Fix the redeclaration of 'x' and 'y' variables:

-    for (var x = 0; x < width; x++){
-        for (var y = 1; y < height; y++) {
+    for (let x = 0; x < width; x++){
+        for (let y = 1; y < height; y++) {

Using let instead of var ensures proper block scoping and prevents potential issues.

📝 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
for (var x = 0; x < width; x++){
for (var y = 1; y < height; y++) {
Buffer1[x][y] = Buffer2[x][y];
}
}
for (let x = 0; x < width; x++) {
for (let y = 1; y < height; y++) {
Buffer1[x][y] = Buffer2[x][y];
}
}
🧰 Tools
🪛 Biome (1.9.4)

[error] 64-64: Shouldn't redeclare 'x'. Consider to delete it or rename it.

'x' is defined here:

(lint/suspicious/noRedeclare)


[error] 65-65: Shouldn't redeclare 'y'. Consider to delete it or rename it.

'y' is defined here:

(lint/suspicious/noRedeclare)

🤖 Prompt for AI Agents
In fire.js around lines 64 to 68, the loop variables 'x' and 'y' are declared
with 'var', causing them to have function scope and potentially leading to bugs.
Change the declarations of 'x' and 'y' from 'var' to 'let' to ensure they have
block scope within the loops, preventing variable redeclaration issues and
improving code safety.

Comment thread fire.js
Comment on lines +47 to +61
for (var x = 1; x < (width - 1); x+=1){
for (var y = 1; y < (height - 1); y+=1) {
if(!Buffer1[x +1]) {Buffer1[x + 1] = 0;}
if(x>1 && x < (width-2)) {
Buffer2[x][y-1] = (Buffer1[x - 1][y] + Buffer1[x + 1][y] + Buffer1[x][y + 1] + Buffer1[x][y - 1]) / 4 - 1;
} else {
Buffer2[x][y-1] = (Buffer1[x][y]+ Buffer1[x][y+1])/2 -4;
}
var val = Buffer2[x][y];
var r = val;
var g = val * 0.5;
var b = 0;
setPixel(imageData, x, y, r, g, b, 0xff);
}
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Verification agent

🧩 Analysis chain

Fire propagation algorithm with improved color mapping.

The refactored fire propagation logic is more readable with explicit RGB value calculations. Setting red to the full value, green to half, and blue to zero creates a more natural fire color gradient.

However, there's a potential issue with variable redeclaration.

The static analysis tool flagged variable redeclaration issues. Fix the redeclaration of 'x' to avoid potential scope problems:

-    for (var x = 1; x < (width - 1); x+=1){
+    for (let x = 1; x < (width - 1); x+=1){

This ensures the 'x' variable is block-scoped and won't interfere with other loops.


🏁 Script executed:

#!/bin/bash
# Search for all 'var x' declarations in fire.js to confirm redeclaration occurrences
rg -n "var x" fire.js

# Search for all 'var y' declarations as well
rg -n "var y" fire.js

Length of output: 280


Use block-scoped loop variables in fire.js
To prevent unintended variable reuse across loops, change all var‐declared loop counters to let.

• Affected locations in fire.js:
– Line 29: for (var x = 1; …)for (let x = 1; …)
– Line 47: for (var x = 1; …)for (let x = 1; …)
– Line 64: for (var x = 0; …)for (let x = 0; …)
– Line 48: for (var y = 1; …)for (let y = 1; …)
– Line 65: for (var y = 1; …)for (let y = 1; …)

Example diff for one loop:

-    for (var x = 1; x < width - 1; x += 1) {
+    for (let x = 1; x < width - 1; x += 1) {

This ensures each loop’s x and y are block-scoped and won’t collide.

📝 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
for (var x = 1; x < (width - 1); x+=1){
for (var y = 1; y < (height - 1); y+=1) {
if(!Buffer1[x +1]) {Buffer1[x + 1] = 0;}
if(x>1 && x < (width-2)) {
Buffer2[x][y-1] = (Buffer1[x - 1][y] + Buffer1[x + 1][y] + Buffer1[x][y + 1] + Buffer1[x][y - 1]) / 4 - 1;
} else {
Buffer2[x][y-1] = (Buffer1[x][y]+ Buffer1[x][y+1])/2 -4;
}
var val = Buffer2[x][y];
var r = val;
var g = val * 0.5;
var b = 0;
setPixel(imageData, x, y, r, g, b, 0xff);
}
}
for (let x = 1; x < width - 1; x += 1) {
for (var y = 1; y < (height - 1); y += 1) {
if (!Buffer1[x + 1]) { Buffer1[x + 1] = 0; }
if (x > 1 && x < (width - 2)) {
Buffer2[x][y - 1] = (
Buffer1[x - 1][y] +
Buffer1[x + 1][y] +
Buffer1[x][y + 1] +
Buffer1[x][y - 1]
) / 4 - 1;
} else {
Buffer2[x][y - 1] = (Buffer1[x][y] + Buffer1[x][y + 1]) / 2 - 4;
}
var val = Buffer2[x][y];
var r = val;
var g = val * 0.5;
var b = 0;
setPixel(imageData, x, y, r, g, b, 0xff);
}
}
🧰 Tools
🪛 Biome (1.9.4)

[error] 47-47: Shouldn't redeclare 'x'. Consider to delete it or rename it.

'x' is defined here:

(lint/suspicious/noRedeclare)

🤖 Prompt for AI Agents
In fire.js around lines 47 to 61, the loop variables 'x' and 'y' are declared
using 'var', which can cause variable redeclaration and scope issues. Change
these declarations from 'var' to 'let' to ensure block scoping and prevent
unintended variable reuse across loops. Apply this fix consistently to all
similar loops in the file as noted.

Comment thread fire.js
Comment on lines +49 to +51
if(!Buffer1[x +1]) {Buffer1[x + 1] = 0;}
if(x>1 && x < (width-2)) {
Buffer2[x][y-1] = (Buffer1[x - 1][y] + Buffer1[x + 1][y] + Buffer1[x][y + 1] + Buffer1[x][y - 1]) / 4 - 1;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Verification agent

🧩 Analysis chain

Guard against undefined array elements.

Line 49 checks for undefined Buffer1[x+1], but this check appears isolated and potentially incomplete.

This check might not be sufficient as it only verifies one index while the next line accesses multiple array elements. Consider a more comprehensive validation or explain the purpose of this specific check.


🏁 Script executed:

#!/bin/bash
# Check for other array access bounds validation in the codebase
rg "Buffer1\[\w+\s?[\+\-]\s?\d+\]" fire.js

Length of output: 220


🏁 Script executed:

#!/bin/bash
# Show surrounding context for Buffer1 usage to inspect loop bounds and y-index handling
rg -n -C 10 "Buffer1" fire.js

Length of output: 2979


Remove or correct the incomplete bounds check on Buffer1

The if (!Buffer1[x + 1]) { Buffer1[x + 1] = 0; } line is both unnecessary (your loops already keep x in [1, width–2]) and insufficient (it only checks that row exists, not the y-index, and if triggered would replace an array with a number). You have two options:

  • Remove that guard entirely and rely on your existing loop bounds to keep all Buffer1[x±1][y±1] accesses valid.

  • Or, if you really need a safety net, replace it with a full check of the specific slot, e.g.:

    if (Buffer1[x + 1][y] === undefined) {
      Buffer1[x + 1][y] = 0;
    }

Locations to update:

  • fire.js, line 49: drop or fix the existing if (!Buffer1[x + 1]) … block.
🤖 Prompt for AI Agents
In fire.js around lines 49 to 51, the current check if (!Buffer1[x + 1]) is
incomplete and incorrect because it only verifies the existence of the row but
not the specific element, and it assigns a number to an array slot which can
cause errors. To fix this, either remove this guard entirely since the loops
already constrain x within valid bounds, or replace it with a precise check for
the element Buffer1[x + 1][y], setting it to 0 if undefined. Update line 49
accordingly by removing or correcting this check to ensure safe and consistent
array access.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant