-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
157 lines (140 loc) · 3.69 KB
/
main.js
File metadata and controls
157 lines (140 loc) · 3.69 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const root = document.getElementById('root');
const canv = document.createElement('canvas');
let ww = 800, wh = 700;
let d = Math.round(Math.max(wh-window.innerHeight,0)*.1)*10;
if(d){d += 46; ww -= d; wh -= d;}
canv.width = ww;
canv.height = wh;
canv.style.backgroundColor = '#5c5c5c';
root.appendChild(canv);
const ctx = canv.getContext('2d');
const p = document.createElement('p');
document.querySelector('body').appendChild(p);
p.innerHTML = 'press space to change view';
const mouse = {x: 0, y: 0};
const num = 30;
let ID, selected, hdots;
let loop = true, add = true, mousedown = false, view = 1;
let nodes = [];
let a = 0, b = 0;
const del = Delaunay;
del.init(ww, wh);
Perlin.noiseDetail(5, 0.3);
function draw(){
del.reset();
a += 0.002;
for(let i = 0; i < num; i++){
b+=0.00001;
nodes[i].x = Perlin.noise(i+b, a)*ww*1.3;
nodes[i].y = Perlin.noise(2000+i+b, 2000+a+b)*wh*1.3;
// repell(nodes[i])
del.add_point(nodes[i].x, nodes[i].y);
}
del.demo(ctx, view);
ID = window.requestAnimationFrame(draw);
}
init();
function init(){
for(let i = 0; i < num; i++)
nodes[i] = {x:0, y:0, vx: 0, vy: 0};
ID = window.requestAnimationFrame(draw);
};
function loadPattern(imgpath, cb){
let img = new Image(100,100);
img.src = imgpath;
img.onload = ()=>{
createImageBitmap(img).then((img)=>{
let pattern = ctx.createPattern(img, 'repeat');
cb(pattern);
});
};
}
function repell(node){
node.vx = lerp(node.vx, (Math.random()-.5)*5, .1);
node.vy = lerp(node.vy, (Math.random()-.5)*5, .1);
node.x += node.vx;
node.y += node.vy;
let d2 = Math.atan2(node.x-ww*.5, node.y-wh*.5);
let d2b = Math.sqrt((node.x-ww*.5)*(node.x-ww*.5) + (node.y-wh*.5)*(node.y-wh*.5))*.005;
node.x -= .5*Math.sin(d2)*d2b;
node.y -= .5*Math.cos(d2)*d2b;
let d = Math.sqrt((node.x-mouse.x)*(node.x-mouse.x) + (node.y-mouse.y)*(node.y-mouse.y));
let th = Math.atan2(node.x-mouse.x, node.y-mouse.y);
let dx = 200*Math.sin(th);
let dy = 200*Math.cos(th);
node.x +=dx/d;
node.y +=dy/d;
}
window.onkeydown = (e)=>{
if(e.key === ' '){
view = ++view % 8;
if(view == 0){
if(!loop){
nodes = [];
for(let i = 0; i < num; i++) nodes[i] = {x:0, y:0, vx: 0, vy: 0};
ID = window.requestAnimationFrame(draw);
p.innerHTML = 'press space to change view';
loop = true;
}
}
if(view > 3){
if(loop){
window.cancelAnimationFrame(ID);
p.innerHTML = 'press space to change view (click to add points or drag to move)';
loop = false;
del.reset();
nodes = [];
}
del.demo(ctx, view, hdots);
}
// del.setHashV(view === 5);
}
}
window.onmousemove = (e)=>{
mouse.x = e.clientX;
mouse.y = e.clientY;
if(mousedown && view > 3){
hdots = ((view == 4 || view == 7) && nodes.length < 3) ? nodes.slice(0, 2) : null;
if(!add){
selected.x = mouse.x; selected.y = mouse.y;
del.reset();
for(var i = 0; i < nodes.length; i++){
del.add_point(nodes[i].x, nodes[i].y);
}
del.demo(ctx, view, hdots);
// del.drawVCell(ctx, selected, '#5f5f5f');
}
}
}
window.onmousedown = (e)=>{
mousedown = true;
if(view > 3){
add = true;
for(let i = 0; i < nodes.length; i++){
if(dist(mouse.x, mouse.y, nodes[i].x, nodes[i].y) <= 12){
add = false;
selected = nodes[i];
break;
}
}
if(add){
nodes.push({x: mouse.x, y: mouse.y});
del.reset();
for(let i = 0; i < nodes.length; i++){
del.add_point(nodes[i].x, nodes[i].y);
}
hdots = ((view == 4 || view == 7) && nodes.length < 3) ? nodes.slice(0, 2) : null;
del.demo(ctx, view, hdots);
}
}
}
window.onmouseup = (e)=>{
mousedown = false;
// del.demo(ctx, view, hdots);
}
function lerp(a, b, n){
return n*(b-a)+a;
}
function dist(ax, ay, bx, by){
return Math.sqrt((ax-bx)*(ax-bx)+(ay-by)*(ay-by));
}