xxxxxxxxxx
let colorAlpha = 0.3;
let colorMax = 10;
let veil;
let maxInk = 500;
let debug = false;
function preload() {
//しっぽり明朝 Ver.2.100
font = loadFont("ShipporiMincho-Regular.otf");
}
function setup() {
createCanvas(windowWidth - 10, windowHeight - 10);
colorMode(HSB, colorMax);
frameRate(30);
noStroke();
textFont(font);
textAlign(CENTER, CENTER);
veil = new Veiler();
keyPressed();
}
function draw() {
background(0, 0, colorMax);
if (mouseIsPressed) {
veil.Add(createVector(mouseX, mouseY));
}
if (veil.count === 0) {
fill(0);
textSize(min(width / 3, height) * 0.5);
text("幾華学", width / 2, height / 2);
} else if (debug) {
fill(0);
textSize(12);
text(veil.count, width / 2, 12);
}
veil.DrawVeil();
}
function keyPressed() {
if (key === 'd' || key === 'D') {
debug = !debug;
} else {
veil.Reset();
}
}
class Veiler {
constructor() {
this.head = null;
this.tail = null;
this.coloring = 0;
this.alpha = colorMax * colorAlpha;
this.count = 0;
this.Reset();
}
Reset() {
this.count = 0;
this.tail = this.head = new Vertex(null, createVector(width / 2, height / 2), color(this.coloring, colorMax, colorMax, colorMax * colorAlpha));
}
DrawVeil() {
if (this.head.next == null) return;
beginShape(TRIANGLE_FAN);
vertex(width / 2, height / 2);
for (let vert = this.head.next; vert !== null; vert = vert.next) {
fill(vert.c);
vertex(vert.pos.x, vert.pos.y);
if (debug) vert.drawDire();
vert.pos.add(vert.dire);
this.NearCheck(vert);
}
endShape();
}
NearCheck(v) {
if ((p5.Vector.sub(v.prevPos, v.pos)).magSq() > 9) return;
if (v.prev !== this.head) {
v.SetPrevPos();
} else {
this.Remove();
}
}
Remove() {
this.count--;
let head = this.head;
let rem = head.next;
head.next = head.next.next;
if (head.next !== null) {
head.next.prev = head;
head.next.SetPrevPos();
} else {
this.tail = head;
}
return rem;
}
Add(pos) {
this.count++;
let position = createVector(pos.x, pos.y, 0);
this.coloring = (this.coloring + 1 / getFrameRate()) % colorMax;
let v;
if (this.count < maxInk) {
v = new Vertex(this.tail, position, color(this.coloring, colorMax, colorMax, this.alpha));
} else {
v = this.Remove();
v.Set(this.tail, position, color(this.coloring, colorMax, colorMax, this.alpha));
v.next = null;
}
this.tail = this.tail.next = v;
}
}
class Vertex {
constructor(pre, Pos, c) {
this.next = null;
this.prevPos = null;
this.c = c;
this.Set(pre, Pos, c);
}
Set(pre, Pos, c) {
this.prev = pre;
this.pos = Pos;
if (this.prev !== null) {
this.SetPrevPos();
this.c = c;
}
}
SetPrevPos() {
this.prevPos = createVector(this.prev.pos.x, this.prev.pos.y);
this.dire = p5.Vector.sub(this.prevPos, this.pos).normalize();
}
drawDire() {
stroke(0);
let pos = this.pos;
line(pos.x, pos.y, pos.x + this.dire.x * 10, pos.y + this.dire.y * 10);
noStroke();
}
}