@tsparticles/plugin-canvas-mask 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.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 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})()));
2
+ !function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t(require("@tsparticles/engine"));else if("function"==typeof define&&define.amd)define(["@tsparticles/engine"],t);else{var r="object"==typeof exports?t(require("@tsparticles/engine")):t(e.window);for(var n in r)("object"==typeof exports?exports:e)[n]=r[n]}}(this,(e=>(()=>{var t,r,n={533:t=>{t.exports=e}},o={};function i(e){var t=o[e];if(void 0!==t)return t.exports;var r=o[e]={exports:{}};return n[e](r,r.exports,i),r.exports}i.m=n,i.d=(e,t)=>{for(var r in t)i.o(t,r)&&!i.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:t[r]})},i.f={},i.e=e=>Promise.all(Object.keys(i.f).reduce(((t,r)=>(i.f[r](e,t),t)),[])),i.u=e=>e+".min.js",i.g=function(){if("object"==typeof globalThis)return globalThis;try{return this||new Function("return this")()}catch(e){if("object"==typeof window)return window}}(),i.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),t={},r="@tsparticles/plugin-canvas-mask:",i.l=(e,n,o,a)=>{if(t[e])t[e].push(n);else{var s,l;if(void 0!==o)for(var c=document.getElementsByTagName("script"),u=0;u<c.length;u++){var p=c[u];if(p.getAttribute("src")==e||p.getAttribute("data-webpack")==r+o){s=p;break}}s||(l=!0,(s=document.createElement("script")).charset="utf-8",s.timeout=120,i.nc&&s.setAttribute("nonce",i.nc),s.setAttribute("data-webpack",r+o),s.src=e),t[e]=[n];var d=(r,n)=>{s.onerror=s.onload=null,clearTimeout(f);var o=t[e];if(delete t[e],s.parentNode&&s.parentNode.removeChild(s),o&&o.forEach((e=>e(n))),r)return r(n)},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),l&&document.head.appendChild(s)}},i.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},(()=>{var e;i.g.importScripts&&(e=i.g.location+"");var t=i.g.document;if(!e&&t&&(t.currentScript&&(e=t.currentScript.src),!e)){var r=t.getElementsByTagName("script");if(r.length)for(var n=r.length-1;n>-1&&!e;)e=r[n--].src}if(!e)throw new Error("Automatic publicPath is not supported in this browser");e=e.replace(/#.*$/,"").replace(/\?.*$/,"").replace(/\/[^\/]+$/,"/"),i.p=e})(),(()=>{var e={592:0};i.f.j=(t,r)=>{var n=i.o(e,t)?e[t]:void 0;if(0!==n)if(n)r.push(n[2]);else{var o=new Promise(((r,o)=>n=e[t]=[r,o]));r.push(n[2]=o);var a=i.p+i.u(t),s=new Error;i.l(a,(r=>{if(i.o(e,t)&&(0!==(n=e[t])&&(e[t]=void 0),n)){var o=r&&("load"===r.type?"missing":r.type),a=r&&r.target&&r.target.src;s.message="Loading chunk "+t+" failed.\n("+o+": "+a+")",s.name="ChunkLoadError",s.type=o,s.request=a,n[1](s)}}),"chunk-"+t,t)}};var t=(t,r)=>{var n,o,a=r[0],s=r[1],l=r[2],c=0;if(a.some((t=>0!==e[t]))){for(n in s)i.o(s,n)&&(i.m[n]=s[n]);if(l)l(i)}for(t&&t(r);c<a.length;c++)o=a[c],i.o(e,o)&&e[o]&&e[o][0](),e[o]=0},r=this.webpackChunk_tsparticles_plugin_canvas_mask=this.webpackChunk_tsparticles_plugin_canvas_mask||[];r.forEach(t.bind(null,0)),r.push=t.bind(null,r.push.bind(r))})();var a={};return(()=>{async function e(e,t=!0){const{CanvasMaskPlugin:r}=await i.e(262).then(i.bind(i,262));await e.addPlugin(new r,t)}i.r(a),i.d(a,{loadCanvasMaskPlugin:()=>e})})(),a})()));
@@ -1 +1 @@
1
- /*! tsParticles Canvas Mask Plugin v3.0.3 by Matteo Bruni */
1
+ /*! tsParticles Canvas Mask Plugin v3.2.0 by Matteo Bruni */
@@ -1,8 +1,7 @@
1
- import type { Engine, IContainerPlugin } from "@tsparticles/engine";
2
1
  import type { CanvasMaskContainer } from "./types.js";
2
+ import type { IContainerPlugin } from "@tsparticles/engine";
3
3
  export declare class CanvasMaskInstance implements IContainerPlugin {
4
4
  private readonly _container;
5
- private readonly _engine;
6
- constructor(container: CanvasMaskContainer, engine: Engine);
5
+ constructor(container: CanvasMaskContainer);
7
6
  init(): Promise<void>;
8
7
  }
@@ -0,0 +1,10 @@
1
+ import type { CanvasMaskOptions, ICanvasMaskOptions } from "./types.js";
2
+ import { type Container, type IPlugin, type RecursivePartial } from "@tsparticles/engine";
3
+ import type { CanvasMaskInstance } from "./CanvasMaskInstance.js";
4
+ export declare class CanvasMaskPlugin implements IPlugin {
5
+ readonly id: string;
6
+ constructor();
7
+ getPlugin(container: Container): Promise<CanvasMaskInstance>;
8
+ loadOptions(options: CanvasMaskOptions, source?: RecursivePartial<ICanvasMaskOptions>): void;
9
+ needsPlugin(options?: RecursivePartial<ICanvasMaskOptions>): boolean;
10
+ }
@@ -1,4 +1,4 @@
1
- import type { IOptionLoader, IRgba, RecursivePartial } from "@tsparticles/engine";
1
+ import { type IOptionLoader, type IRgba, type RecursivePartial } from "@tsparticles/engine";
2
2
  import type { ICanvasMaskPixels } from "../Interfaces/ICanvasMaskPixels.js";
3
3
  export declare class CanvasMaskPixels implements ICanvasMaskPixels, IOptionLoader<ICanvasMaskPixels> {
4
4
  filter: (pixel: IRgba) => boolean;
package/types/utils.d.ts CHANGED
@@ -1,12 +1,12 @@
1
1
  import { type Container, type ICoordinates, type IDimension, type IRgba } from "@tsparticles/engine";
2
2
  import type { ICanvasMaskOverride } from "./Options/Interfaces/ICanvasMaskOverride.js";
3
3
  import type { TextMask } from "./Options/Classes/TextMask.js";
4
- export type CanvasPixelData = {
4
+ export interface CanvasPixelData {
5
5
  height: number;
6
6
  pixels: IRgba[][];
7
7
  width: number;
8
- };
9
- export declare function addParticlesFromCanvasPixels(container: Container, data: CanvasPixelData, position: ICoordinates, scale: number, override: ICanvasMaskOverride, filter: (pixel: IRgba) => boolean): void;
8
+ }
9
+ export declare function addParticlesFromCanvasPixels(container: Container, data: CanvasPixelData, position: ICoordinates, scale: number, override: ICanvasMaskOverride, filter: (pixel: IRgba) => boolean): Promise<void>;
10
10
  export declare function getCanvasImageData(ctx: CanvasRenderingContext2D, size: IDimension, offset: number, clear?: boolean): CanvasPixelData;
11
11
  export declare function getImageData(src: string, offset: number): Promise<CanvasPixelData>;
12
12
  export declare function getTextData(textOptions: TextMask, offset: number): CanvasPixelData | undefined;
@@ -12,9 +12,8 @@
12
12
  exports.CanvasMaskInstance = void 0;
13
13
  const utils_js_1 = require("./utils.js");
14
14
  class CanvasMaskInstance {
15
- constructor(container, engine) {
15
+ constructor(container) {
16
16
  this._container = container;
17
- this._engine = engine;
18
17
  }
19
18
  async init() {
20
19
  const container = this._container, options = container.actualOptions.canvasMask;
@@ -42,8 +41,8 @@
42
41
  }
43
42
  pixelData = data;
44
43
  }
45
- else if (options.element || options.selector) {
46
- const canvas = options.element || (options.selector && document.querySelector(options.selector));
44
+ else if (options.element ?? options.selector) {
45
+ const canvas = options.element ?? (options.selector && document.querySelector(options.selector));
47
46
  if (!canvas) {
48
47
  return;
49
48
  }
@@ -53,7 +52,7 @@
53
52
  }
54
53
  pixelData = (0, utils_js_1.getCanvasImageData)(context, canvas, offset);
55
54
  }
56
- (0, utils_js_1.addParticlesFromCanvasPixels)(container, pixelData, options.position, options.scale, options.override, options.pixels.filter);
55
+ await (0, utils_js_1.addParticlesFromCanvasPixels)(container, pixelData, options.position, options.scale, options.override, options.pixels.filter);
57
56
  }
58
57
  }
59
58
  exports.CanvasMaskInstance = CanvasMaskInstance;
@@ -0,0 +1,61 @@
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
+ };
24
+ (function (factory) {
25
+ if (typeof module === "object" && typeof module.exports === "object") {
26
+ var v = factory(require, exports);
27
+ if (v !== undefined) module.exports = v;
28
+ }
29
+ else if (typeof define === "function" && define.amd) {
30
+ define(["require", "exports", "./Options/Classes/CanvasMask.js"], factory);
31
+ }
32
+ })(function (require, exports) {
33
+ "use strict";
34
+ var __syncRequire = typeof module === "object" && typeof module.exports === "object";
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.CanvasMaskPlugin = void 0;
37
+ const CanvasMask_js_1 = require("./Options/Classes/CanvasMask.js");
38
+ class CanvasMaskPlugin {
39
+ constructor() {
40
+ this.id = "canvasMask";
41
+ }
42
+ async getPlugin(container) {
43
+ const { CanvasMaskInstance } = await (__syncRequire ? Promise.resolve().then(() => __importStar(require("./CanvasMaskInstance.js"))) : new Promise((resolve_1, reject_1) => { require(["./CanvasMaskInstance.js"], resolve_1, reject_1); }).then(__importStar));
44
+ return new CanvasMaskInstance(container);
45
+ }
46
+ loadOptions(options, source) {
47
+ if (!this.needsPlugin(options) && !this.needsPlugin(source)) {
48
+ return;
49
+ }
50
+ let canvasMaskOptions = options.canvasMask;
51
+ if (canvasMaskOptions?.load === undefined) {
52
+ options.canvasMask = canvasMaskOptions = new CanvasMask_js_1.CanvasMask();
53
+ }
54
+ canvasMaskOptions.load(source?.canvasMask);
55
+ }
56
+ needsPlugin(options) {
57
+ return options?.canvasMask?.enable ?? false;
58
+ }
59
+ }
60
+ exports.CanvasMaskPlugin = CanvasMaskPlugin;
61
+ });
@@ -11,9 +11,10 @@
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.CanvasMaskPixels = void 0;
13
13
  const engine_1 = require("@tsparticles/engine");
14
+ const minAlpha = 0;
14
15
  class CanvasMaskPixels {
15
16
  constructor() {
16
- this.filter = (pixel) => pixel.a > 0;
17
+ this.filter = (pixel) => pixel.a > minAlpha;
17
18
  this.offset = 4;
18
19
  }
19
20
  load(data) {
package/umd/index.js CHANGED
@@ -1,41 +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", "./Options/Classes/CanvasMask.js", "./CanvasMaskInstance.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.loadCanvasMaskPlugin = void 0;
13
- const CanvasMask_js_1 = require("./Options/Classes/CanvasMask.js");
14
- const CanvasMaskInstance_js_1 = require("./CanvasMaskInstance.js");
15
- class CanvasMaskPlugin {
16
- constructor(engine) {
17
- this.id = "canvasMask";
18
- this._engine = engine;
19
- }
20
- getPlugin(container) {
21
- return new CanvasMaskInstance_js_1.CanvasMaskInstance(container, this._engine);
22
- }
23
- loadOptions(options, source) {
24
- if (!this.needsPlugin(options) && !this.needsPlugin(source)) {
25
- return;
26
- }
27
- let canvasMaskOptions = options.canvasMask;
28
- if (canvasMaskOptions?.load === undefined) {
29
- options.canvasMask = canvasMaskOptions = new CanvasMask_js_1.CanvasMask();
30
- }
31
- canvasMaskOptions.load(source?.canvasMask);
32
- }
33
- needsPlugin(options) {
34
- return options?.canvasMask?.enable ?? false;
35
- }
36
- }
37
37
  async function loadCanvasMaskPlugin(engine, refresh = true) {
38
- await engine.addPlugin(new CanvasMaskPlugin(engine), refresh);
38
+ const { CanvasMaskPlugin } = await (__syncRequire ? Promise.resolve().then(() => __importStar(require("./CanvasMaskPlugin.js"))) : new Promise((resolve_1, reject_1) => { require(["./CanvasMaskPlugin.js"], resolve_1, reject_1); }).then(__importStar));
39
+ await engine.addPlugin(new CanvasMaskPlugin(), refresh);
39
40
  }
40
41
  exports.loadCanvasMaskPlugin = loadCanvasMaskPlugin;
41
42
  });
package/umd/utils.js CHANGED
@@ -11,15 +11,19 @@
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.getTextData = exports.getImageData = exports.getCanvasImageData = exports.addParticlesFromCanvasPixels = void 0;
13
13
  const engine_1 = require("@tsparticles/engine");
14
- function addParticlesFromCanvasPixels(container, data, position, scale, override, filter) {
14
+ const half = 0.5, origin = {
15
+ x: 0,
16
+ y: 0,
17
+ }, defaultWidth = 0;
18
+ async function addParticlesFromCanvasPixels(container, data, position, scale, override, filter) {
15
19
  const { height, width } = data, numPixels = height * width, indexArray = shuffle(range(numPixels)), maxParticles = Math.min(numPixels, container.actualOptions.particles.number.value), canvasSize = container.canvas.size;
16
20
  let selectedPixels = 0;
17
21
  const positionOffset = {
18
- x: (canvasSize.width * position.x) / 100 - (width * scale) / 2,
19
- y: (canvasSize.height * position.y) / 100 - (height * scale) / 2,
22
+ x: (canvasSize.width * position.x) / engine_1.percentDenominator - width * scale * half,
23
+ y: (canvasSize.height * position.y) / engine_1.percentDenominator - height * scale * half,
20
24
  };
21
25
  while (selectedPixels < maxParticles && indexArray.length) {
22
- const nextIndex = indexArray.pop() || 0, pixelPos = {
26
+ const defaultIndex = 0, nextIndex = indexArray.pop() ?? defaultIndex, pixelPos = {
23
27
  x: nextIndex % width,
24
28
  y: Math.floor(nextIndex / width),
25
29
  }, pixel = data.pixels[pixelPos.y][pixelPos.x], shouldCreateParticle = filter(pixel);
@@ -40,15 +44,15 @@
40
44
  value: pixel.a,
41
45
  };
42
46
  }
43
- container.particles.addParticle(pos, pOptions);
47
+ await container.particles.addParticle(pos, pOptions);
44
48
  selectedPixels++;
45
49
  }
46
50
  }
47
51
  exports.addParticlesFromCanvasPixels = addParticlesFromCanvasPixels;
48
52
  function getCanvasImageData(ctx, size, offset, clear = true) {
49
- const imageData = ctx.getImageData(0, 0, size.width, size.height).data;
53
+ const imageData = ctx.getImageData(origin.x, origin.y, size.width, size.height).data;
50
54
  if (clear) {
51
- ctx.clearRect(0, 0, size.width, size.height);
55
+ ctx.clearRect(origin.x, origin.y, size.width, size.height);
52
56
  }
53
57
  const pixels = [];
54
58
  for (let i = 0; i < imageData.length; i += offset) {
@@ -59,11 +63,17 @@
59
63
  if (!pixels[pos.y]) {
60
64
  pixels[pos.y] = [];
61
65
  }
66
+ const indexesOffset = {
67
+ r: 0,
68
+ g: 1,
69
+ b: 2,
70
+ a: 3,
71
+ }, alphaFactor = 255;
62
72
  pixels[pos.y][pos.x] = {
63
- r: imageData[i],
64
- g: imageData[i + 1],
65
- b: imageData[i + 2],
66
- a: imageData[i + 3] / 255,
73
+ r: imageData[i + indexesOffset.r],
74
+ g: imageData[i + indexesOffset.g],
75
+ b: imageData[i + indexesOffset.b],
76
+ a: imageData[i + indexesOffset.a] / alphaFactor,
67
77
  };
68
78
  }
69
79
  return {
@@ -86,7 +96,7 @@
86
96
  if (!context) {
87
97
  return reject(new Error(`${engine_1.errorPrefix} Could not get canvas context`));
88
98
  }
89
- context.drawImage(image, 0, 0, image.width, image.height, 0, 0, canvas.width, canvas.height);
99
+ context.drawImage(image, origin.x, origin.y, image.width, image.height, origin.x, origin.y, canvas.width, canvas.height);
90
100
  resolve(getCanvasImageData(context, canvas, offset));
91
101
  };
92
102
  });
@@ -102,14 +112,14 @@
102
112
  const lines = text.split(linesOptions.separator), fontSize = (0, engine_1.isNumber)(font.size) ? `${font.size}px` : font.size, linesData = [];
103
113
  let maxWidth = 0, totalHeight = 0;
104
114
  for (const line of lines) {
105
- context.font = `${font.style || ""} ${font.variant || ""} ${font.weight || ""} ${fontSize} ${font.family}`;
115
+ context.font = `${font.style ?? ""} ${font.variant ?? ""} ${font.weight ?? ""} ${fontSize} ${font.family}`;
106
116
  const measure = context.measureText(line), lineData = {
107
117
  measure,
108
118
  text: line,
109
119
  height: measure.actualBoundingBoxAscent + measure.actualBoundingBoxDescent,
110
120
  width: measure.width,
111
121
  };
112
- maxWidth = Math.max(maxWidth || 0, lineData.width);
122
+ maxWidth = Math.max(maxWidth || defaultWidth, lineData.width);
113
123
  totalHeight += lineData.height + linesOptions.spacing;
114
124
  linesData.push(lineData);
115
125
  }
@@ -117,16 +127,17 @@
117
127
  canvas.height = totalHeight;
118
128
  let currentHeight = 0;
119
129
  for (const line of linesData) {
120
- context.font = `${font.style || ""} ${font.variant || ""} ${font.weight || ""} ${fontSize} ${font.family}`;
130
+ context.font = `${font.style ?? ""} ${font.variant ?? ""} ${font.weight ?? ""} ${fontSize} ${font.family}`;
121
131
  context.fillStyle = color;
122
- context.fillText(line.text, 0, currentHeight + line.measure.actualBoundingBoxAscent);
132
+ context.fillText(line.text, origin.x, currentHeight + line.measure.actualBoundingBoxAscent);
123
133
  currentHeight += line.height + linesOptions.spacing;
124
134
  }
125
135
  return getCanvasImageData(context, canvas, offset);
126
136
  }
127
137
  exports.getTextData = getTextData;
128
138
  function shuffle(array) {
129
- for (let currentIndex = array.length - 1; currentIndex >= 0; currentIndex--) {
139
+ const lengthOffset = 1, minIndex = 0;
140
+ for (let currentIndex = array.length - lengthOffset; currentIndex >= minIndex; currentIndex--) {
130
141
  const randomIndex = Math.floor((0, engine_1.getRandom)() * currentIndex);
131
142
  [array[currentIndex], array[randomIndex]] = [array[randomIndex], array[currentIndex]];
132
143
  }