Skip to content

Commit 1685c9a

Browse files
committed
feat: Add Stats destruction method
1 parent b811f89 commit 1685c9a

File tree

7 files changed

+82
-20
lines changed

7 files changed

+82
-20
lines changed

build/proton.js

Lines changed: 18 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/proton.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

build/proton.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/emitter/destroy/testDestroy.html

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#container {
1919
width: 1003px;
2020
height: 610px;
21-
margin-top: 50px;
21+
margin-top: 100px;
2222
margin-left: -501px;
2323
left: 50%;
2424
position: absolute;
@@ -43,7 +43,7 @@
4343
<script src="../../lib/jquery-1.9.1.min.js"></script>
4444
<script src="//localhost:3001/build/proton.js"></script>
4545
<script>
46-
var proton;
46+
var proton, emitter1, emitter2;
4747
var renderer;
4848

4949
main();
@@ -59,42 +59,67 @@
5959
}
6060

6161
function addBtn() {
62-
var btn = $("<div>Click Reset</div>").css({
63-
width: "100px",
62+
var baseCss = {
63+
width: "150px",
6464
height: "30px",
65-
top: "10px",
66-
right: "10px",
6765
color: "#fff",
6866
background: "#004cf8",
6967
padding: "5px",
68+
textAlign: "center",
69+
lineHeight: "30px",
70+
borderRadius: "8px",
7071
cursor: "pointer",
7172
position: "absolute"
73+
};
74+
75+
var btn1 = $("<div>Destroy Proton</div>").css({
76+
top: "10px",
77+
right: "10px",
78+
...baseCss
79+
});
80+
81+
var btn2 = $("<div>Destroy Emitter</div>").css({
82+
top: "10px",
83+
right: "200px",
84+
...baseCss
7285
});
7386

74-
var inited = true;
75-
btn.on("click", function() {
76-
if (inited) {
77-
inited = false;
87+
var inited1 = true;
88+
btn1.on("click", function() {
89+
if (inited1) {
7890
destroy();
7991
} else {
80-
inited = true;
8192
createProton();
8293
}
94+
inited1 = !inited1;
95+
});
96+
97+
var inited2 = true;
98+
btn2.on("click", function() {
99+
if (inited2) {
100+
emitter1.stop();
101+
} else {
102+
emitter1.emit();
103+
}
104+
inited2 = !inited2;
83105
});
84106

85-
$("body").append(btn);
107+
$("body").append(btn1);
108+
$("body").append(btn2);
86109
}
87110

88111
function destroy() {
89112
if (!proton) return;
90113
proton.destroy(true);
114+
console.log(proton);
115+
console.log(renderer);
91116
proton = null;
92117
}
93118

94119
function createProton() {
95120
proton = new Proton();
96-
createImageEmitter();
97-
createColorEmitter();
121+
emitter1 = createImageEmitter();
122+
emitter2 = createColorEmitter();
98123

99124
renderer = new Proton.CanvasRenderer(canvas);
100125
proton.addRenderer(renderer);
@@ -145,7 +170,10 @@
145170

146171
function tick() {
147172
requestAnimationFrame(tick);
148-
proton && proton.update();
173+
if (proton) {
174+
proton.update();
175+
proton.stats.update(1);
176+
}
149177
}
150178
</script>
151179
</body>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "proton-engine",
3-
"version": "5.2.6",
3+
"version": "5.2.7",
44
"description": "Proton is a simple and powerful javascript particle animation engine.",
55
"keywords": [
66
"particle",

src/core/Proton.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,16 @@ export default class Proton {
235235
this.time = 0;
236236
this.then = 0;
237237
this.pool.destroy();
238+
this.stats.destroy();
238239

239240
Util.destroyAll(this.emitters);
240241
Util.destroyAll(this.renderers, this.getAllParticles());
242+
243+
this.integrator = null;
244+
this.renderers = null;
245+
this.emitters = null;
246+
this.stats = null;
247+
this.pool = null;
241248
};
242249

243250
if (remove) {

src/debug/Stats.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,14 @@ export default class Stats {
117117
getEmitterPos(e) {
118118
return Math.round(e.p.x) + "," + Math.round(e.p.y);
119119
}
120+
121+
destroy() {
122+
if (this.container && this.container.parentNode) {
123+
const body = this.body || document.body;
124+
body.removeChild(this.container);
125+
}
126+
127+
this.proton = null;
128+
this.container = null;
129+
}
120130
}

0 commit comments

Comments
 (0)