-
Notifications
You must be signed in to change notification settings - Fork 0
/
Letter.js
76 lines (66 loc) · 2.35 KB
/
Letter.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
function Letter(letter, x, y, z, depth, size, resolution, bevelled = true, font = "Georgia", style = BOLD){
this.letter = letter; // Letter
this.pos = createVector(x, y, z); // Position
this.depth = depth; // Depth in the z axis
this.size = size; // Size that each "pixel" (cube) is
this.res = resolution; // Number of cubes per character (higher is more detailed)
this.bevelled = bevelled; // Outer two z-layers are smaller to give a 3D effect
this.font = font;
this.style = style;
this.create = function() {
// Create the 2D graphic
var test = createGraphics(this.res, this.res);
var array = [];
// Draw the given character in the centre
test.textAlign(CENTER, CENTER);
test.textSize(this.res * 6 / 5)
test.textFont(font);
test.textStyle(style);
test.background(255);
test.text(this.letter, test.width / 2, test.height / 2);
// Put all of the non-white pixels in an array as 1s
test.loadPixels()
for (var x = 0; x < test.width; x++) {
array.push([]);
for (var y = 0; y < test.height; y++) {
if (test.get(x, y)[0] != 255) {
array[x].push(1);
} else {
array[x].push(0);
}
}
}
return array;
}
this.array = this.create();
this.show = function() {
for (var z = -depth; z < depth; z++) {
for (var x = 0; x < this.res; x++) {
for (var y = 0; y < this.res; y++) {
push();
// Move to the cubes to their place
translate(this.pos.x + (x - this.res / 2) * this.size, this.pos.y + (y - this.res / 2) * this.size, this.pos.z + z * this.size);
if (this.array[x][y] == 1) {
if (!this.bevelled) {
box(this.size, this.size, this.size);
} else {
// Checks to see if these points are "surrounded", and if so draw them, giving the inner part to create the 3D effect
var inner = (x > 0 && y > 0 && x < this.res - 1 && y < this.res - 1);
var surrounded = (inner && this.array[x - 1][y] && this.array[x + 1][y] && this.array[x][y - 1] && this.array[x][y + 1]);
var edge = (z == -depth || z == depth - 1);
// Draw the outer layers that are smaller
if (surrounded && edge) {
box(this.size, this.size, this.size);
}
// Draw only the outer part since the inner part is obscured by the inner layer
if (!surrounded && !edge) {
box(this.size, this.size, this.size);
}
}
}
pop();
}
}
}
}
}