-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
155 lines (129 loc) · 4.81 KB
/
script.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
class Area {
constructor(element, width, height, fontSize=100) {
this.element = element;
this.context = element.getContext('2d')
this.element.width = width;
this.element.height = height;
this.fontSize = fontSize
this.context.font = fontSize + 'px Helvetica'
}
addText(text){
this.context.fillStyle = "red";
this.context.textAlign = "center";
this.context.fillText(text, this.element.width/2, this.element.height/2)
}
}
class GridSet {
constructor(area, cellSize, width=1, gridStyle="grid") {
this.area = area;
this.zoom = 1;
this.cellSize = cellSize / this.zoom;
this.cols = Math.ceil(area.element.width / this.cellSize)
this.rows = Math.ceil(area.element.height / this.cellSize)
this.width = width
this.gridStyle = gridStyle
}
render() {
this.area.context.strokeStyle = "white"
this.area.context.fillStyle = "white"
this.area.context.lineWidth = this.width
this.area.context.clearRect(0,0, this.area.element.width, this.area.element.height)
if (this.gridStyle === "grid") {
for (let x = 0; x < this.cols; x++) {
this.area.context.beginPath()
this.area.context.moveTo(this.cellSize * x, 0)
this.area.context.lineTo(this.cellSize * x, this.area.element.height)
this.area.context.stroke();
}
for (let y = 0; y < this.rows; y++) {
this.area.context.beginPath()
this.area.context.moveTo(0, this.cellSize * y)
this.area.context.lineTo(this.area.element.width, this.cellSize * y)
this.area.context.stroke();
}
} else if (this.gridStyle === "point") {
for (let x = 0; x < this.cols; x++) {
for (let y = 0; y < this.rows; y++) {
this.area.context.fillRect(x*this.cellSize, y*this.cellSize, this.width, this.width)
}
}
}
}
setZoom(zoom){
this.zoom = zoom;
this.cellSize = this.zoom < 0 ? this.cellSize / (this.zoom * -1) : this.cellSize * this.zoom;
this.cols = Math.ceil(this.area.element.width / this.cellSize)
this.rows = Math.ceil(this.area.element.height / this.cellSize)
const prevFont = this.area.context.font.split(' ');
const prevFontSize = prevFont[0].replace('px', '');
const newFontSize = Number(prevFontSize) + (10 * this.zoom);
this.area.context.font = newFontSize + 'px ' + prevFont[1];
this.render()
}
getParticlesAlpha(scale=0.03) {
const currCellSize = Math.floor(this.area.fontSize * scale);
const particles = []
const pixels = this.area.context.getImageData(0, 0, this.area.element.width, this.area.element.height).data;
for (let y = 0; y < this.area.element.height; y+=currCellSize) {
for (let x = 0; x < this.area.element.width; x+=currCellSize) {
const pixelIndex = (y * this.area.element.width + x) * 4;
if (pixels[pixelIndex + 3] > 0) {
// alpha
particles.push(
new Particle(new Point(x, y, this), "blue", currCellSize)
)
}
}
}
return particles
}
}
class Point {
constructor(x, y, gridSet) {
this.x = x
this.y = y
this.treeX = Math.ceil(gridSet.cellSize / x)
this.treeY = Math.ceil(gridSet.cellSize / y)
this.gridSet = gridSet
}
move(toPoint) {
this.x = toPoint.x
this.y = toPoint.y
this.treeX = toPoint.treeX
this.treeY = toPoint.treeY
}
}
class Particle {
constructor(point, color, size) {
this.point = point;
this.originX = new Point(0, 0, this.point.gridSet);
this.originY = new Point(0, 0, this.point.gridSet);
this.color = color;
this.size = size
}
render() {
this.point.gridSet.area.context.fillStyle = this.color;
this.point.gridSet.area.context.fillRect(this.point.x, this.point.y, this.size, this.size)
}
moving() {
let x, y;
x += (this.originX - this.point.x)
y += (this.originY - this.point.y)
this.point.move(new Point(x, y, this.point.gridSet))
}
}
const area = new Area(
document.getElementById('particle-area'),
globalThis.window.innerWidth,
globalThis.window.innerHeight
);
const gridSet = new GridSet(area, 256, 1, "point");
gridSet.render()
gridSet.setZoom(-2)
area.addText("Blue particles")
const particles = gridSet.getParticlesAlpha();
// remove particles.forEach for only text displayed
particles.forEach(element => {
// element.moving()
element.render()
});