Compare commits
	
		
			No commits in common. "master" and "gitea-pages" have entirely different histories.
		
	
	
		
			master
			...
			gitea-page
		
	
		
							
								
								
									
										15
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										15
									
								
								README.md
									
									
									
									
									
								
							@ -1,15 +0,0 @@
 | 
			
		||||
A simple static HTML page using JavaScript Canvas to draw recurisive tree. Click [here](https://djmil.pages.djmil.dev/canva-tree/index.html) to see the final result. 
 | 
			
		||||
 | 
			
		||||
# How does it work
 | 
			
		||||
 | 
			
		||||
In it's essence, `index.html` is responsible for providing tree-drawing configuration by placeing initial tree trunk somewhere on the page. Than, the recursive nature of the called function will do the rest of the job by drawing two, slightly smaller & slightly different, sub-trunks on top of the parrent trunk. The core trick is that the beginning of each new branch derived from the end of it's predecessor.
 | 
			
		||||
 | 
			
		||||
# Trigonometry cheetsheet 
 | 
			
		||||
 | 
			
		||||

 | 
			
		||||
 | 
			
		||||
Key: by knowing *coordinates* `x1`, `y1` of a starting point, we want to find a *destination point* `x2`, `y2` by using *distance* `L` and *angle* `Alpha` as additional constaraints.
 | 
			
		||||
 | 
			
		||||
# Pages
 | 
			
		||||
 | 
			
		||||
The pages server, self-hosted at `www.pages.djmil.dev`, is listening for your requests for the web-page content in the form of `http://user.pages.djmil.dev/repo/file.html`, than fetches the desired data from the underline Gitea instance to serve it as a responce. Check [gitea-pages](https://gitea.djmil.dev/goland/gitea-pages) for more details.
 | 
			
		||||
							
								
								
									
										1
									
								
								gitea-pages.toml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								gitea-pages.toml
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1 @@
 | 
			
		||||
allowedrefs=["*"]
 | 
			
		||||
							
								
								
									
										44
									
								
								index.html
									
									
									
									
									
								
							
							
						
						
									
										44
									
								
								index.html
									
									
									
									
									
								
							@ -1,44 +0,0 @@
 | 
			
		||||
<!DOCTYPE html>
 | 
			
		||||
<html>
 | 
			
		||||
<head>
 | 
			
		||||
    <meta charset="utf-8" />
 | 
			
		||||
    <title>Recursive tree</title>
 | 
			
		||||
<!--    <link rel="stylesheet" href="css/style.css"> -->
 | 
			
		||||
	<style>
 | 
			
		||||
		canvas {
 | 
			
		||||
			background: #eee; 
 | 
			
		||||
			display: block;
 | 
			
		||||
			margin: 0 auto;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		p  { width: 800px; margin: 0 auto; }
 | 
			
		||||
		ol { width: 800px; margin: 0 auto; }
 | 
			
		||||
 | 
			
		||||
	</style>
 | 
			
		||||
</head>
 | 
			
		||||
 | 
			
		||||
<body>
 | 
			
		||||
 | 
			
		||||
<section>
 | 
			
		||||
	<h1 align="center">Recursive tree</h1>
 | 
			
		||||
	<p>Reload the page to see another one.</p>
 | 
			
		||||
</section>
 | 
			
		||||
 | 
			
		||||
<canvas id="tree" width="1000" height="750"></canvas>
 | 
			
		||||
 | 
			
		||||
<section>
 | 
			
		||||
	<p>Find out how this magick works at the <a href="https://gitea.djmil.dev/djmil/canva-tree">canvas-tree</a> repo.</p>
 | 
			
		||||
	<p align="right">(c) djmil</p>
 | 
			
		||||
</section>
 | 
			
		||||
 | 
			
		||||
<script src="tree.js"></script>
 | 
			
		||||
 | 
			
		||||
<script>
 | 
			
		||||
	var canvas = document.getElementById("tree");
 | 
			
		||||
    ctx = canvas.getContext("2d");
 | 
			
		||||
	tree(ctx, canvas.width/2,  canvas.height, 200);
 | 
			
		||||
</script>
 | 
			
		||||
 | 
			
		||||
</body>
 | 
			
		||||
 | 
			
		||||
</html>
 | 
			
		||||
							
								
								
									
										34
									
								
								tree.js
									
									
									
									
									
								
							
							
						
						
									
										34
									
								
								tree.js
									
									
									
									
									
								
							@ -1,34 +0,0 @@
 | 
			
		||||
const angle2rad = angle => angle * (Math.PI / 180)
 | 
			
		||||
 | 
			
		||||
const stem = (ctx, x1, y1, length, angle) => {
 | 
			
		||||
	// Визначити координати кіня стовбура знаючи початкову точку, довжину та кут нахилу
 | 
			
		||||
	// Так, це тригонометрія в дії :)
 | 
			
		||||
    const alpha = angle2rad(angle);
 | 
			
		||||
	let x2 = x1 + length * Math.sin(alpha);
 | 
			
		||||
	let y2 = y1 - length * Math.cos(alpha);
 | 
			
		||||
	
 | 
			
		||||
	ctx.beginPath();
 | 
			
		||||
	ctx.moveTo(x1, y1);
 | 
			
		||||
	ctx.lineTo(x2, y2);
 | 
			
		||||
	ctx.lineWidth = length / 10; // Товщина стовбура залежить від його довжини
 | 
			
		||||
	ctx.stroke();
 | 
			
		||||
	ctx.closePath();
 | 
			
		||||
 | 
			
		||||
	// Закоментуй цей IF. Що змінилось в картинці дерева? Чому стало гірше?
 | 
			
		||||
	if (length > 20) {
 | 
			
		||||
		x2 = x1 + (length * 0.95) * Math.sin(alpha);
 | 
			
		||||
		y2 = y1 - (length * 0.95) * Math.cos(alpha);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
    if (length > 1) {
 | 
			
		||||
		const rnd1 = Math.random() *20  -10;  // [-10.0 .. +10.0]
 | 
			
		||||
		const rnd2 = Math.random() *0.2 +0.6; // [  0.6 ..   0.8]
 | 
			
		||||
 | 
			
		||||
		// Рекурсивно малюємо розгалудження стовбура
 | 
			
		||||
		// Початком нового стовбура слугує кінець поточного стовбура
 | 
			
		||||
      	stem(ctx, x2, y2, length * rnd2, angle + 35 +rnd1);
 | 
			
		||||
      	stem(ctx, x2, y2, length * rnd2, angle - 25 +rnd1);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
const tree = (ctx, x, y, height) => stem(ctx, x, y, height, 0);
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user