Friday, March 20, 2020

Content for now

I'm going to fiddle with this later. The code is not that ugly.  Aside from JQuery and math.js, it's under 200 lines of source half of which is Javascript.  It still needs commenting but I'm off to 3D land to start working of a tree version of FibBox which is already a bit mind warping.  Here's the latest version of the Fibonnaci trees.

Tuesday, March 17, 2020

Solved but still plugging

The issue was easier than I thought.  It was the 45 degree rotation which I wanted to better emphasize the treeness of the structure.
This has none of the unexpected lines.  I'm still not loving some of the boundary colour meshing but these are just choices which I can now put some time to improve on.
This update has some hoving CSS that allows you to see the Fibonacci connections of the pieces.  So you can take a look at what is going on before I complete the version that may work as a decent teaching example for coding. Here is an example of seeing that that div has a width of 144 and a height of 89 which are both Fibonacci numbers.

Wednesday, March 04, 2020

Still plugging...

This is close but not quite ready for prime time....
A collection of Fibonacci Trees
Although I've never heard of Fibonacci Trees, it seems like a reasonable name for this since it is made up of Fibonacci spirals. It's created with a bunch rectangles with integer coordinates that are Fibonacci numbers. I thought that I could easily use border-top-left radius:100% and conic-gradients... It's some finicky code that seems to produce a bunch of artifacts that I can't get rid of... Currently, the rough draft of the code needs a lot of polish but I feel there is no point streamlining it until it can produce what I want without the artifacts.... I could fix a few of the colour issues that occur in the upper tree not nicely meshing but since the other problems can't be fixed I'll leave it.


There seems to be no issues with the sizes and placements so the artifacts are related to the conic-gradient and perhaps a browser thing...  There is a better way to colour but not one that will be easy least using CSS. 
Perhaps the 3D version of this tree will be more interesting and will fall together better.


Sunday, February 23, 2020

Back to JQuery...

I got beaten by CORS (cross-origin resource sharing).  To really show the utility of using divs to construct fractals I wanted to use images. Being a lazy sort and less interested in picking the best set of images, I decided to use an somewhat arbitrary collection of images from a site like Flickr. And Boom I had a CORS issue. The cross-origin issue is not easy to solve without a library--a pure Javascript solution was far from pretty, intuitive, or readily available. I ended up retreating back to JQuery.  Here's a what I ended up with:

You can find this code on my dumping site (jimmorey.com) or in this CodePen. I'm cheating a bit with the image sizes--the Sierpinski carpet's holes aren't quite as big as these images but I wanted to show off the images more than accurately have a fractal. Generally, I've been thinking about using the fractal as more of an information display for images.  Perhaps later.

I'll explore a few more parameter tweaks to see if I can have some more fun with graphics. I may explore some rectangular ones but these were my first two.

These used "cat,cute" to find the Flickr images. Make sure to take a look at the active code--in total, it's under 150 lines.  It allows you to hover over images to brighten and in the case of this later triangular one reshape to square images to better see the images. 

I will explore other libraries and photo sites like google photos to see if I can construct a better teaching example but probably I'll let it sit for a bit.

Monday, February 10, 2020

Sierpinski with divs and CSS


A simple construction with plain Javascript and CSS can be seen here.  It is part of my push to make examples that have a few prerequisites.  In this case, ES6 Javascript, CSS, and HTML.  I make use of divs as the main graphical elements. This chunk of code

    function iterate(el){  
        [[1,1],[1,-1],[-1,1]].forEach(x=> {
            let newEl = el.cloneNode();
            newEl.style["transform"]=
                `scale(0.5) 
                 translate(${x[0]*el.offsetWidth/2}px,
                ${x[1]*el.offsetHeight/2}px)`;
            el.appendChild(newEl);
        });
    }
adds three shifted elements, a quarter of the original's size. Each shift is relative to the center of the original, corresponding to all the corners excluding the upper-left corner, which would be the [-1,-1] .

After thee iterates, there are 27 equally small divs as seen below and to the left. To get the more attractive triangles on the right, a class, called half, transforms the appearance of the squares into triangles.


Essentially, the squares are 0x0 divs with a 100px border where as the triangles are divs with borders described in the CSS below
 
    .half{ /* the bottom right half of a 0x0 div */
        border-bottom:100px solid rgba(0,0,255,0.3);
        border-right:100px solid rgba(0,0,255,0.3);
        border-left:100px solid transparent;
        border-top:100px solid transparent;
    }



A further bit of CSS transforms the outer square into a rhombus which transforms the blue part into an equilateral triangle. The class rhomb below skews the shape putting the vertical lines into a 30 degree lines going to the right and squashes the vertical so that the length of the skewed vertical lines are the same as the horizontal lines. The shift (translate) puts bottom horizontal line approximately where the original was.

    .rhomb { 
        transform:skew(30deg) scale(1,0.866) translate(-58px,0);
    }

To further show off this div construction of this pattern, a Button is used to highlight how each div sits in other divs. The Button

        <button onclick="show()">show divs</button>

On button presses, the highlight class is either added or removed from special divs (div.done).

function show(){ // toggles whether the borders are shown in iterations
    let toggle = document.getElementsByClassName("highlight").length > 0;
    Array.from(document.getElementsByClassName("done")).forEach(x=>{ 
        if (toggle) x.classList.remove("highlight");
        else  x.classList.add("highlight");
    });
}

Style can be used to suggest its construction. Above, the stacked shaded backgrounds reveal how many divisions a particular regions is included in. Adding a border to each div can better show how the construction was achieved.

    .highlight { 
        border:2px red solid; 
        background:rgba(220,220,100,0.2);
    }

This code can be a simplified by a Javascript library or framework with  complication of having to something else to learn  and potentially having to later switch if something better or more popular comes along.

Popular Posts