Interactive fractal tree <canvas>

Developer
Size
2,450 Kb
Views
2,024

How do I make an interactive fractal tree <canvas>?

Renders an interactive fractal tree in <canvas> using the angle relative to the current cursor's x-position.. What is a interactive fractal tree <canvas>? How do you make a interactive fractal tree <canvas>? This script and codes were developed by Mirjamsk on 26 January 2023, Thursday.

Interactive fractal tree <canvas> Previews

Interactive fractal tree <canvas> - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Interactive fractal tree <canvas></title> <link href='https://fonts.googleapis.com/css?family=Raleway:500&subset=latin-ext' rel='stylesheet' type='text/css'> <link rel="stylesheet" href="css/style.css">
</head>
<body> <div> Try moving your mouse accross the width of the screen.
</div>
<canvas id="canvas" width="600" height="600"></canvas> <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script> <script src="js/index.js"></script>
</body>
</html>

Interactive fractal tree <canvas> - Script Codes CSS Codes

canvas { display: block; margin-left: auto; margin-right: auto;
}
div{ margin: 20vh 3vw; position: absolute; font-family: 'Raleway';
}

Interactive fractal tree <canvas> - Script Codes JS Codes

var depth = 9;
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
context.fillStyle = '#000';
context.lineWidth = 1.5;
var deg_to_rad = Math.PI / 180.0;
function drawTree(x1, y1, angle, offset, depth){ if (depth == 9){ var x2 = x1 + (Math.cos(-90 * deg_to_rad) * depth * 8.0); var y2 = y1 + (Math.sin(-90 * deg_to_rad) * depth * 8.0); drawLine(x1, y1, x2, y2, depth); drawTree(x2, y2, -90 - offset, offset, depth - 1); drawTree(x2, y2, -90 + offset, offset, depth - 1); } else if (depth != 0){ var x2 = x1 + (Math.cos(angle * deg_to_rad) * depth * 8.0); var y2 = y1 + (Math.sin(angle * deg_to_rad) * depth * 8.0); drawLine(x1, y1, x2, y2, depth); drawTree(x2, y2, angle - offset, offset, depth - 1); drawTree(x2, y2, angle + offset, offset, depth - 1); }
}
function drawLine(x1, y1, x2, y2, brightness){ context.moveTo(x1, y1); context.lineTo(x2, y2);
}
function canvasDraw(angleOffset){ context.clearRect(0, 0, canvas.width, canvas.height); context.beginPath(); drawTree(300, 370, -90, angleOffset, depth); context.closePath(); context.stroke();
}
var k = 1;
function quick_demo(){ canvasDraw(k++); if (k <=90) requestAnimationFrame(quick_demo);
}
function getAngle(x){ return parseInt(1 + 89 * x /$(window).width());}
$(document).ready(function() { quick_demo(); canvasDraw(20); $(window).mousemove(function( e ) { canvasDraw(getAngle(e.pageX)); });
});
Interactive fractal tree <canvas> - Script Codes
Interactive fractal tree <canvas> - Script Codes
Home Page Home
Developer Mirjamsk
Username alojzije
Uploaded January 26, 2023
Rating 4.5
Size 2,450 Kb
Views 2,024
Do you need developer help for Interactive fractal tree <canvas>?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Mirjamsk (alojzije) Script Codes
Create amazing Facebook ads with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!