xxxxxxxxxx
58
let slider = {
x: 200,
y: 30,
radius: 20,
display(){
rect(0,0,windowWidth,this.y+this.radius);
line(0,this.y+this.radius,windowWidth,this.y+this.radius);
line(30,this.y,windowWidth-30,this.y);
ellipse(this.x,this.y,this.radius,this.radius);
}
}
let probConstant;
let dragMode = false;
function setup() {
createCanvas(windowWidth, windowHeight);
frameRate(25);
background(220);
probConstant = (slider.x-30)*1024/windowWidth;
}
function draw() {
slider.display();
if (mouseIsPressed){
if (!(onSlider() || dragMode))
splotch();
else
dragSlider();
}
}
function splotch(){
for (let i=-50; i<50; i++){
for (let j=-50; j<50; j++){
let dist = sqrt(i**2 + j**2);
if (Math.random() < prob(dist) && mouseY+j > slider.y+slider.radius)
point(mouseX+i, mouseY+j);
}
}
}
function prob(dist){
return exp(-(dist**2)/probConstant);
}
function onSlider(){
return sqrt((mouseX - slider.x)**2 + (mouseY - slider.y)**2) < slider.radius + 5;
}
function dragSlider(){
dragMode = true;
slider.x = mouseX>30 ? (mouseX<windowWidth-30? mouseX : windowWidth-30) : 30;
probConstant = (slider.x-30)*1024/windowWidth;
}
function mouseReleased(){
dragMode = false;
}