@tsparticles/plugin-emitters-shape-canvas 3.0.3 → 3.2.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.
@@ -1,2 +1,2 @@
1
1
  /*! For license information please see tsparticles.plugin.emitters.shape.canvas.min.js.LICENSE.txt */
2
- !function(t,e){if("object"==typeof exports&&"object"==typeof module)module.exports=e(require("@tsparticles/plugin-emitters"),require("@tsparticles/engine"));else if("function"==typeof define&&define.amd)define(["@tsparticles/plugin-emitters","@tsparticles/engine"],e);else{var i="object"==typeof exports?e(require("@tsparticles/plugin-emitters"),require("@tsparticles/engine")):e(t.window,t.window);for(var s in i)("object"==typeof exports?exports:t)[s]=i[s]}}(this,((t,e)=>(()=>{"use strict";var i={533:t=>{t.exports=e},68:e=>{e.exports=t}},s={};function o(t){var e=s[t];if(void 0!==e)return e.exports;var r=s[t]={exports:{}};return i[t](r,r.exports,o),r.exports}o.d=(t,e)=>{for(var i in e)o.o(e,i)&&!o.o(t,i)&&Object.defineProperty(t,i,{enumerable:!0,get:e[i]})},o.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),o.r=t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})};var r={};return(()=>{o.r(r),o.d(r,{loadEmittersShapeCanvas:()=>f});var t=o(68),e=o(533);function i(t,e,i,s=!0){const o=t.getImageData(0,0,e.width,e.height).data;s&&t.clearRect(0,0,e.width,e.height);const r=[];for(let t=0;t<o.length;t+=i){const s=t/i,n={x:s%e.width,y:Math.floor(s/e.width)};r[n.y]||(r[n.y]=[]),r[n.y][n.x]={r:o[t],g:o[t+1],b:o[t+2],a:o[t+3]/255}}return{pixels:r,width:Math.min(...r.map((t=>t.length))),height:r.length}}class s extends t.EmitterShapeBase{constructor(t,i,s,o){super(t,i,s,o);const r=o.filter;let n=t=>t.a>0;if(void 0!==r)if((0,e.isString)(r)){if(Object.hasOwn(window,r)){const t=window[r];(0,e.isFunction)(t)&&(n=t)}}else n=r;this.filter=n,this.scale=o.scale,this.pixelData={pixels:[],height:0,width:0}}async init(){let t;const s=this.options,o=s.selector,r=s.pixels,n=s.image,a=s.element,l=s.text,c=r.offset;if(n){const s=n.src;if(!s)return;t=await function(t,s){const o=new Image;o.crossOrigin="Anonymous";const r=new Promise(((t,r)=>{o.onerror=r,o.onload=()=>{const n=document.createElement("canvas");n.width=o.width,n.height=o.height;const a=n.getContext("2d");if(!a)return r(new Error(`${e.errorPrefix} Could not get canvas context`));a.drawImage(o,0,0,o.width,o.height,0,0,n.width,n.height),t(i(a,n,s))}}));return o.src=t,r}(s,c)}else if(l){const s=function(t,s,o){const r=document.createElement("canvas"),n=r.getContext("2d"),{font:a,text:l,lines:c,color:h}=t;if(!l||!n)return;const d=l.split(c.separator),f=(0,e.isNumber)(a.size)?`${a.size}px`:a.size,p=[];let u=0,g=0;for(const t of d){n.font=`${a.style||""} ${a.variant||""} ${a.weight||""} ${f} ${a.family}`;const e=n.measureText(t),i={measure:e,text:t,height:e.actualBoundingBoxAscent+e.actualBoundingBoxDescent,width:e.width};u=Math.max(u||0,i.width),g+=i.height+c.spacing,p.push(i)}r.width=u,r.height=g;let x=0;for(const t of p)n.font=`${a.style||""} ${a.variant||""} ${a.weight||""} ${f} ${a.family}`,o?(n.fillStyle=h,n.fillText(t.text,0,x+t.measure.actualBoundingBoxAscent)):(n.strokeStyle=h,n.strokeText(t.text,0,x+t.measure.actualBoundingBoxAscent)),x+=t.height+c.spacing;return i(n,r,s)}(l,c,this.fill);if(!s)return;t=s}else if(a||o){const e=a||o&&document.querySelector(o);if(!e)return;const s=e.getContext("2d");if(!s)return;t=i(s,e,c)}t&&(this.pixelData=t)}async randomPosition(){const{height:t,width:i}=this.pixelData,s=this.pixelData,o=this.position,r=this.scale,n=o.x-i*r/2,a=o.y-t*r/2;for(let o=0;o<100;o++){const o=Math.floor((0,e.getRandom)()*i*t),l={x:o%i,y:Math.floor(o/i)},c=s.pixels[l.y][l.x];if(this.filter(c))return{position:{x:l.x*r+n,y:l.y*r+a},color:{...c},opacity:c.a}}return null}resize(t,e){super.resize(t,e)}}class n{constructor(){this.offset=4}load(t){t&&void 0!==t.offset&&(this.offset=t.offset)}}class a{constructor(){this.family="Verdana",this.size=32,this.style="",this.variant="",this.weight=""}load(t){t&&(void 0!==t.family&&(this.family=t.family),void 0!==t.size&&(this.size=t.size),void 0!==t.style&&(this.style=t.style),void 0!==t.variant&&(this.variant=t.variant),void 0!==t.weight&&(this.weight=t.weight))}}class l{constructor(){this.separator="\n",this.spacing=0}load(t){t&&(void 0!==t.separator&&(this.separator=t.separator),void 0!==t.spacing&&(this.spacing=t.spacing))}}class c{constructor(){this.color="#000000",this.font=new a,this.lines=new l,this.text=""}load(t){t&&(void 0!==t.color&&(this.color=t.color),this.font.load(t.font),this.lines.load(t.lines),void 0!==t.text&&(this.text=t.text))}}class h{constructor(){this.filter=t=>t.a>0,this.pixels=new n,this.scale=1,this.selector="",this.text=new c}load(t){t&&(void 0!==t.element&&(this.element=t.element),void 0!==t.filter&&(this.filter=t.filter),this.pixels.load(t.pixels),void 0!==t.scale&&(this.scale=t.scale),void 0!==t.selector&&(this.selector=t.selector),void 0!==t.image&&(this.image=t.image),this.text.load(t.text))}}class d{generate(t,e,i,o){const r=new h;return r.load(o),new s(t,e,i,r)}}async function f(t,e=!0){const i=t;i.addEmitterShapeGenerator&&i.addEmitterShapeGenerator("canvas",new d),await i.refresh(e)}})(),r})()));
2
+ !function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t(require("@tsparticles/plugin-emitters"),require("@tsparticles/engine"));else if("function"==typeof define&&define.amd)define(["@tsparticles/plugin-emitters","@tsparticles/engine"],t);else{var r="object"==typeof exports?t(require("@tsparticles/plugin-emitters"),require("@tsparticles/engine")):t(e.window,e.window);for(var i in r)("object"==typeof exports?exports:e)[i]=r[i]}}(this,((e,t)=>(()=>{var r,i,n={533:e=>{e.exports=t},68:t=>{t.exports=e}},o={};function a(e){var t=o[e];if(void 0!==t)return t.exports;var r=o[e]={exports:{}};return n[e](r,r.exports,a),r.exports}a.m=n,a.d=(e,t)=>{for(var r in t)a.o(t,r)&&!a.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:t[r]})},a.f={},a.e=e=>Promise.all(Object.keys(a.f).reduce(((t,r)=>(a.f[r](e,t),t)),[])),a.u=e=>e+".min.js",a.g=function(){if("object"==typeof globalThis)return globalThis;try{return this||new Function("return this")()}catch(e){if("object"==typeof window)return window}}(),a.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),r={},i="@tsparticles/plugin-emitters-shape-canvas:",a.l=(e,t,n,o)=>{if(r[e])r[e].push(t);else{var s,p;if(void 0!==n)for(var c=document.getElementsByTagName("script"),l=0;l<c.length;l++){var u=c[l];if(u.getAttribute("src")==e||u.getAttribute("data-webpack")==i+n){s=u;break}}s||(p=!0,(s=document.createElement("script")).charset="utf-8",s.timeout=120,a.nc&&s.setAttribute("nonce",a.nc),s.setAttribute("data-webpack",i+n),s.src=e),r[e]=[t];var d=(t,i)=>{s.onerror=s.onload=null,clearTimeout(f);var n=r[e];if(delete r[e],s.parentNode&&s.parentNode.removeChild(s),n&&n.forEach((e=>e(i))),t)return t(i)},f=setTimeout(d.bind(null,void 0,{type:"timeout",target:s}),12e4);s.onerror=d.bind(null,s.onerror),s.onload=d.bind(null,s.onload),p&&document.head.appendChild(s)}},a.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},(()=>{var e;a.g.importScripts&&(e=a.g.location+"");var t=a.g.document;if(!e&&t&&(t.currentScript&&(e=t.currentScript.src),!e)){var r=t.getElementsByTagName("script");if(r.length)for(var i=r.length-1;i>-1&&!e;)e=r[i--].src}if(!e)throw new Error("Automatic publicPath is not supported in this browser");e=e.replace(/#.*$/,"").replace(/\?.*$/,"").replace(/\/[^\/]+$/,"/"),a.p=e})(),(()=>{var e={926:0};a.f.j=(t,r)=>{var i=a.o(e,t)?e[t]:void 0;if(0!==i)if(i)r.push(i[2]);else{var n=new Promise(((r,n)=>i=e[t]=[r,n]));r.push(i[2]=n);var o=a.p+a.u(t),s=new Error;a.l(o,(r=>{if(a.o(e,t)&&(0!==(i=e[t])&&(e[t]=void 0),i)){var n=r&&("load"===r.type?"missing":r.type),o=r&&r.target&&r.target.src;s.message="Loading chunk "+t+" failed.\n("+n+": "+o+")",s.name="ChunkLoadError",s.type=n,s.request=o,i[1](s)}}),"chunk-"+t,t)}};var t=(t,r)=>{var i,n,o=r[0],s=r[1],p=r[2],c=0;if(o.some((t=>0!==e[t]))){for(i in s)a.o(s,i)&&(a.m[i]=s[i]);if(p)p(a)}for(t&&t(r);c<o.length;c++)n=o[c],a.o(e,n)&&e[n]&&e[n][0](),e[n]=0},r=this.webpackChunk_tsparticles_plugin_emitters_shape_canvas=this.webpackChunk_tsparticles_plugin_emitters_shape_canvas||[];r.forEach(t.bind(null,0)),r.push=t.bind(null,r.push.bind(r))})();var s={};return(()=>{async function e(e,t=!0){const r=e,{EmittersCanvasShapeGenerator:i}=await a.e(720).then(a.bind(a,720));r.addEmitterShapeGenerator?.("canvas",new i),await r.refresh(t)}a.r(s),a.d(s,{loadEmittersShapeCanvas:()=>e})})(),s})()));
@@ -1 +1 @@
1
- /*! tsParticles Emitters Shape Canvas Plugin v3.0.3 by Matteo Bruni */
1
+ /*! tsParticles Emitters Shape Canvas Plugin v3.2.0 by Matteo Bruni */
package/types/types.d.ts CHANGED
@@ -1,12 +1,12 @@
1
1
  import type { IRgba } from "@tsparticles/engine";
2
- export type CanvasPixelData = {
2
+ export interface CanvasPixelData {
3
3
  height: number;
4
4
  pixels: IRgba[][];
5
5
  width: number;
6
- };
7
- export type TextLineData = {
6
+ }
7
+ export interface TextLineData {
8
8
  height: number;
9
9
  measure: TextMetrics;
10
10
  text: string;
11
11
  width: number;
12
- };
12
+ }
package/types/utils.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { CanvasPixelData } from "./types.js";
2
- import type { IDimension } from "@tsparticles/engine";
2
+ import { type IDimension } from "@tsparticles/engine";
3
3
  import type { TextOptions } from "./Options/Classes/TextOptions.js";
4
4
  export declare function getCanvasImageData(ctx: CanvasRenderingContext2D, size: IDimension, offset: number, clear?: boolean): CanvasPixelData;
5
5
  export declare function getImageData(src: string, offset: number): Promise<CanvasPixelData>;
@@ -13,11 +13,12 @@
13
13
  const plugin_emitters_1 = require("@tsparticles/plugin-emitters");
14
14
  const engine_1 = require("@tsparticles/engine");
15
15
  const utils_js_1 = require("./utils.js");
16
+ const maxRetries = 100, half = 0.5;
16
17
  class EmittersCanvasShape extends plugin_emitters_1.EmitterShapeBase {
17
18
  constructor(position, size, fill, options) {
18
19
  super(position, size, fill, options);
19
- const filter = options.filter;
20
- let filterFunc = (pixel) => pixel.a > 0;
20
+ const filter = options.filter, minAlpha = 0;
21
+ let filterFunc = (pixel) => pixel.a > minAlpha;
21
22
  if (filter !== undefined) {
22
23
  if ((0, engine_1.isString)(filter)) {
23
24
  if (Object.hasOwn(window, filter)) {
@@ -56,8 +57,8 @@
56
57
  }
57
58
  pixelData = data;
58
59
  }
59
- else if (element || selector) {
60
- const canvas = element || (selector && document.querySelector(selector));
60
+ else if (element ?? selector) {
61
+ const canvas = element ?? (selector && document.querySelector(selector));
61
62
  if (!canvas) {
62
63
  return;
63
64
  }
@@ -73,12 +74,11 @@
73
74
  this.pixelData = pixelData;
74
75
  }
75
76
  async randomPosition() {
76
- const { height, width } = this.pixelData, data = this.pixelData, position = this.position, scale = this.scale;
77
- const positionOffset = {
78
- x: position.x - (width * scale) / 2,
79
- y: position.y - (height * scale) / 2,
77
+ const { height, width } = this.pixelData, data = this.pixelData, position = this.position, scale = this.scale, positionOffset = {
78
+ x: position.x - width * scale * half,
79
+ y: position.y - height * scale * half,
80
80
  };
81
- for (let i = 0; i < 100; i++) {
81
+ for (let i = 0; i < maxRetries; i++) {
82
82
  const nextIndex = Math.floor((0, engine_1.getRandom)() * width * height), pixelPos = {
83
83
  x: nextIndex % width,
84
84
  y: Math.floor(nextIndex / width),
@@ -86,16 +86,16 @@
86
86
  if (!shouldCreateParticle) {
87
87
  continue;
88
88
  }
89
- return {
89
+ return Promise.resolve({
90
90
  position: {
91
91
  x: pixelPos.x * scale + positionOffset.x,
92
92
  y: pixelPos.y * scale + positionOffset.y,
93
93
  },
94
94
  color: { ...pixel },
95
95
  opacity: pixel.a,
96
- };
96
+ });
97
97
  }
98
- return null;
98
+ return Promise.resolve(null);
99
99
  }
100
100
  resize(position, size) {
101
101
  super.resize(position, size);
@@ -12,9 +12,10 @@
12
12
  exports.EmittersCanvasShapeOptions = void 0;
13
13
  const PixelsOptions_js_1 = require("./PixelsOptions.js");
14
14
  const TextOptions_js_1 = require("./TextOptions.js");
15
+ const minAlpha = 0;
15
16
  class EmittersCanvasShapeOptions {
16
17
  constructor() {
17
- this.filter = (pixel) => pixel.a > 0;
18
+ this.filter = (pixel) => pixel.a > minAlpha;
18
19
  this.pixels = new PixelsOptions_js_1.PixelsOptions();
19
20
  this.scale = 1;
20
21
  this.selector = "";
package/umd/index.js CHANGED
@@ -1,20 +1,42 @@
1
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
2
+ if (k2 === undefined) k2 = k;
3
+ var desc = Object.getOwnPropertyDescriptor(m, k);
4
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
5
+ desc = { enumerable: true, get: function() { return m[k]; } };
6
+ }
7
+ Object.defineProperty(o, k2, desc);
8
+ }) : (function(o, m, k, k2) {
9
+ if (k2 === undefined) k2 = k;
10
+ o[k2] = m[k];
11
+ }));
12
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
13
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
14
+ }) : function(o, v) {
15
+ o["default"] = v;
16
+ });
17
+ var __importStar = (this && this.__importStar) || function (mod) {
18
+ if (mod && mod.__esModule) return mod;
19
+ var result = {};
20
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
21
+ __setModuleDefault(result, mod);
22
+ return result;
23
+ };
1
24
  (function (factory) {
2
25
  if (typeof module === "object" && typeof module.exports === "object") {
3
26
  var v = factory(require, exports);
4
27
  if (v !== undefined) module.exports = v;
5
28
  }
6
29
  else if (typeof define === "function" && define.amd) {
7
- define(["require", "exports", "./EmittersCanvasShapeGenerator.js"], factory);
30
+ define(["require", "exports"], factory);
8
31
  }
9
32
  })(function (require, exports) {
10
33
  "use strict";
34
+ var __syncRequire = typeof module === "object" && typeof module.exports === "object";
11
35
  Object.defineProperty(exports, "__esModule", { value: true });
12
36
  exports.loadEmittersShapeCanvas = void 0;
13
- const EmittersCanvasShapeGenerator_js_1 = require("./EmittersCanvasShapeGenerator.js");
14
37
  async function loadEmittersShapeCanvas(engine, refresh = true) {
15
- const emittersEngine = engine;
16
- emittersEngine.addEmitterShapeGenerator &&
17
- emittersEngine.addEmitterShapeGenerator("canvas", new EmittersCanvasShapeGenerator_js_1.EmittersCanvasShapeGenerator());
38
+ const emittersEngine = engine, { EmittersCanvasShapeGenerator } = await (__syncRequire ? Promise.resolve().then(() => __importStar(require("./EmittersCanvasShapeGenerator.js"))) : new Promise((resolve_1, reject_1) => { require(["./EmittersCanvasShapeGenerator.js"], resolve_1, reject_1); }).then(__importStar));
39
+ emittersEngine.addEmitterShapeGenerator?.("canvas", new EmittersCanvasShapeGenerator());
18
40
  await emittersEngine.refresh(refresh);
19
41
  }
20
42
  exports.loadEmittersShapeCanvas = loadEmittersShapeCanvas;
package/umd/utils.js CHANGED
@@ -11,10 +11,14 @@
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.getTextData = exports.getImageData = exports.getCanvasImageData = void 0;
13
13
  const engine_1 = require("@tsparticles/engine");
14
+ const origin = {
15
+ x: 0,
16
+ y: 0,
17
+ }, minWidth = 0;
14
18
  function getCanvasImageData(ctx, size, offset, clear = true) {
15
- const imageData = ctx.getImageData(0, 0, size.width, size.height).data;
19
+ const imageData = ctx.getImageData(origin.x, origin.y, size.width, size.height).data;
16
20
  if (clear) {
17
- ctx.clearRect(0, 0, size.width, size.height);
21
+ ctx.clearRect(origin.x, origin.y, size.width, size.height);
18
22
  }
19
23
  const pixels = [];
20
24
  for (let i = 0; i < imageData.length; i += offset) {
@@ -25,11 +29,17 @@
25
29
  if (!pixels[pos.y]) {
26
30
  pixels[pos.y] = [];
27
31
  }
32
+ const indexesOffset = {
33
+ r: 0,
34
+ g: 1,
35
+ b: 2,
36
+ a: 3,
37
+ }, alphaFactor = 255;
28
38
  pixels[pos.y][pos.x] = {
29
- r: imageData[i],
30
- g: imageData[i + 1],
31
- b: imageData[i + 2],
32
- a: imageData[i + 3] / 255,
39
+ r: imageData[i + indexesOffset.r],
40
+ g: imageData[i + indexesOffset.g],
41
+ b: imageData[i + indexesOffset.b],
42
+ a: imageData[i + indexesOffset.a] / alphaFactor,
33
43
  };
34
44
  }
35
45
  return {
@@ -52,7 +62,7 @@
52
62
  if (!context) {
53
63
  return reject(new Error(`${engine_1.errorPrefix} Could not get canvas context`));
54
64
  }
55
- context.drawImage(image, 0, 0, image.width, image.height, 0, 0, canvas.width, canvas.height);
65
+ context.drawImage(image, origin.x, origin.y, image.width, image.height, origin.x, origin.y, canvas.width, canvas.height);
56
66
  resolve(getCanvasImageData(context, canvas, offset));
57
67
  };
58
68
  });
@@ -75,7 +85,7 @@
75
85
  height: measure.actualBoundingBoxAscent + measure.actualBoundingBoxDescent,
76
86
  width: measure.width,
77
87
  };
78
- maxWidth = Math.max(maxWidth || 0, lineData.width);
88
+ maxWidth = Math.max(maxWidth || minWidth, lineData.width);
79
89
  totalHeight += lineData.height + linesOptions.spacing;
80
90
  linesData.push(lineData);
81
91
  }
@@ -86,11 +96,11 @@
86
96
  context.font = `${font.style || ""} ${font.variant || ""} ${font.weight || ""} ${fontSize} ${font.family}`;
87
97
  if (fill) {
88
98
  context.fillStyle = color;
89
- context.fillText(line.text, 0, currentHeight + line.measure.actualBoundingBoxAscent);
99
+ context.fillText(line.text, origin.x, currentHeight + line.measure.actualBoundingBoxAscent);
90
100
  }
91
101
  else {
92
102
  context.strokeStyle = color;
93
- context.strokeText(line.text, 0, currentHeight + line.measure.actualBoundingBoxAscent);
103
+ context.strokeText(line.text, origin.x, currentHeight + line.measure.actualBoundingBoxAscent);
94
104
  }
95
105
  currentHeight += line.height + linesOptions.spacing;
96
106
  }