Skip to content

Commit 628a05d

Browse files
committed
fix: fixed issue in Firefox that caused the canvas to expand each frame
1 parent 20df6f9 commit 628a05d

2 files changed

Lines changed: 66 additions & 19 deletions

File tree

demo/vite/src/main.ts

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,45 @@ import configs from "@tsparticles/configs";
66
(async (engine: Engine) => {
77
await loadAll(engine);
88

9-
const keys = Object.keys(configs),
10-
randomKey = keys[Math.floor(Math.random() * keys.length)] as keyof typeof configs;
9+
/*const keys = Object.keys(configs),
10+
randomKey = keys[Math.floor(Math.random() * keys.length)] as keyof typeof configs;*/
1111

1212
await engine.load({
1313
id: "tsparticles",
14-
options: configs[randomKey]
14+
options: {
15+
...configs.basic,
16+
fullScreen: {
17+
...configs.basic.fullScreen,
18+
enable: false
19+
},
20+
interactivity: {
21+
...configs.basic.interactivity,
22+
events: {
23+
...configs.basic.interactivity.events,
24+
onClick: {
25+
...configs.basic.interactivity.events.onClick,
26+
enable: false,
27+
},
28+
onHover: {
29+
...configs.basic.interactivity.events.onHover,
30+
enable: false,
31+
}
32+
}
33+
},
34+
particles: {
35+
...configs.basic.particles,
36+
number: {
37+
...configs.basic.particles.number,
38+
density: {
39+
...configs.basic.particles.number.density,
40+
enable: false,
41+
}
42+
},
43+
links: {
44+
...configs.basic.particles.links,
45+
enable: false
46+
}
47+
}
48+
}
1549
});
1650
})(tsParticles);

engine/src/Core/Canvas.ts

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,27 @@ export class Canvas {
9090
private _postDrawUpdaters: IParticleUpdater[];
9191
private _preDrawUpdaters: IParticleUpdater[];
9292
private _resizePlugins: IContainerPlugin[];
93+
private readonly _standardSize: IDimension;
9394
private _trailFill?: ITrailFillData;
9495

9596
/**
9697
* Constructor of canvas manager
9798
* @param container - the parent container
9899
*/
99100
constructor(private readonly container: Container) {
100-
this.size = {
101+
this._standardSize = {
101102
height: 0,
102103
width: 0,
103104
};
104105

106+
const pxRatio = container.retina.pixelRatio,
107+
stdSize = this._standardSize;
108+
109+
this.size = {
110+
height: stdSize.height * pxRatio,
111+
width: stdSize.width * pxRatio,
112+
};
113+
105114
this._context = null;
106115
this._generated = false;
107116
this._preDrawUpdaters = [];
@@ -395,8 +404,14 @@ export class Canvas {
395404
this.element = canvas;
396405
this.element.ariaHidden = "true";
397406
this._originalStyle = deepExtend({}, this.element.style) as Record<string, string | null>;
398-
this.size.height = canvas.offsetHeight;
399-
this.size.width = canvas.offsetWidth;
407+
this._standardSize.height = canvas.offsetHeight;
408+
this._standardSize.width = canvas.offsetWidth;
409+
410+
const pxRatio = this.container.retina.pixelRatio;
411+
412+
this.size.height = this._standardSize.height * pxRatio;
413+
this.size.width = this._standardSize.width * pxRatio;
414+
400415
this._context = this.element.getContext("2d");
401416

402417
this._safeMutationObserver(obs => {
@@ -444,26 +459,24 @@ export class Canvas {
444459
}
445460

446461
const container = this.container,
447-
pxRatio = container.retina.pixelRatio,
448-
size = container.canvas.size,
462+
size = container.canvas._standardSize,
449463
newSize = {
450-
width: this.element.offsetWidth * pxRatio,
451-
height: this.element.offsetHeight * pxRatio,
464+
width: this.element.offsetWidth,
465+
height: this.element.offsetHeight,
452466
};
453467

454-
if (
455-
newSize.height === size.height &&
456-
newSize.width === size.width &&
457-
newSize.height === this.element.height &&
458-
newSize.width === this.element.width
459-
) {
468+
if (newSize.height === size.height && newSize.width === size.width) {
460469
return false;
461470
}
462471

463-
const oldSize = { ...size };
472+
const oldSize = { ...size },
473+
pxRatio = container.retina.pixelRatio;
474+
475+
this._standardSize.height = newSize.height;
476+
this._standardSize.width = newSize.width;
464477

465-
this.element.width = size.width = this.element.offsetWidth * pxRatio;
466-
this.element.height = size.height = this.element.offsetHeight * pxRatio;
478+
this.element.width = this.size.width = this._standardSize.width * pxRatio;
479+
this.element.height = this.size.height = this._standardSize.height * pxRatio;
467480

468481
if (this.container.started) {
469482
container.particles.setResizeFactor({

0 commit comments

Comments
 (0)