kaka.farm

Unnamed repository; edit this file 'description' to name the repository.
git clone https://kaka.farm/~git/kaka.farm
Log | Files | Refs | README

main.js (3546B)


      1 'use strict;'
      2 
      3 Math.TAU = 2 * Math.PI;
      4 
      5 (function() {
      6 	let line_a = [
      7 		[Math.random(), Math.random()],
      8 		[Math.random(), Math.random()],
      9 	];
     10 
     11 	let line_b = [
     12 		[Math.random(), Math.random()],
     13 		[Math.random(), Math.random()],
     14 	];
     15 
     16 	const HSV_VALUE_VALUE = 0.7;
     17 	const HSV_SATURATION_VALUE = 0.7;
     18 	const WANTED_NUMBER_OF_LINES = 50;
     19 
     20 	function lerp(p, a, b) {
     21 		return p * (b - a) + a;
     22 	}
     23 
     24 	function lerp_2d(p, a, b) {
     25 		return [
     26 			lerp(p, a[0], b[0]),
     27 			lerp(p, a[1], b[1]),
     28 		];
     29 	}
     30 
     31 	function hsv_to_rgb(hue, saturation, value) {
     32 		// https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_RGB
     33 		// H in [0, 1] (position on the color wheel)
     34 		// S in [0, 1]
     35 		// V in [0, 1]
     36 
     37 		let chroma = value * saturation;
     38 
     39 		let hue_tag = hue * 6;
     40 
     41 		let x = chroma * (1 - Math.abs((hue_tag % 2) - 1));
     42 
     43 		let red_1, green_1, blue_1;
     44 
     45 		if (hue_tag >= 0 && hue_tag < 1) { 
     46 			[red_1, green_1, blue_1] = [chroma, x, 0];
     47 		} else if (hue_tag >= 1 && hue_tag < 2) {
     48 			[red_1, green_1, blue_1] = [x, chroma, 0];
     49 		} else if (hue_tag >= 2 && hue_tag < 3) { 
     50 			[red_1, green_1, blue_1] = [0, chroma, x];
     51 		} else if (hue_tag >= 3 && hue_tag < 4) { 
     52 			[red_1, green_1, blue_1] = [0, x, chroma];
     53 		} else if (hue_tag >= 4 && hue_tag < 5) { 
     54 			[red_1, green_1, blue_1] = [x, 0, chroma];
     55 		} else if (hue_tag >= 5 && hue_tag < 6) { 
     56 			[red_1, green_1, blue_1] = [chroma, 0, x];
     57 		};
     58 
     59 		let m = value - chroma;
     60 
     61 		return [red_1 + m, green_1 + m, blue_1 + m];
     62 	}
     63 
     64 	function draw_line(line, rgb) {
     65 		let canvas = document.getElementById('canvas');
     66 		let ctx = canvas.getContext('2d');
     67 
     68 		ctx.beginPath();
     69 
     70 		ctx.strokeStyle = `rgb(${rgb[0]}, ${rgb[1]}, ${rgb[2]})`;
     71 
     72 		ctx.moveTo(canvas.width * line[0][0], canvas.height * line[0][1]);
     73 		ctx.lineTo(canvas.width * line[1][0], canvas.height * line[1][1]);
     74 
     75 		ctx.stroke();
     76 	}
     77 
     78 	function uniform_random_direction() {
     79 		let uniform = Math.random();
     80 		let direction_vector = [
     81 			Math.cos(uniform * Math.TAU),
     82 			Math.sin(uniform * Math.TAU),
     83 		];
     84 		return direction_vector;
     85 	}
     86 
     87 	function draw() {
     88 		let canvas = document.getElementById('canvas');
     89 		let ctx = canvas.getContext('2d');
     90 
     91 		ctx.clearRect(0, 0, canvas.width, canvas.height);
     92 
     93 		//draw_line(line_a, [255, 255, 255]);
     94 		//draw_line(line_b, [255, 255, 255]);
     95 
     96 
     97 		for (let current_line_number = 0; current_line_number < WANTED_NUMBER_OF_LINES; current_line_number++) {
     98 			let bezier_curve_portion = current_line_number / (WANTED_NUMBER_OF_LINES - 1);
     99 			let rgb = hsv_to_rgb(
    100 				bezier_curve_portion,
    101 				HSV_SATURATION_VALUE,
    102 				HSV_VALUE_VALUE,
    103 			).map(x => 255 * x);
    104 
    105 			let bezier_line = [
    106 				lerp_2d(bezier_curve_portion, line_a[0], line_a[1]),
    107 				lerp_2d(bezier_curve_portion, line_b[0], line_b[1]),
    108 			];
    109 
    110 			draw_line(bezier_line, rgb);
    111 		}
    112 	}
    113 
    114 	function resize_window() {
    115 		// https://stackoverflow.com/a/32119392
    116 
    117 		let canvas = document.getElementById('canvas');
    118 
    119 		canvas.width = window.innerWidth;
    120 		canvas.style.width = window.innerWidth;
    121 		canvas.height = window.innerHeight;
    122 		canvas.style.height = window.innerHeight;
    123 
    124 		draw();
    125 	}
    126 
    127 	function make_new_world() {
    128 		line_a = [
    129 			[Math.random(), Math.random()],
    130 			[Math.random(), Math.random()],
    131 		];
    132 		line_b = [
    133 			[Math.random(), Math.random()],
    134 			[Math.random(), Math.random()],
    135 		];
    136 	}
    137 
    138 	function main() {
    139 		let canvas = document.getElementById('canvas');
    140 
    141 		resize_window();
    142 		make_new_world();
    143 
    144 		canvas.addEventListener('click', function() {
    145 			make_new_world();
    146 			draw();
    147 		});
    148 
    149 		window.addEventListener('resize', resize_window)
    150 	}
    151 
    152 	window.addEventListener('load', main);
    153 })();