UNPKG

@tsparticles/plugin-emitters-shape-canvas

Version:

tsParticles emitters shape canvas plugin

2 lines 5.23 kB
/*! For license information please see tsparticles.plugin.emitters.shape.canvas.min.js.LICENSE.txt */ !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)=>(()=>{var i={303:t=>{t.exports=e},526: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={};o.r(r),o.d(r,{loadEmittersShapeCanvas:()=>w});var n=o(526),a=o(303);const l=0,c=0;function h(t,e,i,s=!0){const o=t.getImageData(l,c,e.width,e.height).data;s&&t.clearRect(l,c,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]=[]);const a={r:0,g:1,b:2,a:3},l=255;r[n.y][n.x]={r:o[t+a.r],g:o[t+a.g],b:o[t+a.b],a:o[t+a.a]/l}}return{pixels:r,width:Math.min(...r.map((t=>t.length))),height:r.length}}class d extends n.EmitterShapeBase{constructor(t,e,i,s){super(t,e,i,s);const o=s.filter;let r=t=>t.a>0;if(void 0!==o)if((0,a.isString)(o)){if(Object.hasOwn(window,o)){const t=window[o];(0,a.isFunction)(t)&&(r=t)}}else r=o;this.filter=r,this.scale=s.scale,this.pixelData={pixels:[],height:0,width:0}}async init(){let t;const e=this.options,i=e.selector,s=e.pixels,o=e.image,r=e.element,n=e.text,d=s.offset;if(o){const e=o.src;if(!e)return;t=await function(t,e){const i=new Image;i.crossOrigin="Anonymous";const s=new Promise(((t,s)=>{i.onerror=s,i.onload=()=>{const o=document.createElement("canvas");o.width=i.width,o.height=i.height;const r=o.getContext("2d");if(!r)return s(new Error(`${a.errorPrefix} Could not get canvas context`));r.drawImage(i,l,c,i.width,i.height,l,c,o.width,o.height),t(h(r,o,e))}}));return i.src=t,s}(e,d)}else if(n){const e=function(t,e,i){const s=document.createElement("canvas"),o=s.getContext("2d"),{font:r,text:n,lines:c,color:d}=t;if(!n||!o)return;const f=n.split(c.separator),u=(0,a.isNumber)(r.size)?`${r.size}px`:r.size,p=[];let g=0,x=0;for(const t of f){o.font=`${r.style||""} ${r.variant||""} ${r.weight||""} ${u} ${r.family}`;const e=o.measureText(t),i={measure:e,text:t,height:e.actualBoundingBoxAscent+e.actualBoundingBoxDescent,width:e.width};g=Math.max(g||0,i.width),x+=i.height+c.spacing,p.push(i)}s.width=g,s.height=x;let m=0;for(const t of p)o.font=`${r.style||""} ${r.variant||""} ${r.weight||""} ${u} ${r.family}`,i?(o.fillStyle=d,o.fillText(t.text,l,m+t.measure.actualBoundingBoxAscent)):(o.strokeStyle=d,o.strokeText(t.text,l,m+t.measure.actualBoundingBoxAscent)),m+=t.height+c.spacing;return h(o,s,e)}(n,d,this.fill);if((0,a.isNull)(e))return;t=e}else if(r??i){const e=r??(i&&document.querySelector(i));if(!e)return;const s=e.getContext("2d");if(!s)return;t=h(s,e,d)}t&&(this.pixelData=t)}randomPosition(){const{height:t,width:e}=this.pixelData,i=this.pixelData,s=this.position,o=this.scale,r=s.x-e*o*.5,n=s.y-t*o*.5;for(let s=0;s<100;s++){const s=Math.floor((0,a.getRandom)()*e*t),l={x:s%e,y:Math.floor(s/e)},c=i.pixels[l.y][l.x];if(this.filter(c))return{position:{x:l.x*o+r,y:l.y*o+n},color:{...c},opacity:c.a}}return null}resize(t,e){super.resize(t,e)}}class f{constructor(){this.offset=4}load(t){(0,a.isNull)(t)||void 0!==t.offset&&(this.offset=t.offset)}}class u{constructor(){this.family="Verdana",this.size=32,this.style="",this.variant="",this.weight=""}load(t){(0,a.isNull)(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 p{constructor(){this.separator="\n",this.spacing=0}load(t){(0,a.isNull)(t)||(void 0!==t.separator&&(this.separator=t.separator),void 0!==t.spacing&&(this.spacing=t.spacing))}}class g{constructor(){this.color="#000000",this.font=new u,this.lines=new p,this.text=""}load(t){(0,a.isNull)(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 x{constructor(){this.filter=t=>t.a>0,this.pixels=new f,this.scale=1,this.selector="",this.text=new g}load(t){(0,a.isNull)(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 m{generate(t,e,i,s){const o=new x;return o.load(s),new d(t,e,i,o)}}async function w(t,e=!0){const i=t;i.checkVersion("3.9.1"),i.addEmitterShapeGenerator?.("canvas",new m),await i.refresh(e)}return r})()));