xxxxxxxxxx
247
let walls = [];
let particle;
let t = 0;
let font; // opentype.js font object
let fSize; // font size
let string = "cavolo!"; // text to write
let pts = []; // store path data
let path;
let tempArray = [];
let kern = -20;
let kernIterator = 0;
function preload() {
diatype = "ABCDiatype-Medium-Trial.otf";
loadOpenfont(diatype);
}
function loadOpenfont(fontfile) {
opentype.load(fontfile, function (err, f) {
if (err) {
alert("Font could not be loaded: " + err);
} else {
font = f;
}
});
}
function textToPoints(string, size, x, y, kern) {
var fontPath = font.getPath(string, x, y, size,{letterSpacing:kern});
// convert it to a g.Path object
path = new g.Path(fontPath.commands);
// resample it with equidistant points
return g.resampleByLength(path, 2);
}
function keyPressed() {
if (keyCode == DELETE || keyCode == BACKSPACE) {
if (string.length > 0) {
string = string.substring(0, string.length - 1);
}
}
}
function keyTyped() {
if (keyCode >= 32) {
string += key;
}
}
function setup() {
createCanvas(windowWidth, windowHeight);
walls.push(new Boundary(0, 0, width, 0));
walls.push(new Boundary(width, 0, width, height));
walls.push(new Boundary(width, height, 0, height));
walls.push(new Boundary(0, height, 0, 0));
particle = new Particle();
}
function draw() {
t++;
background(0);
if (font) {
m = textToPoints(string, 200, 20, 200,-0.05);
for (let cmd of m.commands) {
if (cmd.type === "Z") {
for (k = 0; k < tempArray.length; k++) {
index = (k + 1) % (tempArray.length - 1);
walls.push(
new Boundary(
tempArray[k][0],
tempArray[k][1],
tempArray[index][0],
tempArray[index][1]
)
);
}
tempArray = [];
} else {
tempArray.push([cmd.x, cmd.y]);
}
}
s = textToPoints(string, 200, 20, 400,-0.05);
for (let cmd of s.commands) {
if (cmd.type === "Z") {
for (k = 0; k < tempArray.length; k++) {
index = (k + 1) % (tempArray.length - 1);
walls.push(
new Boundary(
tempArray[k][0],
tempArray[k][1],
tempArray[index][0],
tempArray[index][1]
)
);
}
tempArray = [];
} else {
tempArray.push([cmd.x, cmd.y]);
}
}
}
for (let wall of walls) {
wall.show();
}
particle.update(mouseX, mouseY);
particle.look(walls);
walls = [];
walls.push(new Boundary(0, 0, width, 0));
walls.push(new Boundary(width, 0, width, height));
walls.push(new Boundary(width, height, 0, height));
walls.push(new Boundary(0, height, 0, 0));
}
class Boundary {
constructor(x1, y1, x2, y2) {
this.a = createVector(x1, y1);
this.b = createVector(x2, y2);
}
show() {
strokeWeight(1);
stroke(255,255,0);
line(this.a.x, this.a.y, this.b.x, this.b.y);
}
}
class Ray {
constructor(pos, angle) {
this.pos = pos;
this.dir = p5.Vector.fromAngle(angle);
}
lookAt(x, y) {
this.dir.x = x - this.pos.x;
this.dir.y = y - this.pos.y;
this.dir.normalize();
}
show() {
strokeWeight(1);
stroke(255, 30);
push();
translate(this.pos.x, this.pos.y);
line(0, 0, this.dir.x * 10, this.dir.y * 10);
pop();
}
cast(wall) {
const x1 = wall.a.x;
const y1 = wall.a.y;
const x2 = wall.b.x;
const y2 = wall.b.y;
const x3 = this.pos.x;
const y3 = this.pos.y;
const x4 = this.pos.x + this.dir.x;
const y4 = this.pos.y + this.dir.y;
const den = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4);
if (den == 0) {
return;
}
const t = ((x1 - x3) * (y3 - y4) - (y1 - y3) * (x3 - x4)) / den;
const u = -((x1 - x2) * (y1 - y3) - (y1 - y2) * (x1 - x3)) / den;
if (t > 0 && t < 1 && u > 0) {
const pt = createVector();
pt.x = x1 + t * (x2 - x1);
pt.y = y1 + t * (y2 - y1);
return pt;
} else {
return;
}
}
}
class Particle {
constructor() {
this.pos = createVector(width / 2, height / 2);
this.rays = [];
this.angleOffset = 0;
for (let a = 0; a < 360; a += 0.5) {
this.rays.push(new Ray(this.pos, radians(a)));
}
}
update(x, y) {
this.pos.set(x, y);
}
updateAngle(angle) {
this.angleOffset = angle;
this.rays = [];
for (let a = this.angleOffset; a < 360 + this.angleOffset; a += 20) {
this.rays.push(new Ray(this.pos, radians(a)));
}
}
look(walls) {
fill(255, 255, 0);
beginShape();
for (let ray of this.rays) {
let closest = null;
let record = Infinity;
for (let wall of walls) {
const pt = ray.cast(wall);
if (pt) {
const d = p5.Vector.dist(this.pos, pt);
if (d < record) {
record = d;
closest = pt;
}
}
}
if (closest) {
vertex(closest.x, closest.y);
}
}
endShape();
}
show() {
fill(255);
beginShape();
for (let ray of this.rays) {
ray.show();
}
}
}