www.xbdev.net
xbdev - software development
Thursday December 26, 2024
Home | Contact | Support | Fractals Natures pattern... | Fractals Natures pattern...
     
 

Fractals

Natures pattern...

 

Fractals > Smiley Face Fractal Algorithm


You can never have enough smileys! How about a smiley fractal algorithm?

This recursive fractal algorithm draws a smiley face. The drawSmiley function draws a single smiley face, and drawSmileyFractal recursively draws smaller smiley faces at the corners of each face.

Take it for a test spin - full code is below - try adjust the parameters as needed to customize the appearance to make the fractals have more emotions.


fractal smileys


<!DOCTYPE html>
<
html>
<
head>
    <
title>xbdev.net Smiley Face Fractal</title>
    <
style>
        
canvas {
            
border1px solid black;
        }
    </
style>
</
head>
<
body>
    <
canvas id="canvas" width="400" height="400"></canvas>
    <
script>
        const 
canvas document.getElementById('canvas');
        const 
ctx canvas.getContext('2d');

        function 
drawSmiley(xysize) {
            
// Draw face
            
ctx.beginPath();
            
ctx.arc(xysize0Math.PI 2);
            
ctx.stroke();

            
// Draw eyes
            
ctx.beginPath();
            
ctx.arc(size 2size 4size 100Math.PI 2);
            
ctx.stroke();
            
ctx.beginPath();
            
ctx.arc(size 2size 4size 100Math.PI 2);
            
ctx.stroke();

            
// Draw mouth
            
ctx.beginPath();
            
ctx.arc(xysize 0.6Math.PI 0.2Math.PI 0.8);
            
ctx.stroke();
        }

        function 
drawSmileyFractal(xysizedepth) {
            
drawSmiley(xysize);
            if (
depth 0) {
                const 
newSize size 0.6;
                
drawSmileyFractal(sizesizenewSizedepth 1);
                
drawSmileyFractal(sizesizenewSizedepth 1);
                
drawSmileyFractal(sizesizenewSizedepth 1);
                
drawSmileyFractal(sizesizenewSizedepth 1);
            }
        }

        
drawSmileyFractal(canvas.width 2canvas.height 2803);
    </
script>
</
body>
</
html>


Things to Try

• Fill in the smiley faces so they have a solid color (e.g., yellow background)
• Add extra random parameters so each smiley can be slightly different (e.g., bigger/smaller eyes, sad/happy)
• Instead of a 'smiley' try other shapes/patterns, for example, kettles, frogs, flowers, text, ...
• Colors mix in more colors and style
• Explore adding 'depth' (3-dimensional)
• Add a 'save' button so the generated fractal can be exported/saved
• Develop a 'app' page, so the user can select/change options and click 'generate' and it'll build a fractal image
• Modify the 'level of detail' - explore non linear recursion/pattern


Resources & Links


If you like smiley algorithms - you might like another version of the concept which generates lots of different smiley faces by modifying and mixing the various facial features (eyes, mouth and noise)
Smiley Proof Concept Face Code/Demo (Notebook)
Smiley Generator Page












WebGPU Development Pixels - coding fragment shaders from post processing to ray tracing! WebGPU by Example: Fractals, Image Effects, Ray-Tracing, Procedural Geometry, 2D/3D, Particles, Simulations WebGPU Games WGSL 2d 3d interactive web-based fun learning WebGPU Compute WebGPU API - Owners WebGPU Development Cookbook - coding recipes for all your webgpu needs! WebGPU & WGSL Essentials: A Hands-On Approach to Interactive Graphics, Games, 2D Interfaces, 3D Meshes, Animation, Security and Production Kenwright graphics and animations using the webgpu api 12 week course kenwright learn webgpu api kenwright programming compute and graphics applications with html5 and webgpu api kenwright real-time 3d graphics with webgpu kenwright webgpu for dummies kenwright webgpu api develompent a quick start guide kenwright webgpu by example 2022 kenwright webgpu gems kenwright webgpu interactive compute and graphics visualization cookbook kenwright wgsl webgpu shading language cookbook kenwright WebGPU Shader Language Development: Vertex, Fragment, Compute Shaders for Programmers Kenwright wgsl webgpugems shading language cookbook kenwright WGSL Fundamentals book kenwright WebGPU Data Visualization Cookbook kenwright Special Effects Programming with WebGPU kenwright WebGPU Programming Guide: Interactive Graphics and Compute Programming with WebGPU & WGSL kenwright



 
Advert (Support Website)

 
 Visitor:
Copyright (c) 2002-2024 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.