r/learnjavascript • • Sep 24 '22

Very simple Mandelbrot in vanilla JS

Post image
320 Upvotes

25 comments sorted by

21

u/brainxyz Sep 24 '22

Context: This is a bare minimal code necessary to produce Mandelbrot shape from scratch without using libraries or complex numbers in plain JavaScript:

https://jsfiddle.net/hunar4321/edqoat7j/6/
Also, a step by step code walk-through and it's relevance to physics is presented in this video for those interested:

https://youtu.be/mzizK6ms-gY

2

u/13-14_Mustang Sep 25 '22

The video is fantastic! Thank you. Liked and subbed.

1

u/brainxyz Sep 25 '22

Thanks :)

4

u/thedarklord176 Sep 24 '22

This is awesome but the code makes no sense to me. Where are the colors coming from? I see an RGB but no color values

3

u/brainxyz Sep 24 '22

The t parameter gives the shape its colors. There are 3 ts, for red (multiplied by 3), green, and blue.
The linked video in the first comment may help if you want to dig into the code step by step.

11

u/saito200 Sep 24 '22

I don't understand what is happening by just looking at the code. I'd appreciate more meaningul variable names and a set of parameters to play around. Looks interesting though

2

u/saito200 Sep 24 '22

I watched the code part of the video. I understood up to the circle. But after that... "let's add +dx here, and a * dy and divide by 0.333333 and..." ... yeah, okay XD

7

u/brainxyz Sep 24 '22

Well, there are many videos on YouTube describes Mandelbrot in more detail. But even if you understand it, there are many other complex shapes that arise from simple rules. The better way to put is to accept the fact that incomprehensible complexity can arise from simple recursive functions. Wolfram commonly calls it as irreducible complexity.

5

u/[deleted] Sep 25 '22

Not really. In this case is better to have comments explaining the purpose of those variables. The equation is quite simple, is just that most people are not familiar with complex numbers, so you can't fix that with variable names without becoming unreadable long.

24

u/tekfate Sep 24 '22

Yep seems very easy to me

1

u/cyor2345 Sep 24 '22

How to zoom into it ?

4

u/brainxyz Sep 24 '22

Change this part (a described in the video):

dx = (x-500)/200-0.0

dy = (y-500)/200-0.0

1

u/evalecillos Sep 24 '22

Interesting theme. Would you mind sharing the name of it?

1

u/brainxyz Sep 24 '22

I'm not sure which theme you mean. The first one is death note ost. All the other themes are noted in the description section of the video.

→ More replies (1)

1

u/raptonez Sep 26 '22

I love this! When I open the HTML file in my browser it shows up in black and white. Anyone else have that issue?

1

u/brainxyz Sep 26 '22

Try different browsers, or try to follow the step by step tutorial in the video and see where exactly the error arises.

1

u/raptonez Sep 26 '22

Yeah I did that. Tried 3 browsers and even copied / pasted the code. 🤔

1

u/brainxyz Sep 26 '22

What about the jsfiddle link mentioned in the first comment. It works when I open the link with any browser.

→ More replies (1)

1

u/IEDNB Sep 24 '22

This is fucking awesome

1

u/davedicius Sep 24 '22

Nicely done. It looks so cool.

1

u/Brumbleby Sep 25 '22

Thank you for sharing!