-
Notifications
You must be signed in to change notification settings - Fork 0
/
Particle.js
190 lines (155 loc) · 4.77 KB
/
Particle.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import Image from "../Image.js";
import GameObject from "../../objects/GameObject.js";
export default class Particle extends GameObject {
static drawQueue = {};
static partSheets = [];
z = 1000;
constructor(engine, options = {start:{}}) {
super(engine, {x: 50, y: 50, radius: 50});
this.part = generateParticle();
this.ctx = this.part.img.getContext("2d");
this.ctx.globalCompositeOperation = "source-atop";
this.time = 0;
this.lifeSpan = options.lifeSpan ?? 1;
this.optimizeColorTransitions = options.optimizeColorTransitions ?? true;
this.initial = options.start;
this._setState(this.initial);
this.stateDelta = {};
for(var key in options.start) {
if ( typeof options.start[key] === "number" && typeof options.end?.[key] === "number" ) {
if ( this.optimizeColorTransitions && ['r', 'g', 'b'].includes(key)) {
this._normalizeColor(options, key);
}
this.stateDelta[key] = options.end[key] - options.start[key];
}
}
}
update(ctx) {
this.time += 1/60;
if ( this.time > this.lifeSpan ) {
this.engine.unregister(this);
}
this._setState(this._generateDeltaState(this.time / this.lifeSpan));
}
draw(ctx) {
Particle._queueForDraw(this);
}
get r() {
return this._r;
}
set r(val) {
this._r = Math.floor(val);
this._changeColor(this.r, this.g, this.b);
}
get g() {
return this._g;
}
set g(val) {
this._g = Math.floor(val);
this._changeColor(this.r, this.g, this.b);
}
get b() {
return this._b;
}
set b(val) {
this._b = Math.floor(val);
this._changeColor(this.r, this.g, this.b);
}
_changeColor() {
this.col = "rgb("+this.r+","+this.g+","+this.b+")";
}
_setState(state) {
for ( var key in state ) {
this[key] = state[key];
}
}
_generateDeltaState(delta) {
var newDeltaState = {};
for ( var key in this.stateDelta ) {
newDeltaState[key] = this.initial[key] + this.stateDelta[key] * delta;
}
return newDeltaState;
}
_normalizeColor(options, color) {
if ( options.start ) {
[options.start, options.end].forEach(state => {
if ( state?.hasOwnProperty(color) ) {
state[color] = Math.round(state[color]/16)*16;
}
});
}
}
static _queueForDraw(particle) {
this.drawQueue[particle.col] = this.drawQueue[particle.col] ?? [];
this.drawQueue[particle.col].push(particle);
}
static drawQueuedParticles(ctx) {
var x = 0, y = 0, sheet = 0;
var particleSegments = [];
if ( Particle.partSheets.length === 0 ) {
var can = generateParticleSheet();
Particle.partSheets.push({can, ctx: can.getContext("2d")});
}
for ( var key in this.drawQueue ) {
var particleList = this.drawQueue[key];
if ( sheet >= Particle.partSheets.length ) {
var can = generateParticleSheet();
Particle.partSheets.push({can, ctx: can.getContext("2d")});
}
var sheetCtx = Particle.partSheets[sheet].ctx;
sheetCtx.fillStyle = key;
sheetCtx.fillRect(x, y, 50, 50);
particleList.forEach(particle => particleSegments.push({particle, sheet, x, y}));
x += 50;
if ( x >= 1000 ) {
x = 0;
y += 50;
if ( y >= 1000 ) {
y = 0;
sheet++;
}
}
};
// console.log("Drawing " + Object.keys(this.drawQueue).length + "/" + particleSegments.length + " particles.");
particleSegments.forEach(seg => {
var { x: px, y: py, w: pw, h: ph } = seg.particle.rect;
ctx.globalAlpha = seg.particle.alpha;
ctx.drawImage(Particle.partSheets[seg.sheet].can, seg.x, seg.y, 50, 50, px, py, pw, ph);
});
this.drawQueue = {};
}
}
function generateParticle(size = 50) {
if ( generateParticle.particle ) {
return generateParticle.particle;
}
var can = document.createElement("canvas");
can.width = can.height = size;
var ctx = can.getContext("2d");
var iData = ctx.getImageData(0, 0, size, size);
var data = iData.data;
var i = 0, center = size / 2;
for ( var y = 0; y < size; y++ ) {
for ( var x = 0; x < size; x++ ) {
var dist = Math.sqrt(Math.pow(x-center, 2) + Math.pow(y-center, 2));
data[i] = data[i + 1] = data[i + 2] = 255;
data[i + 3] = Math.max((center - dist) / center, 0) * 255;
i += 4;
}
}
ctx.putImageData(iData, 0, 0);
return generateParticle.particle = new Image(can);
}
function generateParticleSheet() {
var part = generateParticle(50);
var sheet = document.createElement("canvas");
sheet.width = sheet.height = 1000;
var ctx = sheet.getContext("2d");
for ( var y = 0; y < 1000; y += 50 ) {
for ( var x = 0; x < 1000; x += 50 ) {
part.draw(ctx, x, y);
}
}
ctx.globalCompositeOperation = "source-atop";
return sheet;
}