Thanks to visit codestin.com
Credit goes to github.com

Skip to content

m-sarabi/chaos-game

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

76 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Chaos Game

License: MIT NPM Version

A high-performance, highly configurable fractal generator based on the Chaos Game algorithm, using a Web Worker and an OffscreenCanvas to ensure a smooth, non-blocking user experience, even with millions of points.

Gallery

Here are a few examples of fractals generated with this tool, showcasing different settings.

Title Settings Image
Sierpinski triangle sides: 3
jumpDistance: 0.5
restriction: 'none'

alt text

Sierpinski Hexagon sides: 6
jumpDistance: 0.6667
restriction: 'none'

alt text

The Hexagonal Bloom sides: 6
jumpDistance: 0.585786
restriction: 'none'
centerVertex: true
solidBg: false
fgColor: "#FF0040"
gammaExponent: 5

alt text

Unnamed sides: 5
jumpDistance: 1.618034
restriction: 'no-repeat'
fgColor: "#FFFF80"
bgColor: "#081020"
gammaExponent: 4
canvasSize: 1000
padding: 380

alt text

Unnamed sides: 10
jumpDistance: 0.618034
restriction: 'none'
fgColor: "#E05C5C"

alt text

What is the Chaos Game?

The Chaos Game is a simple algorithm for generating fractals. It works like this:

  1. Define a set of points (e.g., the vertices of a polygon).
  2. Start with a random point anywhere inside the polygon.
  3. Randomly Choose one of the polygon's vertices.
  4. Move a fraction of the distance (e.g., halfway) from your current point toward the chosen vertex.
  5. Plot a point at this new location.
  6. Repeat from step 3, using the newly plotted point as the current point.

By repeating this process thousands or millions of times, self-similar fractal patterns emerge from the chaos.

Features

  • Non-Blocking Simulation: All calculations run in a separate Web Worker, so the main UI thread remains responsive.
  • Extensive Customization: Control the number of sides, jump distance, colors, extra vertices, and more.
  • Advanced Restriction Rules: Go beyond the basic algorithm by applying rules that forbid choosing certain vertices, such as:
    • no-repeat: Cannot pick the same vertex twice in a row.
    • no-neighbor: Cannot pick a vertex adjacent to the last one.
    • ...and more!
  • Symmetry Mode: Instantly create perfectly symmetrical fractals by applying rotational and reflectional symmetry to every plotted point.
  • Smart Simulation Control: Features like live rendering, auto-stop when the fractal is complete, and easy PNG download.

Getting Started

You can use Chaos Game JS either by installing it as an NPM package or by including it directly in your HTML file from a CDN.

Option 1: Usage with a Module Bundler (Vite, Webpack, etc.)

  1. Installation

    npm install chaos-game
  2. HTML Setup Create an index.html file with a canvas element. Make sure your worker.js file is accessible from your project's public directory.

    <!DOCTYPE html>
    <html lang="en">
    <body>
      <canvas id="chaos-canvas" width="800" height="800"></canvas>
      <script type="module" src="main.js"></script>
    </body>
    </html>
  3. JavaScript Usage In your main.js file, import the ChaosGame class, define your settings, and start the simulation.

    import { ChaosGame } from 'chaos-game';
    
    const canvas = document.getElementById('chaos-canvas');
    
    const settings = {
      canvasSize: 800,
      sides: 5,
      jumpDistance: 0.6,
      restriction: 'no-repeat',
      symmetrical: true,
      // ... other settings
    };
    
    const game = new ChaosGame(canvas, settings);
    game.play();

Option 2: Usage in the Browser via CDN

  1. HTML Setup Simply add the script tag to your index.html. The ChaosGame class will be available globally.

    <!DOCTYPE html>
    <html lang="en">
    <body>
      <canvas id="chaos-canvas" width="800" height="800"></canvas>
    
      <!-- 1. Load the library from the CDN -->
      <script src="https://cdn.jsdelivr.net/npm/[email protected]"></script>
    
      <!-- 2. Your script to run the game -->
      <script>
        const canvas = document.getElementById('chaos-canvas');
    
        const settings = {
          canvasSize: 800,
          sides: 5,
          jumpDistance: 0.6,
          restriction: 'no-repeat',
          symmetrical: true,
          // ... other settings
        };
    
        // Note: The worker.js is loaded automatically by the library
        const game = new ChaosGame(canvas, settings);
        game.play();
      </script>
    </body>
    </html>

Configuration (ChaosGameSettings)

You can pass any of these options in the settings object during instantiation or update them later with updateSetting(key, value).

Option Type Default Description
canvasSize number 1000 Width and height of the canvas in pixels.
sides number 3 Number of vertices in the main polygon.
jumpDistance number 0.5 Fraction of the distance to move towards the chosen vertex.
padding number 10 Margin in pixels from the canvas edge to the polygon.
midpointVertex boolean false If true, adds vertices at the midpoint of each side.
centerVertex boolean false If true, adds a vertex at the center of the polygon.
restriction string 'none' The rule for choosing the next vertex (e.g., 'none', 'no-repeat').
fgColor string '#FFFFFF' The foreground color for points and outlines (CSS format).
bgColor string '#000000' The background color (CSS format).
solidBg boolean true If true, use a solid background; otherwise, it's transparent.
gammaExponent number 3 Gamma correction value for adjusting brightness and contrast.
drawCircle boolean false If true, draws the circumscribing circle.
drawPolygon boolean false If true, draws the main polygon's outline.
symmetrical boolean true If true, applies rotational and reflectional symmetry.
autoStop boolean true If true, the simulation stops automatically when stable.
liveRendering boolean true If true, the canvas updates periodically during the simulation.
stabilityNewPixelsThreshold number 1 Threshold for new pixels below which the simulation is considered stable.

Methods

  • .play(): Starts or resumes the simulation.
  • .stop(): Pauses the simulation.
  • .reset(): Clears the canvas and restarts the simulation with the current settings.
  • .updateSetting(key, value): Updates a single setting.
  • .download(): Triggers a download of the current canvas as a PNG file.
  • .on(eventName, callback): Listens for events like 'finish' or 'stabilityCheck'.

License

This project is licensed under the MIT License. See the LICENSE file for details.


Created by Mohammad Sarabi.