@tsparticles/plugin-canvas-mask 3.0.0-alpha.0 → 3.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.
@@ -4,7 +4,7 @@
4
4
  * Demo / Generator : https://particles.js.org/
5
5
  * GitHub : https://www.github.com/matteobruni/tsparticles
6
6
  * How to use? : Check the GitHub README
7
- * v3.0.0-alpha.0
7
+ * v3.0.0-beta.0
8
8
  */
9
9
  (function webpackUniversalModuleDefinition(root, factory) {
10
10
  if(typeof exports === 'object' && typeof module === 'object')
@@ -91,7 +91,7 @@ __webpack_require__.r(__webpack_exports__);
91
91
 
92
92
  // EXPORTS
93
93
  __webpack_require__.d(__webpack_exports__, {
94
- "loadCanvasMaskPlugin": () => (/* binding */ loadCanvasMaskPlugin)
94
+ loadCanvasMaskPlugin: () => (/* binding */ loadCanvasMaskPlugin)
95
95
  });
96
96
 
97
97
  ;// CONCATENATED MODULE: ./dist/browser/Options/Classes/CanvasMaskOverride.js
@@ -112,7 +112,10 @@ class CanvasMaskOverride {
112
112
  }
113
113
  }
114
114
  }
115
+ // EXTERNAL MODULE: external {"commonjs":"@tsparticles/engine","commonjs2":"@tsparticles/engine","amd":"@tsparticles/engine","root":"window"}
116
+ var engine_root_window_ = __webpack_require__(533);
115
117
  ;// CONCATENATED MODULE: ./dist/browser/Options/Classes/CanvasMaskPixels.js
118
+
116
119
  class CanvasMaskPixels {
117
120
  constructor() {
118
121
  this.filter = pixel => pixel.a > 0;
@@ -123,10 +126,10 @@ class CanvasMaskPixels {
123
126
  return;
124
127
  }
125
128
  if (data.filter !== undefined) {
126
- if (typeof data.filter === "string") {
129
+ if ((0,engine_root_window_.isString)(data.filter)) {
127
130
  if (Object.hasOwn(window, data.filter)) {
128
131
  const filter = window[data.filter];
129
- if (typeof filter === "function") {
132
+ if ((0,engine_root_window_.isFunction)(filter)) {
130
133
  this.filter = filter;
131
134
  }
132
135
  }
@@ -239,7 +242,6 @@ class CanvasMask {
239
242
  this.scale = 1;
240
243
  }
241
244
  load(data) {
242
- var _a, _b;
243
245
  if (!data) {
244
246
  return;
245
247
  }
@@ -258,8 +260,8 @@ class CanvasMask {
258
260
  this.pixels.load(data.pixels);
259
261
  if (data.position) {
260
262
  this.position = {
261
- x: (_a = data.position.x) !== null && _a !== void 0 ? _a : this.position.x,
262
- y: (_b = data.position.y) !== null && _b !== void 0 ? _b : this.position.y
263
+ x: data.position.x ?? this.position.x,
264
+ y: data.position.y ?? this.position.y
263
265
  };
264
266
  }
265
267
  this.override.load(data.override);
@@ -277,8 +279,6 @@ class CanvasMask {
277
279
  }
278
280
  }
279
281
  }
280
- // EXTERNAL MODULE: external {"commonjs":"@tsparticles/engine","commonjs2":"@tsparticles/engine","amd":"@tsparticles/engine","root":"window"}
281
- var engine_root_window_ = __webpack_require__(533);
282
282
  ;// CONCATENATED MODULE: ./dist/browser/utils.js
283
283
 
284
284
  function shuffle(array) {
@@ -310,25 +310,26 @@ function addParticlesFromCanvasPixels(container, data, position, scale, override
310
310
  },
311
311
  pixel = data.pixels[pixelPos.y][pixelPos.x],
312
312
  shouldCreateParticle = filter(pixel);
313
- if (shouldCreateParticle) {
314
- const pos = {
313
+ if (!shouldCreateParticle) {
314
+ continue;
315
+ }
316
+ const pos = {
315
317
  x: pixelPos.x * scale + positionOffset.x,
316
318
  y: pixelPos.y * scale + positionOffset.y
319
+ },
320
+ pOptions = {};
321
+ if (override.color) {
322
+ pOptions.color = {
323
+ value: pixel
324
+ };
325
+ }
326
+ if (override.opacity) {
327
+ pOptions.opacity = {
328
+ value: pixel.a
317
329
  };
318
- const pOptions = {};
319
- if (override.color) {
320
- pOptions.color = {
321
- value: pixel
322
- };
323
- }
324
- if (override.opacity) {
325
- pOptions.opacity = {
326
- value: pixel.a
327
- };
328
- }
329
- container.particles.addParticle(pos, pOptions);
330
- selectedPixels++;
331
330
  }
331
+ container.particles.addParticle(pos, pOptions);
332
+ selectedPixels++;
332
333
  }
333
334
  }
334
335
  function getCanvasImageData(ctx, size, offset, clear = true) {
@@ -370,7 +371,7 @@ function getImageData(src, offset) {
370
371
  canvas.height = image.height;
371
372
  const context = canvas.getContext("2d");
372
373
  if (!context) {
373
- return reject(new Error("Could not get canvas context"));
374
+ return reject(new Error(`${engine_root_window_.errorPrefix} Could not get canvas context`));
374
375
  }
375
376
  context.drawImage(image, 0, 0, image.width, image.height, 0, 0, canvas.width, canvas.height);
376
377
  resolve(getCanvasImageData(context, canvas, offset));
@@ -392,7 +393,7 @@ function getTextData(textOptions, offset) {
392
393
  return;
393
394
  }
394
395
  const lines = text.split(linesOptions.separator),
395
- fontSize = typeof font.size === "number" ? `${font.size}px` : font.size,
396
+ fontSize = (0,engine_root_window_.isNumber)(font.size) ? `${font.size}px` : font.size,
396
397
  linesData = [];
397
398
  let maxWidth = 0,
398
399
  totalHeight = 0;
@@ -431,7 +432,7 @@ class CanvasMaskInstance {
431
432
  async init() {
432
433
  const container = this._container,
433
434
  options = container.actualOptions.canvasMask;
434
- if (!(options === null || options === void 0 ? void 0 : options.enable)) {
435
+ if (!options?.enable) {
435
436
  return;
436
437
  }
437
438
  let pixelData = {
@@ -479,23 +480,21 @@ class CanvasMaskPlugin {
479
480
  return new CanvasMaskInstance(container, this._engine);
480
481
  }
481
482
  loadOptions(options, source) {
482
- if (!this.needsPlugin(source)) {
483
+ if (!this.needsPlugin(options) && !this.needsPlugin(source)) {
483
484
  return;
484
485
  }
485
486
  let canvasMaskOptions = options.canvasMask;
486
- if ((canvasMaskOptions === null || canvasMaskOptions === void 0 ? void 0 : canvasMaskOptions.load) === undefined) {
487
+ if (canvasMaskOptions?.load === undefined) {
487
488
  options.canvasMask = canvasMaskOptions = new CanvasMask();
488
489
  }
489
- canvasMaskOptions.load(source === null || source === void 0 ? void 0 : source.canvasMask);
490
+ canvasMaskOptions.load(source?.canvasMask);
490
491
  }
491
492
  needsPlugin(options) {
492
- var _a, _b;
493
- return (_b = (_a = options === null || options === void 0 ? void 0 : options.canvasMask) === null || _a === void 0 ? void 0 : _a.enable) !== null && _b !== void 0 ? _b : false;
493
+ return options?.canvasMask?.enable ?? false;
494
494
  }
495
495
  }
496
- async function loadCanvasMaskPlugin(engine) {
497
- const plugin = new CanvasMaskPlugin(engine);
498
- await engine.addPlugin(plugin);
496
+ async function loadCanvasMaskPlugin(engine, refresh = true) {
497
+ await engine.addPlugin(new CanvasMaskPlugin(engine), refresh);
499
498
  }
500
499
  })();
501
500
 
@@ -1,2 +1,2 @@
1
1
  /*! For license information please see tsparticles.plugin.canvas-mask.min.js.LICENSE.txt */
2
- !function(t,e){if("object"==typeof exports&&"object"==typeof module)module.exports=e(require("@tsparticles/engine"));else if("function"==typeof define&&define.amd)define(["@tsparticles/engine"],e);else{var i="object"==typeof exports?e(require("@tsparticles/engine")):e(t.window);for(var o in i)("object"==typeof exports?exports:t)[o]=i[o]}}(this,(t=>(()=>{"use strict";var e={533:e=>{e.exports=t}},i={};function o(t){var s=i[t];if(void 0!==s)return s.exports;var n=i[t]={exports:{}};return e[t](n,n.exports,o),n.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 s={};return(()=>{o.r(s),o.d(s,{loadCanvasMaskPlugin:()=>g});class t{constructor(){this.color=!0,this.opacity=!1}load(t){t&&(void 0!==t.color&&(this.color=t.color),void 0!==t.opacity&&(this.opacity=t.opacity))}}class e{constructor(){this.filter=t=>t.a>0,this.offset=4}load(t){if(t){if(void 0!==t.filter)if("string"==typeof t.filter){if(Object.hasOwn(window,t.filter)){const e=window[t.filter];"function"==typeof e&&(this.filter=e)}}else this.filter=t.filter;void 0!==t.offset&&(this.offset=t.offset)}}}class i{constructor(){this.src=""}load(t){t&&void 0!==t.src&&(this.src=t.src)}}class n{constructor(){this.family="sans-serif",this.size=100}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 a{constructor(){this.separator="\n",this.spacing=10}load(t){t&&(void 0!==t.separator&&(this.separator=t.separator),void 0!==t.spacing&&(this.spacing=t.spacing))}}class r{constructor(){this.color="#000000",this.font=new n,this.lines=new a,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 l{constructor(){this.enable=!1,this.override=new t,this.pixels=new e,this.position={x:50,y:50},this.scale=1}load(t){var e,o;t&&(void 0!==t.element&&t.element instanceof HTMLCanvasElement&&(this.element=t.element),void 0!==t.enable&&(this.enable=t.enable),t.image&&(this.image||(this.image=new i),this.image.load(t.image)),this.pixels.load(t.pixels),t.position&&(this.position={x:null!==(e=t.position.x)&&void 0!==e?e:this.position.x,y:null!==(o=t.position.y)&&void 0!==o?o:this.position.y}),this.override.load(t.override),void 0!==t.scale&&(this.scale=t.scale),void 0!==t.selector&&(this.selector=t.selector),t.text&&(this.text||(this.text=new r),this.text.load(t.text)))}}var c=o(533);function h(t,e,i,o,s,n){const{height:a,width:r}=e,l=a*r,h=function(t){for(let e=t.length-1;e>=0;e--){const i=Math.floor((0,c.getRandom)()*e);[t[e],t[i]]=[t[i],t[e]]}return t}(f(l)),d=Math.min(l,t.actualOptions.particles.number.value),u=t.canvas.size;let p=0;const g=u.width*i.x/100-r*o/2,v=u.height*i.y/100-a*o/2;for(;p<d&&h.length;){const i=h.pop()||0,a={x:i%r,y:Math.floor(i/r)},l=e.pixels[a.y][a.x];if(n(l)){const e={x:a.x*o+g,y:a.y*o+v},i={};s.color&&(i.color={value:l}),s.opacity&&(i.opacity={value:l.a}),t.particles.addParticle(e,i),p++}}}function d(t,e,i,o=!0){const s=t.getImageData(0,0,e.width,e.height).data;o&&t.clearRect(0,0,e.width,e.height);const n=[];for(let t=0;t<s.length;t+=i){const o=t/i,a={x:o%e.width,y:Math.floor(o/e.width)};n[a.y]||(n[a.y]=[]),n[a.y][a.x]={r:s[t],g:s[t+1],b:s[t+2],a:s[t+3]/255}}return{pixels:n,width:Math.min(...n.map((t=>t.length))),height:n.length}}const f=t=>[...Array(t).keys()];class u{constructor(t,e){this._container=t,this._engine=e}async init(){const t=this._container,e=t.actualOptions.canvasMask;if(!(null==e?void 0:e.enable))return;let i={pixels:[],height:0,width:0};const o=e.pixels.offset;if(e.image){const t=e.image.src;if(!t)return;i=await function(t,e){const i=new Image;i.crossOrigin="Anonymous";const o=new Promise(((t,o)=>{i.onerror=o,i.onload=()=>{const s=document.createElement("canvas");s.width=i.width,s.height=i.height;const n=s.getContext("2d");if(!n)return o(new Error("Could not get canvas context"));n.drawImage(i,0,0,i.width,i.height,0,0,s.width,s.height),t(d(n,s,e))}}));return i.src=t,o}(t,o)}else if(e.text){const t=function(t,e){const i=document.createElement("canvas"),o=i.getContext("2d"),{font:s,text:n,lines:a,color:r}=t;if(!n||!o)return;const l=n.split(a.separator),c="number"==typeof s.size?`${s.size}px`:s.size,h=[];let f=0,u=0;for(const t of l){o.font=`${s.style||""} ${s.variant||""} ${s.weight||""} ${c} ${s.family}`;const e=o.measureText(t),i={measure:e,text:t,height:e.actualBoundingBoxAscent+e.actualBoundingBoxDescent,width:e.width};f=Math.max(f||0,i.width),u+=i.height+a.spacing,h.push(i)}i.width=f,i.height=u;let p=0;for(const t of h)o.font=`${s.style||""} ${s.variant||""} ${s.weight||""} ${c} ${s.family}`,o.fillStyle=r,o.fillText(t.text,0,p+t.measure.actualBoundingBoxAscent),p+=t.height+a.spacing;return d(o,i,e)}(e.text,o);if(!t)return;i=t}else if(e.element||e.selector){const t=e.element||e.selector&&document.querySelector(e.selector);if(!t)return;const s=t.getContext("2d");if(!s)return;i=d(s,t,o)}h(t,i,e.position,e.scale,e.override,e.pixels.filter)}}class p{constructor(t){this.id="canvasMask",this._engine=t}getPlugin(t){return new u(t,this._engine)}loadOptions(t,e){if(!this.needsPlugin(e))return;let i=t.canvasMask;void 0===(null==i?void 0:i.load)&&(t.canvasMask=i=new l),i.load(null==e?void 0:e.canvasMask)}needsPlugin(t){var e,i;return null!==(i=null===(e=null==t?void 0:t.canvasMask)||void 0===e?void 0:e.enable)&&void 0!==i&&i}}async function g(t){const e=new p(t);await t.addPlugin(e)}})(),s})()));
2
+ !function(t,e){if("object"==typeof exports&&"object"==typeof module)module.exports=e(require("@tsparticles/engine"));else if("function"==typeof define&&define.amd)define(["@tsparticles/engine"],e);else{var i="object"==typeof exports?e(require("@tsparticles/engine")):e(t.window);for(var s in i)("object"==typeof exports?exports:t)[s]=i[s]}}(this,(t=>(()=>{"use strict";var e={533:e=>{e.exports=t}},i={};function s(t){var o=i[t];if(void 0!==o)return o.exports;var n=i[t]={exports:{}};return e[t](n,n.exports,s),n.exports}s.d=(t,e)=>{for(var i in e)s.o(e,i)&&!s.o(t,i)&&Object.defineProperty(t,i,{enumerable:!0,get:e[i]})},s.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),s.r=t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})};var o={};return(()=>{s.r(o),s.d(o,{loadCanvasMaskPlugin:()=>p});class t{constructor(){this.color=!0,this.opacity=!1}load(t){t&&(void 0!==t.color&&(this.color=t.color),void 0!==t.opacity&&(this.opacity=t.opacity))}}var e=s(533);class i{constructor(){this.filter=t=>t.a>0,this.offset=4}load(t){if(t){if(void 0!==t.filter)if((0,e.isString)(t.filter)){if(Object.hasOwn(window,t.filter)){const i=window[t.filter];(0,e.isFunction)(i)&&(this.filter=i)}}else this.filter=t.filter;void 0!==t.offset&&(this.offset=t.offset)}}}class n{constructor(){this.src=""}load(t){t&&void 0!==t.src&&(this.src=t.src)}}class r{constructor(){this.family="sans-serif",this.size=100}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 a{constructor(){this.separator="\n",this.spacing=10}load(t){t&&(void 0!==t.separator&&(this.separator=t.separator),void 0!==t.spacing&&(this.spacing=t.spacing))}}class l{constructor(){this.color="#000000",this.font=new r,this.lines=new a,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 c{constructor(){this.enable=!1,this.override=new t,this.pixels=new i,this.position={x:50,y:50},this.scale=1}load(t){t&&(void 0!==t.element&&t.element instanceof HTMLCanvasElement&&(this.element=t.element),void 0!==t.enable&&(this.enable=t.enable),t.image&&(this.image||(this.image=new n),this.image.load(t.image)),this.pixels.load(t.pixels),t.position&&(this.position={x:t.position.x??this.position.x,y:t.position.y??this.position.y}),this.override.load(t.override),void 0!==t.scale&&(this.scale=t.scale),void 0!==t.selector&&(this.selector=t.selector),t.text&&(this.text||(this.text=new l),this.text.load(t.text)))}}function h(t,i,s,o,n,r){const{height:a,width:l}=i,c=a*l,h=function(t){for(let i=t.length-1;i>=0;i--){const s=Math.floor((0,e.getRandom)()*i);[t[i],t[s]]=[t[s],t[i]]}return t}(f(c)),d=Math.min(c,t.actualOptions.particles.number.value),u=t.canvas.size;let g=0;const p=u.width*s.x/100-l*o/2,x=u.height*s.y/100-a*o/2;for(;g<d&&h.length;){const e=h.pop()||0,s={x:e%l,y:Math.floor(e/l)},a=i.pixels[s.y][s.x];if(!r(a))continue;const c={x:s.x*o+p,y:s.y*o+x},d={};n.color&&(d.color={value:a}),n.opacity&&(d.opacity={value:a.a}),t.particles.addParticle(c,d),g++}}function d(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 n=[];for(let t=0;t<o.length;t+=i){const s=t/i,r={x:s%e.width,y:Math.floor(s/e.width)};n[r.y]||(n[r.y]=[]),n[r.y][r.x]={r:o[t],g:o[t+1],b:o[t+2],a:o[t+3]/255}}return{pixels:n,width:Math.min(...n.map((t=>t.length))),height:n.length}}const f=t=>[...Array(t).keys()];class u{constructor(t,e){this._container=t,this._engine=e}async init(){const t=this._container,i=t.actualOptions.canvasMask;if(!i?.enable)return;let s={pixels:[],height:0,width:0};const o=i.pixels.offset;if(i.image){const t=i.image.src;if(!t)return;s=await function(t,i){const s=new Image;s.crossOrigin="Anonymous";const o=new Promise(((t,o)=>{s.onerror=o,s.onload=()=>{const n=document.createElement("canvas");n.width=s.width,n.height=s.height;const r=n.getContext("2d");if(!r)return o(new Error(`${e.errorPrefix} Could not get canvas context`));r.drawImage(s,0,0,s.width,s.height,0,0,n.width,n.height),t(d(r,n,i))}}));return s.src=t,o}(t,o)}else if(i.text){const t=function(t,i){const s=document.createElement("canvas"),o=s.getContext("2d"),{font:n,text:r,lines:a,color:l}=t;if(!r||!o)return;const c=r.split(a.separator),h=(0,e.isNumber)(n.size)?`${n.size}px`:n.size,f=[];let u=0,g=0;for(const t of c){o.font=`${n.style||""} ${n.variant||""} ${n.weight||""} ${h} ${n.family}`;const e=o.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+a.spacing,f.push(i)}s.width=u,s.height=g;let p=0;for(const t of f)o.font=`${n.style||""} ${n.variant||""} ${n.weight||""} ${h} ${n.family}`,o.fillStyle=l,o.fillText(t.text,0,p+t.measure.actualBoundingBoxAscent),p+=t.height+a.spacing;return d(o,s,i)}(i.text,o);if(!t)return;s=t}else if(i.element||i.selector){const t=i.element||i.selector&&document.querySelector(i.selector);if(!t)return;const e=t.getContext("2d");if(!e)return;s=d(e,t,o)}h(t,s,i.position,i.scale,i.override,i.pixels.filter)}}class g{constructor(t){this.id="canvasMask",this._engine=t}getPlugin(t){return new u(t,this._engine)}loadOptions(t,e){if(!this.needsPlugin(t)&&!this.needsPlugin(e))return;let i=t.canvasMask;void 0===i?.load&&(t.canvasMask=i=new c),i.load(e?.canvasMask)}needsPlugin(t){return t?.canvasMask?.enable??!1}}async function p(t,e=!0){await t.addPlugin(new g(t),e)}})(),o})()));
@@ -1,8 +1 @@
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
- * v3.0.0-alpha.0
8
- */
1
+ /*! tsParticles Canvas Mask Plugin v3.0.0-beta.0 by Matteo Bruni */
package/types/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  import type { Engine } from "@tsparticles/engine";
2
- export declare function loadCanvasMaskPlugin(engine: Engine): Promise<void>;
2
+ export declare function loadCanvasMaskPlugin(engine: Engine, refresh?: boolean): Promise<void>;
package/types/utils.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { Container, ICoordinates, IDimension, IRgba } from "@tsparticles/engine";
1
+ import { type Container, type ICoordinates, type IDimension, type IRgba } from "@tsparticles/engine";
2
2
  import type { ICanvasMaskOverride } from "./Options/Interfaces/ICanvasMaskOverride";
3
3
  import type { TextMask } from "./Options/Classes/TextMask";
4
4
  export type CanvasPixelData = {
@@ -18,7 +18,7 @@
18
18
  }
19
19
  async init() {
20
20
  const container = this._container, options = container.actualOptions.canvasMask;
21
- if (!(options === null || options === void 0 ? void 0 : options.enable)) {
21
+ if (!options?.enable) {
22
22
  return;
23
23
  }
24
24
  let pixelData = {
@@ -26,7 +26,6 @@
26
26
  this.scale = 1;
27
27
  }
28
28
  load(data) {
29
- var _a, _b;
30
29
  if (!data) {
31
30
  return;
32
31
  }
@@ -45,8 +44,8 @@
45
44
  this.pixels.load(data.pixels);
46
45
  if (data.position) {
47
46
  this.position = {
48
- x: (_a = data.position.x) !== null && _a !== void 0 ? _a : this.position.x,
49
- y: (_b = data.position.y) !== null && _b !== void 0 ? _b : this.position.y,
47
+ x: data.position.x ?? this.position.x,
48
+ y: data.position.y ?? this.position.y,
50
49
  };
51
50
  }
52
51
  this.override.load(data.override);
@@ -4,12 +4,13 @@
4
4
  if (v !== undefined) module.exports = v;
5
5
  }
6
6
  else if (typeof define === "function" && define.amd) {
7
- define(["require", "exports"], factory);
7
+ define(["require", "exports", "@tsparticles/engine"], factory);
8
8
  }
9
9
  })(function (require, exports) {
10
10
  "use strict";
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.CanvasMaskPixels = void 0;
13
+ const engine_1 = require("@tsparticles/engine");
13
14
  class CanvasMaskPixels {
14
15
  constructor() {
15
16
  this.filter = (pixel) => pixel.a > 0;
@@ -20,10 +21,10 @@
20
21
  return;
21
22
  }
22
23
  if (data.filter !== undefined) {
23
- if (typeof data.filter === "string") {
24
+ if ((0, engine_1.isString)(data.filter)) {
24
25
  if (Object.hasOwn(window, data.filter)) {
25
26
  const filter = window[data.filter];
26
- if (typeof filter === "function") {
27
+ if ((0, engine_1.isFunction)(filter)) {
27
28
  this.filter = filter;
28
29
  }
29
30
  }
package/umd/index.js CHANGED
@@ -21,23 +21,21 @@
21
21
  return new CanvasMaskInstance_1.CanvasMaskInstance(container, this._engine);
22
22
  }
23
23
  loadOptions(options, source) {
24
- if (!this.needsPlugin(source)) {
24
+ if (!this.needsPlugin(options) && !this.needsPlugin(source)) {
25
25
  return;
26
26
  }
27
27
  let canvasMaskOptions = options.canvasMask;
28
- if ((canvasMaskOptions === null || canvasMaskOptions === void 0 ? void 0 : canvasMaskOptions.load) === undefined) {
28
+ if (canvasMaskOptions?.load === undefined) {
29
29
  options.canvasMask = canvasMaskOptions = new CanvasMask_1.CanvasMask();
30
30
  }
31
- canvasMaskOptions.load(source === null || source === void 0 ? void 0 : source.canvasMask);
31
+ canvasMaskOptions.load(source?.canvasMask);
32
32
  }
33
33
  needsPlugin(options) {
34
- var _a, _b;
35
- return (_b = (_a = options === null || options === void 0 ? void 0 : options.canvasMask) === null || _a === void 0 ? void 0 : _a.enable) !== null && _b !== void 0 ? _b : false;
34
+ return options?.canvasMask?.enable ?? false;
36
35
  }
37
36
  }
38
- async function loadCanvasMaskPlugin(engine) {
39
- const plugin = new CanvasMaskPlugin(engine);
40
- await engine.addPlugin(plugin);
37
+ async function loadCanvasMaskPlugin(engine, refresh = true) {
38
+ await engine.addPlugin(new CanvasMaskPlugin(engine), refresh);
41
39
  }
42
40
  exports.loadCanvasMaskPlugin = loadCanvasMaskPlugin;
43
41
  });
package/umd/utils.js CHANGED
@@ -31,25 +31,25 @@
31
31
  x: nextIndex % width,
32
32
  y: Math.floor(nextIndex / width),
33
33
  }, pixel = data.pixels[pixelPos.y][pixelPos.x], shouldCreateParticle = filter(pixel);
34
- if (shouldCreateParticle) {
35
- const pos = {
36
- x: pixelPos.x * scale + positionOffset.x,
37
- y: pixelPos.y * scale + positionOffset.y,
34
+ if (!shouldCreateParticle) {
35
+ continue;
36
+ }
37
+ const pos = {
38
+ x: pixelPos.x * scale + positionOffset.x,
39
+ y: pixelPos.y * scale + positionOffset.y,
40
+ }, pOptions = {};
41
+ if (override.color) {
42
+ pOptions.color = {
43
+ value: pixel,
44
+ };
45
+ }
46
+ if (override.opacity) {
47
+ pOptions.opacity = {
48
+ value: pixel.a,
38
49
  };
39
- const pOptions = {};
40
- if (override.color) {
41
- pOptions.color = {
42
- value: pixel,
43
- };
44
- }
45
- if (override.opacity) {
46
- pOptions.opacity = {
47
- value: pixel.a,
48
- };
49
- }
50
- container.particles.addParticle(pos, pOptions);
51
- selectedPixels++;
52
50
  }
51
+ container.particles.addParticle(pos, pOptions);
52
+ selectedPixels++;
53
53
  }
54
54
  }
55
55
  exports.addParticlesFromCanvasPixels = addParticlesFromCanvasPixels;
@@ -92,7 +92,7 @@
92
92
  canvas.height = image.height;
93
93
  const context = canvas.getContext("2d");
94
94
  if (!context) {
95
- return reject(new Error("Could not get canvas context"));
95
+ return reject(new Error(`${engine_1.errorPrefix} Could not get canvas context`));
96
96
  }
97
97
  context.drawImage(image, 0, 0, image.width, image.height, 0, 0, canvas.width, canvas.height);
98
98
  resolve(getCanvasImageData(context, canvas, offset));
@@ -107,7 +107,7 @@
107
107
  if (!text || !context) {
108
108
  return;
109
109
  }
110
- const lines = text.split(linesOptions.separator), fontSize = typeof font.size === "number" ? `${font.size}px` : font.size, linesData = [];
110
+ const lines = text.split(linesOptions.separator), fontSize = (0, engine_1.isNumber)(font.size) ? `${font.size}px` : font.size, linesData = [];
111
111
  let maxWidth = 0, totalHeight = 0;
112
112
  for (const line of lines) {
113
113
  context.font = `${font.style || ""} ${font.variant || ""} ${font.weight || ""} ${fontSize} ${font.family}`;