@tsparticles/interaction-external-cannon 4.0.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/49.min.js +1 -0
- package/LICENSE +21 -0
- package/README.md +88 -0
- package/browser/Cannoner.js +126 -0
- package/browser/Options/Classes/Cannon.js +46 -0
- package/browser/Options/Classes/CannonOptions.js +1 -0
- package/browser/Options/Interfaces/ICannon.js +1 -0
- package/browser/Types.js +1 -0
- package/browser/index.js +12 -0
- package/browser/package.json +1 -0
- package/cjs/Cannoner.js +126 -0
- package/cjs/Options/Classes/Cannon.js +46 -0
- package/cjs/Options/Classes/CannonOptions.js +1 -0
- package/cjs/Options/Interfaces/ICannon.js +1 -0
- package/cjs/Types.js +1 -0
- package/cjs/index.js +12 -0
- package/cjs/package.json +1 -0
- package/dist_browser_Cannoner_js.js +30 -0
- package/esm/Cannoner.js +126 -0
- package/esm/Options/Classes/Cannon.js +46 -0
- package/esm/Options/Classes/CannonOptions.js +1 -0
- package/esm/Options/Interfaces/ICannon.js +1 -0
- package/esm/Types.js +1 -0
- package/esm/index.js +12 -0
- package/esm/package.json +1 -0
- package/package.json +97 -0
- package/report.html +95 -0
- package/tsparticles.interaction.external.cannon.js +367 -0
- package/tsparticles.interaction.external.cannon.min.js +2 -0
- package/types/Cannoner.d.ts +19 -0
- package/types/Options/Classes/Cannon.d.ts +14 -0
- package/types/Options/Classes/CannonOptions.d.ts +7 -0
- package/types/Options/Interfaces/ICannon.d.ts +10 -0
- package/types/Types.d.ts +13 -0
- package/types/index.d.ts +5 -0
- package/umd/Cannoner.js +140 -0
- package/umd/Options/Classes/Cannon.js +60 -0
- package/umd/Options/Classes/CannonOptions.js +12 -0
- package/umd/Options/Interfaces/ICannon.js +12 -0
- package/umd/Types.js +12 -0
- package/umd/index.js +61 -0
package/49.min.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";(this.webpackChunk_tsparticles_interaction_external_cannon=this.webpackChunk_tsparticles_interaction_external_cannon||[]).push([[49],{49(t,e,i){i.d(e,{Cannoner:()=>d});var a,n,r=i(702),o=i(303),s=i(512);(a=n||(n={}))[a.idle=0]="idle",a[a.aiming=1]="aiming",a[a.fired=2]="fired";let c="cannon",l=o.identity/20;class d extends r.ExternalInteractorBase{maxDistance=0;_data;_gesture={origin:o.Vector.origin,current:o.Vector.origin,active:!1};_lastDownPosition=void 0;_state=n.idle;constructor(t){super(t)}clear(t,e){}init(){let t=this.container.actualOptions.interactivity?.modes.cannon??new s.a;this._data={spread:(0,o.degToRad)(t.spread),maxDragDistance:t.maxDragDistance,velocityFactor:t.velocityFactor,particleFactor:t.particleFactor,minParticles:t.minParticles,maxParticles:t.maxParticles,drawVector:t.drawVector,vectorColor:t.vectorColor}}interact(t,e){let i=t.mouse,a=i.position,r=i.clicking,o=i.downPosition;r&&o&&o!==this._lastDownPosition&&this._state===n.idle&&(this._lastDownPosition=o,this._gesture={origin:{x:o.x,y:o.y},current:{x:o.x,y:o.y},active:!0},this._state=n.aiming),this._state===n.aiming&&a&&(this._gesture.current={x:a.x,y:a.y},this._drawVector()),r||this._state!==n.aiming||(this._gesture.active=!1,this._fire(),this._state=n.idle)}isEnabled(t){let{container:e}=this,i=e.actualOptions.interactivity?.events;if(!i?.onClick.enable)return!1;let a=i.onClick.mode;return!!(Array.isArray(a)?a.includes(c):a===c)&&(this._state!==n.idle||t.mouse.clicking)}loadModeOptions(t,...e){for(let i of(t.cannon??=new s.a,e))t.cannon.load(i?.cannon)}reset(t,e){}_drawVector(){this.container.canvas.render.draw(t=>{let e=this._data;if(!e)return;let{origin:i,current:a}=this._gesture,n=this.container.retina.pixelRatio,r=(0,o.getDistance)(i,a),s=e.maxDragDistance>o.none?Math.min(r,e.maxDragDistance*n):n,c=r>0?s/r:0,d=i.x+(a.x-i.x)*c,h=i.y+(a.y-i.y)*c;t.save(),t.strokeStyle=e.vectorColor,t.lineWidth=o.double*n,t.beginPath(),t.moveTo(i.x,i.y),t.lineTo(d,h),t.stroke();let g=Math.max(4,s*l)*n;t.beginPath(),t.arc(i.x,i.y,g,0,o.doublePI),t.strokeStyle=e.vectorColor,t.lineWidth=o.double*n,t.stroke(),t.restore()})}_fire(){var t,e,i;let a=this._data;if(!a)return;let{origin:n,current:r}=this._gesture,s=this.container.retina.pixelRatio,c=(0,o.getDistance)(n,r),l=a.maxDragDistance>o.none?Math.min(c,a.maxDragDistance*s):c;if(l<2)return;let d=o.identity/s,h=(t=r.x,e=r.y,i=n.x,Math.atan2(n.y-e,i-t)),g=l*d*a.velocityFactor,_=Math.min(a.maxParticles,Math.max(a.minParticles,Math.round(l*a.particleFactor))),m=180/Math.PI;for(let t=0;t<_;t++){let t=h+(0,o.getRandomInRange)(-a.spread,a.spread),e=(0,o.getRandomInRange)(.25*g,g);this.container.particles.addParticle(n,{move:{enable:!0,speed:e,direction:t*m}})}}}}}]);
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Matteo Bruni
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
[](https://particles.js.org)
|
|
2
|
+
|
|
3
|
+
# tsParticles External Cannon Interaction
|
|
4
|
+
|
|
5
|
+
[](https://www.jsdelivr.com/package/npm/@tsparticles/interaction-external-cannon)
|
|
6
|
+
[](https://www.npmjs.com/package/@tsparticles/interaction-external-cannon)
|
|
7
|
+
[](https://www.npmjs.com/package/@tsparticles/interaction-external-cannon) [](https://github.com/sponsors/matteobruni)
|
|
8
|
+
|
|
9
|
+
[tsParticles](https://github.com/tsparticles/tsparticles) interaction plugin for a particles cannon
|
|
10
|
+
with the mouse/pointer.
|
|
11
|
+
|
|
12
|
+
## How to use it
|
|
13
|
+
|
|
14
|
+
### CDN / Vanilla JS / jQuery
|
|
15
|
+
|
|
16
|
+
The CDN/Vanilla version JS has one required file in vanilla configuration:
|
|
17
|
+
|
|
18
|
+
Including the `tsparticles.interaction.external.cannon.min.js` file will export the function to load the interaction
|
|
19
|
+
plugin:
|
|
20
|
+
|
|
21
|
+
```javascript
|
|
22
|
+
loadExternalCannonInteraction;
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
### Usage
|
|
26
|
+
|
|
27
|
+
Once the scripts are loaded you can set up `tsParticles` and the interaction plugin like this:
|
|
28
|
+
|
|
29
|
+
```javascript
|
|
30
|
+
(async () => {
|
|
31
|
+
await loadInteractivityPlugin(tsParticles);
|
|
32
|
+
await loadExternalCannonInteraction(tsParticles);
|
|
33
|
+
|
|
34
|
+
await tsParticles.load({
|
|
35
|
+
id: "tsparticles",
|
|
36
|
+
options: {
|
|
37
|
+
interactivity: {
|
|
38
|
+
events: {
|
|
39
|
+
onClick: {
|
|
40
|
+
enable: true,
|
|
41
|
+
mode: "cannon",
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
})();
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### ESM / CommonJS
|
|
51
|
+
|
|
52
|
+
This package is compatible also with ES or CommonJS modules, firstly this needs to be installed, like this:
|
|
53
|
+
|
|
54
|
+
```shell
|
|
55
|
+
$ npm install @tsparticles/interaction-external-cannon
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
or
|
|
59
|
+
|
|
60
|
+
```shell
|
|
61
|
+
$ yarn add @tsparticles/interaction-external-cannon
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Then you need to import it in the app, like this:
|
|
65
|
+
|
|
66
|
+
```javascript
|
|
67
|
+
const { tsParticles } = require("@tsparticles/engine");
|
|
68
|
+
const { loadInteractivityPlugin } = require("@tsparticles/plugin-interactivity");
|
|
69
|
+
const { loadExternalCannonInteraction } = require("@tsparticles/interaction-external-cannon");
|
|
70
|
+
|
|
71
|
+
(async () => {
|
|
72
|
+
await loadInteractivityPlugin(tsParticles);
|
|
73
|
+
await loadExternalCannonInteraction(tsParticles);
|
|
74
|
+
})();
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
or
|
|
78
|
+
|
|
79
|
+
```javascript
|
|
80
|
+
import { tsParticles } from "@tsparticles/engine";
|
|
81
|
+
import { loadInteractivityPlugin } from "@tsparticles/plugin-interactivity";
|
|
82
|
+
import { loadExternalCannonInteraction } from "@tsparticles/interaction-external-cannon";
|
|
83
|
+
|
|
84
|
+
(async () => {
|
|
85
|
+
await loadInteractivityPlugin(tsParticles);
|
|
86
|
+
await loadExternalCannonInteraction(tsParticles);
|
|
87
|
+
})();
|
|
88
|
+
```
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { ExternalInteractorBase, } from "@tsparticles/plugin-interactivity";
|
|
2
|
+
import { Vector, degToRad, double, doublePI, getDistance, getRandomInRange, identity, none, } from "@tsparticles/engine";
|
|
3
|
+
import { Cannon } from "./Options/Classes/Cannon.js";
|
|
4
|
+
var CannonState;
|
|
5
|
+
(function (CannonState) {
|
|
6
|
+
CannonState[CannonState["idle"] = 0] = "idle";
|
|
7
|
+
CannonState[CannonState["aiming"] = 1] = "aiming";
|
|
8
|
+
CannonState[CannonState["fired"] = 2] = "fired";
|
|
9
|
+
})(CannonState || (CannonState = {}));
|
|
10
|
+
const cannonMode = "cannon", minAngle = 0, powerRadiusMin = 4, powerRadiusMaxDenominator = 20, powerRadiusMaxFactor = identity / powerRadiusMaxDenominator, minTapsLength = 2, piDeg = 180, quarter = 0.25, minDistance = 0;
|
|
11
|
+
function angleRad(x0, y0, x1, y1) {
|
|
12
|
+
return Math.atan2(y1 - y0, x1 - x0);
|
|
13
|
+
}
|
|
14
|
+
export class Cannoner extends ExternalInteractorBase {
|
|
15
|
+
maxDistance = 0;
|
|
16
|
+
_data;
|
|
17
|
+
_gesture = {
|
|
18
|
+
origin: Vector.origin,
|
|
19
|
+
current: Vector.origin,
|
|
20
|
+
active: false,
|
|
21
|
+
};
|
|
22
|
+
_lastDownPosition = undefined;
|
|
23
|
+
_state = CannonState.idle;
|
|
24
|
+
constructor(container) {
|
|
25
|
+
super(container);
|
|
26
|
+
}
|
|
27
|
+
clear(_particle, _delta) {
|
|
28
|
+
}
|
|
29
|
+
init() {
|
|
30
|
+
const options = this.container.actualOptions.interactivity?.modes.cannon ?? new Cannon();
|
|
31
|
+
this._data = {
|
|
32
|
+
spread: degToRad(options.spread),
|
|
33
|
+
maxDragDistance: options.maxDragDistance,
|
|
34
|
+
velocityFactor: options.velocityFactor,
|
|
35
|
+
particleFactor: options.particleFactor,
|
|
36
|
+
minParticles: options.minParticles,
|
|
37
|
+
maxParticles: options.maxParticles,
|
|
38
|
+
drawVector: options.drawVector,
|
|
39
|
+
vectorColor: options.vectorColor,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
interact(interactivityData, _delta) {
|
|
43
|
+
const mouse = interactivityData.mouse, mousePos = mouse.position, clicking = mouse.clicking, downPos = mouse.downPosition;
|
|
44
|
+
if (clicking && downPos && downPos !== this._lastDownPosition && this._state === CannonState.idle) {
|
|
45
|
+
this._lastDownPosition = downPos;
|
|
46
|
+
this._gesture = {
|
|
47
|
+
origin: { x: downPos.x, y: downPos.y },
|
|
48
|
+
current: { x: downPos.x, y: downPos.y },
|
|
49
|
+
active: true,
|
|
50
|
+
};
|
|
51
|
+
this._state = CannonState.aiming;
|
|
52
|
+
}
|
|
53
|
+
if (this._state === CannonState.aiming && mousePos) {
|
|
54
|
+
this._gesture.current = { x: mousePos.x, y: mousePos.y };
|
|
55
|
+
this._drawVector();
|
|
56
|
+
}
|
|
57
|
+
if (!clicking && this._state === CannonState.aiming) {
|
|
58
|
+
this._gesture.active = false;
|
|
59
|
+
this._fire();
|
|
60
|
+
this._state = CannonState.idle;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
isEnabled(interactivityData) {
|
|
64
|
+
const { container } = this, events = container.actualOptions.interactivity?.events;
|
|
65
|
+
if (!events?.onClick.enable) {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
const modes = events.onClick.mode, modeEnabled = Array.isArray(modes) ? modes.includes(cannonMode) : modes === cannonMode;
|
|
69
|
+
if (!modeEnabled) {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
return this._state !== CannonState.idle || interactivityData.mouse.clicking;
|
|
73
|
+
}
|
|
74
|
+
loadModeOptions(options, ...sources) {
|
|
75
|
+
options.cannon ??= new Cannon();
|
|
76
|
+
for (const source of sources) {
|
|
77
|
+
options.cannon.load(source?.cannon);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
reset(_interactivityData, _particle) {
|
|
81
|
+
}
|
|
82
|
+
_drawVector() {
|
|
83
|
+
this.container.canvas.render.draw(ctx => {
|
|
84
|
+
const opts = this._data;
|
|
85
|
+
if (!opts) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
const { origin, current } = this._gesture, pxRatio = this.container.retina.pixelRatio, dragDist = getDistance(origin, current), clampedDist = opts.maxDragDistance > none ? Math.min(dragDist, opts.maxDragDistance * pxRatio) : pxRatio, clampRatio = dragDist > minDistance ? clampedDist / dragDist : minDistance, clampedX = origin.x + (current.x - origin.x) * clampRatio, clampedY = origin.y + (current.y - origin.y) * clampRatio;
|
|
89
|
+
ctx.save();
|
|
90
|
+
ctx.strokeStyle = opts.vectorColor;
|
|
91
|
+
ctx.lineWidth = double * pxRatio;
|
|
92
|
+
ctx.beginPath();
|
|
93
|
+
ctx.moveTo(origin.x, origin.y);
|
|
94
|
+
ctx.lineTo(clampedX, clampedY);
|
|
95
|
+
ctx.stroke();
|
|
96
|
+
const radius = Math.max(powerRadiusMin, clampedDist * powerRadiusMaxFactor) * pxRatio;
|
|
97
|
+
ctx.beginPath();
|
|
98
|
+
ctx.arc(origin.x, origin.y, radius, minAngle, doublePI);
|
|
99
|
+
ctx.strokeStyle = opts.vectorColor;
|
|
100
|
+
ctx.lineWidth = double * pxRatio;
|
|
101
|
+
ctx.stroke();
|
|
102
|
+
ctx.restore();
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
_fire() {
|
|
106
|
+
const opts = this._data;
|
|
107
|
+
if (!opts) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
const { origin, current } = this._gesture, pxRatio = this.container.retina.pixelRatio, dist = getDistance(origin, current), dragLength = opts.maxDragDistance > none ? Math.min(dist, opts.maxDragDistance * pxRatio) : dist;
|
|
111
|
+
if (dragLength < minTapsLength) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
const pxRatioFactor = identity / pxRatio, launchAngle = angleRad(current.x, current.y, origin.x, origin.y), velocity = dragLength * pxRatioFactor * opts.velocityFactor, count = Math.min(opts.maxParticles, Math.max(opts.minParticles, Math.round(dragLength * opts.particleFactor))), toDeg = piDeg / Math.PI;
|
|
115
|
+
for (let i = 0; i < count; i++) {
|
|
116
|
+
const spreadAngle = launchAngle + getRandomInRange(-opts.spread, opts.spread), speed = getRandomInRange(velocity * quarter, velocity);
|
|
117
|
+
this.container.particles.addParticle(origin, {
|
|
118
|
+
move: {
|
|
119
|
+
enable: true,
|
|
120
|
+
speed,
|
|
121
|
+
direction: spreadAngle * toDeg,
|
|
122
|
+
},
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
export class Cannon {
|
|
2
|
+
drawVector;
|
|
3
|
+
maxDragDistance;
|
|
4
|
+
maxParticles;
|
|
5
|
+
minParticles;
|
|
6
|
+
particleFactor;
|
|
7
|
+
spread;
|
|
8
|
+
vectorColor;
|
|
9
|
+
velocityFactor;
|
|
10
|
+
constructor() {
|
|
11
|
+
this.spread = 30;
|
|
12
|
+
this.velocityFactor = 0.5;
|
|
13
|
+
this.particleFactor = 0.2;
|
|
14
|
+
this.maxDragDistance = 200;
|
|
15
|
+
this.minParticles = 5;
|
|
16
|
+
this.maxParticles = 200;
|
|
17
|
+
this.drawVector = true;
|
|
18
|
+
this.vectorColor = "#ffffff80";
|
|
19
|
+
}
|
|
20
|
+
load(data) {
|
|
21
|
+
if (!data) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
if (data.spread !== undefined) {
|
|
25
|
+
this.spread = data.spread;
|
|
26
|
+
}
|
|
27
|
+
if (data.velocityFactor !== undefined) {
|
|
28
|
+
this.velocityFactor = data.velocityFactor;
|
|
29
|
+
}
|
|
30
|
+
if (data.particleFactor !== undefined) {
|
|
31
|
+
this.particleFactor = data.particleFactor;
|
|
32
|
+
}
|
|
33
|
+
if (data.minParticles !== undefined) {
|
|
34
|
+
this.minParticles = data.minParticles;
|
|
35
|
+
}
|
|
36
|
+
if (data.maxParticles !== undefined) {
|
|
37
|
+
this.maxParticles = data.maxParticles;
|
|
38
|
+
}
|
|
39
|
+
if (data.drawVector !== undefined) {
|
|
40
|
+
this.drawVector = data.drawVector;
|
|
41
|
+
}
|
|
42
|
+
if (data.vectorColor !== undefined) {
|
|
43
|
+
this.vectorColor = data.vectorColor;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/browser/Types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/browser/index.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export { Cannon } from "./Options/Classes/Cannon.js";
|
|
2
|
+
export async function loadExternalCannonInteraction(engine) {
|
|
3
|
+
engine.checkVersion("4.0.0-beta.0");
|
|
4
|
+
await engine.pluginManager.register(async (e) => {
|
|
5
|
+
const { ensureInteractivityPluginLoaded } = await import("@tsparticles/plugin-interactivity");
|
|
6
|
+
ensureInteractivityPluginLoaded(e);
|
|
7
|
+
e.pluginManager.addInteractor?.("externalCannon", async (container) => {
|
|
8
|
+
const { Cannoner } = await import("./Cannoner.js");
|
|
9
|
+
return new Cannoner(container);
|
|
10
|
+
});
|
|
11
|
+
});
|
|
12
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{ "type": "module" }
|
package/cjs/Cannoner.js
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { ExternalInteractorBase, } from "@tsparticles/plugin-interactivity";
|
|
2
|
+
import { Vector, degToRad, double, doublePI, getDistance, getRandomInRange, identity, none, } from "@tsparticles/engine";
|
|
3
|
+
import { Cannon } from "./Options/Classes/Cannon.js";
|
|
4
|
+
var CannonState;
|
|
5
|
+
(function (CannonState) {
|
|
6
|
+
CannonState[CannonState["idle"] = 0] = "idle";
|
|
7
|
+
CannonState[CannonState["aiming"] = 1] = "aiming";
|
|
8
|
+
CannonState[CannonState["fired"] = 2] = "fired";
|
|
9
|
+
})(CannonState || (CannonState = {}));
|
|
10
|
+
const cannonMode = "cannon", minAngle = 0, powerRadiusMin = 4, powerRadiusMaxDenominator = 20, powerRadiusMaxFactor = identity / powerRadiusMaxDenominator, minTapsLength = 2, piDeg = 180, quarter = 0.25, minDistance = 0;
|
|
11
|
+
function angleRad(x0, y0, x1, y1) {
|
|
12
|
+
return Math.atan2(y1 - y0, x1 - x0);
|
|
13
|
+
}
|
|
14
|
+
export class Cannoner extends ExternalInteractorBase {
|
|
15
|
+
maxDistance = 0;
|
|
16
|
+
_data;
|
|
17
|
+
_gesture = {
|
|
18
|
+
origin: Vector.origin,
|
|
19
|
+
current: Vector.origin,
|
|
20
|
+
active: false,
|
|
21
|
+
};
|
|
22
|
+
_lastDownPosition = undefined;
|
|
23
|
+
_state = CannonState.idle;
|
|
24
|
+
constructor(container) {
|
|
25
|
+
super(container);
|
|
26
|
+
}
|
|
27
|
+
clear(_particle, _delta) {
|
|
28
|
+
}
|
|
29
|
+
init() {
|
|
30
|
+
const options = this.container.actualOptions.interactivity?.modes.cannon ?? new Cannon();
|
|
31
|
+
this._data = {
|
|
32
|
+
spread: degToRad(options.spread),
|
|
33
|
+
maxDragDistance: options.maxDragDistance,
|
|
34
|
+
velocityFactor: options.velocityFactor,
|
|
35
|
+
particleFactor: options.particleFactor,
|
|
36
|
+
minParticles: options.minParticles,
|
|
37
|
+
maxParticles: options.maxParticles,
|
|
38
|
+
drawVector: options.drawVector,
|
|
39
|
+
vectorColor: options.vectorColor,
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
interact(interactivityData, _delta) {
|
|
43
|
+
const mouse = interactivityData.mouse, mousePos = mouse.position, clicking = mouse.clicking, downPos = mouse.downPosition;
|
|
44
|
+
if (clicking && downPos && downPos !== this._lastDownPosition && this._state === CannonState.idle) {
|
|
45
|
+
this._lastDownPosition = downPos;
|
|
46
|
+
this._gesture = {
|
|
47
|
+
origin: { x: downPos.x, y: downPos.y },
|
|
48
|
+
current: { x: downPos.x, y: downPos.y },
|
|
49
|
+
active: true,
|
|
50
|
+
};
|
|
51
|
+
this._state = CannonState.aiming;
|
|
52
|
+
}
|
|
53
|
+
if (this._state === CannonState.aiming && mousePos) {
|
|
54
|
+
this._gesture.current = { x: mousePos.x, y: mousePos.y };
|
|
55
|
+
this._drawVector();
|
|
56
|
+
}
|
|
57
|
+
if (!clicking && this._state === CannonState.aiming) {
|
|
58
|
+
this._gesture.active = false;
|
|
59
|
+
this._fire();
|
|
60
|
+
this._state = CannonState.idle;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
isEnabled(interactivityData) {
|
|
64
|
+
const { container } = this, events = container.actualOptions.interactivity?.events;
|
|
65
|
+
if (!events?.onClick.enable) {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
const modes = events.onClick.mode, modeEnabled = Array.isArray(modes) ? modes.includes(cannonMode) : modes === cannonMode;
|
|
69
|
+
if (!modeEnabled) {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
return this._state !== CannonState.idle || interactivityData.mouse.clicking;
|
|
73
|
+
}
|
|
74
|
+
loadModeOptions(options, ...sources) {
|
|
75
|
+
options.cannon ??= new Cannon();
|
|
76
|
+
for (const source of sources) {
|
|
77
|
+
options.cannon.load(source?.cannon);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
reset(_interactivityData, _particle) {
|
|
81
|
+
}
|
|
82
|
+
_drawVector() {
|
|
83
|
+
this.container.canvas.render.draw(ctx => {
|
|
84
|
+
const opts = this._data;
|
|
85
|
+
if (!opts) {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
const { origin, current } = this._gesture, pxRatio = this.container.retina.pixelRatio, dragDist = getDistance(origin, current), clampedDist = opts.maxDragDistance > none ? Math.min(dragDist, opts.maxDragDistance * pxRatio) : pxRatio, clampRatio = dragDist > minDistance ? clampedDist / dragDist : minDistance, clampedX = origin.x + (current.x - origin.x) * clampRatio, clampedY = origin.y + (current.y - origin.y) * clampRatio;
|
|
89
|
+
ctx.save();
|
|
90
|
+
ctx.strokeStyle = opts.vectorColor;
|
|
91
|
+
ctx.lineWidth = double * pxRatio;
|
|
92
|
+
ctx.beginPath();
|
|
93
|
+
ctx.moveTo(origin.x, origin.y);
|
|
94
|
+
ctx.lineTo(clampedX, clampedY);
|
|
95
|
+
ctx.stroke();
|
|
96
|
+
const radius = Math.max(powerRadiusMin, clampedDist * powerRadiusMaxFactor) * pxRatio;
|
|
97
|
+
ctx.beginPath();
|
|
98
|
+
ctx.arc(origin.x, origin.y, radius, minAngle, doublePI);
|
|
99
|
+
ctx.strokeStyle = opts.vectorColor;
|
|
100
|
+
ctx.lineWidth = double * pxRatio;
|
|
101
|
+
ctx.stroke();
|
|
102
|
+
ctx.restore();
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
_fire() {
|
|
106
|
+
const opts = this._data;
|
|
107
|
+
if (!opts) {
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
const { origin, current } = this._gesture, pxRatio = this.container.retina.pixelRatio, dist = getDistance(origin, current), dragLength = opts.maxDragDistance > none ? Math.min(dist, opts.maxDragDistance * pxRatio) : dist;
|
|
111
|
+
if (dragLength < minTapsLength) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
const pxRatioFactor = identity / pxRatio, launchAngle = angleRad(current.x, current.y, origin.x, origin.y), velocity = dragLength * pxRatioFactor * opts.velocityFactor, count = Math.min(opts.maxParticles, Math.max(opts.minParticles, Math.round(dragLength * opts.particleFactor))), toDeg = piDeg / Math.PI;
|
|
115
|
+
for (let i = 0; i < count; i++) {
|
|
116
|
+
const spreadAngle = launchAngle + getRandomInRange(-opts.spread, opts.spread), speed = getRandomInRange(velocity * quarter, velocity);
|
|
117
|
+
this.container.particles.addParticle(origin, {
|
|
118
|
+
move: {
|
|
119
|
+
enable: true,
|
|
120
|
+
speed,
|
|
121
|
+
direction: spreadAngle * toDeg,
|
|
122
|
+
},
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
export class Cannon {
|
|
2
|
+
drawVector;
|
|
3
|
+
maxDragDistance;
|
|
4
|
+
maxParticles;
|
|
5
|
+
minParticles;
|
|
6
|
+
particleFactor;
|
|
7
|
+
spread;
|
|
8
|
+
vectorColor;
|
|
9
|
+
velocityFactor;
|
|
10
|
+
constructor() {
|
|
11
|
+
this.spread = 30;
|
|
12
|
+
this.velocityFactor = 0.5;
|
|
13
|
+
this.particleFactor = 0.2;
|
|
14
|
+
this.maxDragDistance = 200;
|
|
15
|
+
this.minParticles = 5;
|
|
16
|
+
this.maxParticles = 200;
|
|
17
|
+
this.drawVector = true;
|
|
18
|
+
this.vectorColor = "#ffffff80";
|
|
19
|
+
}
|
|
20
|
+
load(data) {
|
|
21
|
+
if (!data) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
if (data.spread !== undefined) {
|
|
25
|
+
this.spread = data.spread;
|
|
26
|
+
}
|
|
27
|
+
if (data.velocityFactor !== undefined) {
|
|
28
|
+
this.velocityFactor = data.velocityFactor;
|
|
29
|
+
}
|
|
30
|
+
if (data.particleFactor !== undefined) {
|
|
31
|
+
this.particleFactor = data.particleFactor;
|
|
32
|
+
}
|
|
33
|
+
if (data.minParticles !== undefined) {
|
|
34
|
+
this.minParticles = data.minParticles;
|
|
35
|
+
}
|
|
36
|
+
if (data.maxParticles !== undefined) {
|
|
37
|
+
this.maxParticles = data.maxParticles;
|
|
38
|
+
}
|
|
39
|
+
if (data.drawVector !== undefined) {
|
|
40
|
+
this.drawVector = data.drawVector;
|
|
41
|
+
}
|
|
42
|
+
if (data.vectorColor !== undefined) {
|
|
43
|
+
this.vectorColor = data.vectorColor;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/cjs/Types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/cjs/index.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export { Cannon } from "./Options/Classes/Cannon.js";
|
|
2
|
+
export async function loadExternalCannonInteraction(engine) {
|
|
3
|
+
engine.checkVersion("4.0.0-beta.0");
|
|
4
|
+
await engine.pluginManager.register(async (e) => {
|
|
5
|
+
const { ensureInteractivityPluginLoaded } = await import("@tsparticles/plugin-interactivity");
|
|
6
|
+
ensureInteractivityPluginLoaded(e);
|
|
7
|
+
e.pluginManager.addInteractor?.("externalCannon", async (container) => {
|
|
8
|
+
const { Cannoner } = await import("./Cannoner.js");
|
|
9
|
+
return new Cannoner(container);
|
|
10
|
+
});
|
|
11
|
+
});
|
|
12
|
+
}
|
package/cjs/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{ "type": "commonjs" }
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Author : Matteo Bruni
|
|
3
|
+
* MIT license: https://opensource.org/licenses/MIT
|
|
4
|
+
* Demo / Generator : https://particles.js.org/
|
|
5
|
+
* GitHub : https://www.github.com/matteobruni/tsparticles
|
|
6
|
+
* How to use? : Check the GitHub README
|
|
7
|
+
* v4.0.0-beta.0
|
|
8
|
+
*/
|
|
9
|
+
"use strict";
|
|
10
|
+
/*
|
|
11
|
+
* ATTENTION: The "eval" devtool has been used (maybe by default in mode: "development").
|
|
12
|
+
* This devtool is neither made for production nor for readable output files.
|
|
13
|
+
* It uses "eval()" calls to create a separate source file in the browser devtools.
|
|
14
|
+
* If you are trying to read the output file, select a different devtool (https://webpack.js.org/configuration/devtool/)
|
|
15
|
+
* or disable the default devtool with "devtool: false".
|
|
16
|
+
* If you are looking for production-ready output files, see mode: "production" (https://webpack.js.org/configuration/mode/).
|
|
17
|
+
*/
|
|
18
|
+
(this["webpackChunk_tsparticles_interaction_external_cannon"] = this["webpackChunk_tsparticles_interaction_external_cannon"] || []).push([["dist_browser_Cannoner_js"],{
|
|
19
|
+
|
|
20
|
+
/***/ "./dist/browser/Cannoner.js"
|
|
21
|
+
/*!**********************************!*\
|
|
22
|
+
!*** ./dist/browser/Cannoner.js ***!
|
|
23
|
+
\**********************************/
|
|
24
|
+
(__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) {
|
|
25
|
+
|
|
26
|
+
eval("{__webpack_require__.r(__webpack_exports__);\n/* harmony export */ __webpack_require__.d(__webpack_exports__, {\n/* harmony export */ Cannoner: () => (/* binding */ Cannoner)\n/* harmony export */ });\n/* harmony import */ var _tsparticles_plugin_interactivity__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! @tsparticles/plugin-interactivity */ \"@tsparticles/plugin-interactivity\");\n/* harmony import */ var _tsparticles_engine__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! @tsparticles/engine */ \"@tsparticles/engine\");\n/* harmony import */ var _Options_Classes_Cannon_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./Options/Classes/Cannon.js */ \"./dist/browser/Options/Classes/Cannon.js\");\n\n\n\nvar CannonState;\n(function(CannonState) {\n CannonState[CannonState[\"idle\"] = 0] = \"idle\";\n CannonState[CannonState[\"aiming\"] = 1] = \"aiming\";\n CannonState[CannonState[\"fired\"] = 2] = \"fired\";\n})(CannonState || (CannonState = {}));\nconst cannonMode = \"cannon\", minAngle = 0, powerRadiusMin = 4, powerRadiusMaxDenominator = 20, powerRadiusMaxFactor = _tsparticles_engine__WEBPACK_IMPORTED_MODULE_1__.identity / powerRadiusMaxDenominator, minTapsLength = 2, piDeg = 180, quarter = 0.25, minDistance = 0;\nfunction angleRad(x0, y0, x1, y1) {\n return Math.atan2(y1 - y0, x1 - x0);\n}\nclass Cannoner extends _tsparticles_plugin_interactivity__WEBPACK_IMPORTED_MODULE_0__.ExternalInteractorBase {\n maxDistance = 0;\n _data;\n _gesture = {\n origin: _tsparticles_engine__WEBPACK_IMPORTED_MODULE_1__.Vector.origin,\n current: _tsparticles_engine__WEBPACK_IMPORTED_MODULE_1__.Vector.origin,\n active: false\n };\n _lastDownPosition = undefined;\n _state = CannonState.idle;\n constructor(container){\n super(container);\n }\n clear(_particle, _delta) {}\n init() {\n const options = this.container.actualOptions.interactivity?.modes.cannon ?? new _Options_Classes_Cannon_js__WEBPACK_IMPORTED_MODULE_2__.Cannon();\n this._data = {\n spread: (0,_tsparticles_engine__WEBPACK_IMPORTED_MODULE_1__.degToRad)(options.spread),\n maxDragDistance: options.maxDragDistance,\n velocityFactor: options.velocityFactor,\n particleFactor: options.particleFactor,\n minParticles: options.minParticles,\n maxParticles: options.maxParticles,\n drawVector: options.drawVector,\n vectorColor: options.vectorColor\n };\n }\n interact(interactivityData, _delta) {\n const mouse = interactivityData.mouse, mousePos = mouse.position, clicking = mouse.clicking, downPos = mouse.downPosition;\n if (clicking && downPos && downPos !== this._lastDownPosition && this._state === CannonState.idle) {\n this._lastDownPosition = downPos;\n this._gesture = {\n origin: {\n x: downPos.x,\n y: downPos.y\n },\n current: {\n x: downPos.x,\n y: downPos.y\n },\n active: true\n };\n this._state = CannonState.aiming;\n }\n if (this._state === CannonState.aiming && mousePos) {\n this._gesture.current = {\n x: mousePos.x,\n y: mousePos.y\n };\n this._drawVector();\n }\n if (!clicking && this._state === CannonState.aiming) {\n this._gesture.active = false;\n this._fire();\n this._state = CannonState.idle;\n }\n }\n isEnabled(interactivityData) {\n const { container } = this, events = container.actualOptions.interactivity?.events;\n if (!events?.onClick.enable) {\n return false;\n }\n const modes = events.onClick.mode, modeEnabled = Array.isArray(modes) ? modes.includes(cannonMode) : modes === cannonMode;\n if (!modeEnabled) {\n return false;\n }\n return this._state !== CannonState.idle || interactivityData.mouse.clicking;\n }\n loadModeOptions(options, ...sources) {\n options.cannon ??= new _Options_Classes_Cannon_js__WEBPACK_IMPORTED_MODULE_2__.Cannon();\n for (const source of sources){\n options.cannon.load(source?.cannon);\n }\n }\n reset(_interactivityData, _particle) {}\n _drawVector() {\n this.container.canvas.render.draw((ctx)=>{\n const opts = this._data;\n if (!opts) {\n return;\n }\n const { origin, current } = this._gesture, pxRatio = this.container.retina.pixelRatio, dragDist = (0,_tsparticles_engine__WEBPACK_IMPORTED_MODULE_1__.getDistance)(origin, current), clampedDist = opts.maxDragDistance > _tsparticles_engine__WEBPACK_IMPORTED_MODULE_1__.none ? Math.min(dragDist, opts.maxDragDistance * pxRatio) : pxRatio, clampRatio = dragDist > minDistance ? clampedDist / dragDist : minDistance, clampedX = origin.x + (current.x - origin.x) * clampRatio, clampedY = origin.y + (current.y - origin.y) * clampRatio;\n ctx.save();\n ctx.strokeStyle = opts.vectorColor;\n ctx.lineWidth = _tsparticles_engine__WEBPACK_IMPORTED_MODULE_1__.double * pxRatio;\n ctx.beginPath();\n ctx.moveTo(origin.x, origin.y);\n ctx.lineTo(clampedX, clampedY);\n ctx.stroke();\n const radius = Math.max(powerRadiusMin, clampedDist * powerRadiusMaxFactor) * pxRatio;\n ctx.beginPath();\n ctx.arc(origin.x, origin.y, radius, minAngle, _tsparticles_engine__WEBPACK_IMPORTED_MODULE_1__.doublePI);\n ctx.strokeStyle = opts.vectorColor;\n ctx.lineWidth = _tsparticles_engine__WEBPACK_IMPORTED_MODULE_1__.double * pxRatio;\n ctx.stroke();\n ctx.restore();\n });\n }\n _fire() {\n const opts = this._data;\n if (!opts) {\n return;\n }\n const { origin, current } = this._gesture, pxRatio = this.container.retina.pixelRatio, dist = (0,_tsparticles_engine__WEBPACK_IMPORTED_MODULE_1__.getDistance)(origin, current), dragLength = opts.maxDragDistance > _tsparticles_engine__WEBPACK_IMPORTED_MODULE_1__.none ? Math.min(dist, opts.maxDragDistance * pxRatio) : dist;\n if (dragLength < minTapsLength) {\n return;\n }\n const pxRatioFactor = _tsparticles_engine__WEBPACK_IMPORTED_MODULE_1__.identity / pxRatio, launchAngle = angleRad(current.x, current.y, origin.x, origin.y), velocity = dragLength * pxRatioFactor * opts.velocityFactor, count = Math.min(opts.maxParticles, Math.max(opts.minParticles, Math.round(dragLength * opts.particleFactor))), toDeg = piDeg / Math.PI;\n for(let i = 0; i < count; i++){\n const spreadAngle = launchAngle + (0,_tsparticles_engine__WEBPACK_IMPORTED_MODULE_1__.getRandomInRange)(-opts.spread, opts.spread), speed = (0,_tsparticles_engine__WEBPACK_IMPORTED_MODULE_1__.getRandomInRange)(velocity * quarter, velocity);\n this.container.particles.addParticle(origin, {\n move: {\n enable: true,\n speed,\n direction: spreadAngle * toDeg\n }\n });\n }\n }\n}\n\n\n//# sourceURL=webpack://@tsparticles/interaction-external-cannon/./dist/browser/Cannoner.js?\n}");
|
|
27
|
+
|
|
28
|
+
/***/ }
|
|
29
|
+
|
|
30
|
+
}]);
|