-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
87 lines (83 loc) · 1.79 KB
/
script.js
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
let points=[],n,step=1-0.5,noofsides=3,x,y,an=0,drag=false,dragIn=-1,prevPos=0;
let side,dis;
function setup() {
createCanvas(windowHeight,windowHeight)
sliders();
n = createVector(random(width),random(height));
init();
background(0);
stroke(255);
}
function sliders(){
side = createSlider(3,10,3,1);
side.position(width+20,100);
dis = createSlider(0.5,0.9,0.5,0.01);
dis.position(width+20,200);
}
function draw() {
strokeWeight(5);
stroke(255);
if(step!=dis.value()){
step=dis.value();
background(0);
};
if(noofsides!=side.value()){
noofsides=side.value();
init();
};
if(mouseIsPressed){
drag=true;
}
strokeWeight(1);
// stroke(255,20);
for(let i=0;i<1000;i++){
let clr=map(dist(width/2,height/2,n.x,n.y),0,height,150,255);
stroke(clr,255-clr,clr/2,20);
//stroke(255,10);
point(abs(n.x),abs(n.y));
setPoint();
}
draging();
}
function init(){
points = [];
an=0;
m=TWO_PI/noofsides;
len =height/2-10;
for(let i=0;i<noofsides;i++){
x = width/2 + len*cos(an);
y = height/2 + len*sin(an);
points.push(createVector(x,y));
an+=m;
}
background(0);
}
const setPoint=()=>{
let ran = int(random(noofsides));
if(ran!=prevPos){
let m = atan2(points[ran].y - n.y,points[ran].x - n.x);
let len = dist(n.x,n.y,points[ran].x,points[ran].y)*step;
n.x = n.x + len*cos(m);
n.y = n.y + len*sin(m);
prevPos=ran;
}
}
function draging(){
if(drag){
if(dragIn==-1)
points.forEach( (p,i)=>{
if(dist(p.x,p.y,mouseX,mouseY) < 25){
dragIn=i;
}
})
else if(dragIn >-1){
points[dragIn].x=mouseX;
points[dragIn].y=mouseY;
}
}
}
function mouseReleased(){
drag = false;
dragIn = -1;
background(0);
}