@jdfranel/snowflake-generator
v1.0.1
Published
Generates svg snowflakes using Bronian tree.
Downloads
2
Readme
Snowflake Generator
A Snowflake generator using Bronian tree method. From a snowflake you can create an SVG view using a path or multiple points.
This implementation is based on Daniel Shiffman's Coding Challenge 127: Brownian Motion Snowflake impementation using p5.js.
It works in both Node.js and the browser.
More informations on the drawing of a snowflake on this code challenge - Draw a Snowflake page.
Installation
Node.js
npm install @jdfranel/snowflake-generator --save
const SnowflakeGenerator = require('@jdfranel/snowflake-generator');
var snowflake = SnowflakeGenerator.create();
Global object
Include the pre-built script.
<script src="./build/snowflake-generator.js"></script>
<script>
var snowflake = SnowflakeGenerator.create();
</script>
Build
npm run build
Documentation
SnowflakeGenerator
public static create(particleDistance: number): Snowflake
Returns an initiated snowflake with 1 particle. This snowflake can have more particle until the particleDistance
is reached.
particleDistance
: The distance at witch particles will start to get to the snowflake
public static generate(particleDistance: number): Snowflake
Returns a fully grown snowflake using snowflake.generate()
. Depending of the particleDistance
the generation of a snowflake can take some time (around 1.3s for a distance of 500).
particleDistance
: The distance at witch particles will start to get to the snowflake
Snowflake
constructor(distance: number = 500)
Creates a new snowflake to be generated with 1 particle.
distance
: The distance at witch particles will start to get to the snowflake
public isDone(): boolean
Indicates if the snowflake is completed.
public generate(): void
Generate the snowflake by using addParticle()
until isDone()
.
public addParticle(): number
Adds a patricle to the snowflake.
Returns The number of moves needed to make the particle meet the snowflake or -1
if the snowflake is already completed.
public getParticles(): Particle[]
Returns the particle array.
public toSVG(size: number = 100): ViewElement
Generates an SVG path representation of the particles composing the snowflake.
size
: The width and height of the svg output.
public toSVGPath(size?: number): ViewElement
Generates an SVG path representation of the particles composing the snowflake.
size
: The width and height of the svg output.
public toSVGDots(size?: number): ViewElement
Generates an SVG dots representation of the particles composing the snowflake.
size
: The width and height of the svg output
ViewElement
The ViewElement
class exists to allow the library to generate the SVG structure and convert it as a platform compatible format.
public toDomElement(): Element
Generates an SVG DOM Element that you can append into your document when using Snowflake generator in a browser.
public toString(): string
Generates an string representation of the SVG Element that you can print, save or do whatever you need when using Snowflake generator as a nodejs module.
Particle
The Particle
class is not of much interest to generate the snowflake but you can tweek the update()
function to fit your likings.
Most of the time you will want to get the particle position using the following:
particle.pos: Victor
The position of the particle is stored in a Victor.js 2d vector.
Demo
You can find a single web page demo that show how you can manipulate a snowflake and render it.
The demo can be found in the ./web
folder.
Credits
- Daniel Shiffman : Inspiration and implementation in p5.js in Coding Challenge 127: Brownian Motion Snowflake
- Jean-Daniel Franel : The rest.
License
MIT
Donate
In case you want to compensate all the snowy fun you have by a beer, you will need this.
- BTC : 37kyGHUfAh7zJ3znaBNhspgHMXddimztox
- ETH : 0x705C45Dcf60EFc7aF6521B4211940dBB7d25aFF5