forked from jmdejong/s6
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrassgen.html
90 lines (82 loc) · 1.75 KB
/
grassgen.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<!DOCTYPE html>
<html>
<head>
<style>
body {
text-align: center;
}
canvas {
border: 3px solid black;
}
</style>
<script>
function Rng(a) {
// source: https://stackoverflow.com/a/47593316
return function() {
var t = a += 0x6D2B79F5;
t = Math.imul(t ^ t >>> 15, t | 1);
t ^= t + Math.imul(t ^ t >>> 7, t | 61);
return ((t ^ t >>> 14) >>> 0) / 4294967296;
}
}
class Range{
constructor(min, max) {
if (max >= min){
this.min = min;
this.max = max;
} else {
this.min = max;
this.max = min;
}
}
sample(t) {
return t * (this.max - this.min) + this.min;
}
}
function redraw() {
let seed = 1;
let width = 256;
let height = 128;
let nblades = 64;
let nsegments = 6;
let spread = 64;
let colorspread = 0.4;
let bladeWidth = new Range(2, 8);
let random = Rng(seed);
let canvas = document.getElementById("canvas");
canvas.width = width;
canvas.height = height;
let ctx = canvas.getContext("2d");
for (let i=0; i<nblades; ++i){
let base = {
x: new Range(0.1, 0.9).sample(random()) * width,
y: 0,
w: bladeWidth.sample(random())
};
let end = {
x: new Range(Math.max(base.x - spread, 0), Math.min(base.x + spread, width)).sample(random()),
y: new Range(height / 2, height).sample(random()),
w: base.w / 2
};
let control = {
x: new Range(Math.max(base.x - spread, 0), Math.min(base.x + spread, width)).sample(random()),
y: new Range(end.y / 2, end.y).sample(random()),
w: new Range(end.w, base.w).sample(random())
}
ctx.beginPath();
ctx.moveTo(base.x, height - base.y);
ctx.lineTo(control.x, height - control.y);
ctx.lineTo(end.x, height - end.y);
ctx.stroke();
}
}
function main(){
redraw();
}
addEventListener("load", main)
</script>
</head>
<body>
<canvas id="canvas"></canvas>
</body>
</html>