xxxxxxxxxx
129
let inks = []
function setup() {
createCanvas(400, 400);
colorMode(HSB, 255)
background(220);
}
let vol = 1
function mousePressed() {
if (mouseIsPressed) {
vol = vol + 10
setTimeout(mousePressed, 0.25)
} else {
inks.push( new InkSpot(mouseX, mouseY, color(150, 255, 200, 255), min(vol, 10000)))
vol = 1
}
}
function draw() {
for (let ink of inks) {
ink.draw()
ink.spread()
}
}
class InkPoint{
constructor(x, y, pigment){
this.x = x
this.y = y
this.pigment = pigment
}
draw(){
stroke(this.pigment)
point(this.x, this.y)
}
getNeighbors(){
let x = this.x
let y = this.y
return [
[x, y+1],
[x, y-1],
[x+1, y],
[x-1, y]]
}
getNeighbors8(){
let x = this.x
let y = this.y
return [
[x, y+1],
[x, y-1],
[x+1, y],
[x-1, y],
[x+1,y+1],
[x-1,y-1],
[x+1,y-1],
[x-1,y+1],
]
}
}
class InkSpot{
constructor(x, y, pigment, volume){
this.volume = volume
this.pigment = pigment
this.points = new Map()
this.undrawn_points = new Map()
this.undrawn_points.set(str_key(x, y), new InkPoint(x, y, pigment))
}
draw(){
for (let p of this.undrawn_points.values()) {
p.draw()
this.points.set(str_key(p.x, p.y), p)
}
this.undrawn_points = new Map()
}
spread(){
if (this.volume > 0) {
let values = Array.from(this.points.values())
for (let p of values) {
let nbrs = p.getNeighbors8()
for (let n of nbrs) {
let x = n[0]
let y = n[1]
if (this.volume <= 0) break
if (!this.points.has(str_key(x, y)) && (random(1.0) > 0.85)) {
this.volume--
this.undrawn_points.set(str_key(x, y), new InkPoint(x, y, decay(p.pigment)))
}
}
}
} else {
self.points = new Map()
}
}
}
function decay(pigment) {
let h = hue(pigment)
let s = saturation(pigment)
let b = brightness(pigment)
let a = alpha(pigment)
return color(h, s, b, a * random(0.8, 1.0))
}
function str_key(x, y) {
return str(x) + ',' + str(y)
}