beautiful-backgrounds 0.0.6 → 0.0.7
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 +1 @@
|
|
|
1
|
-
!function(t,i){"object"==typeof exports&&"undefined"!=typeof module?i(exports):"function"==typeof define&&define.amd?define(["exports"],i):i((t="undefined"!=typeof globalThis?globalThis:t||self).version={})}(this,(function(t){"use strict";class i extends HTMLElement{constructor(){super(),this.interpolationDuration=1e3,this.interpolationStep=100,this.fullyInitialized=!1,this.shadowR=this.attachShadow({mode:"open"}),this.shadowR.innerHTML="\n <style>\n :host {\n display: flex;\n }\n </style>\n ",this.lastTime=0,this.fps=30,this.timer=0,this.resizeDebounceTimer=null,this.canvas=document.createElement("canvas"),this.ctx=this.canvas.getContext("2d"),this.backgroundColor="0, 0, 0"}debouncedResizeCanvas(){clearTimeout(this.resizeDebounceTimer),this.resizeDebounceTimer=setTimeout((()=>{this.resizeCanvas()}),1e3)}startAnimation(t){const i=s=>{const a=s-this.lastTime;this.lastTime=s,this.timer>1e3/this.fps?(t(),this.timer=0):this.timer+=a,this.animationFrameId=requestAnimationFrame(i)};this.animationFrameId=requestAnimationFrame(i)}interpolateWithThrottle(t,i,s,a,e){const n=Math.ceil(s/a),r=(i-t)/n;let o=t,h=0,l=!0,d=null;!function t(){h<s&&l?(e(o,!1),o+=r,h+=a,d=window.setTimeout(t,a)):l&&(e(i,!0),l=!1,d&&window.clearTimeout(d))}()}triggerInterpolation(t,i,s){const a=Number(i),e=Number(s),n=this.getPropNameCamelCased(t);var r,o;a!==e&&this.fullyInitialized?(r=this,o=n,Object.prototype.hasOwnProperty.call(r,o)&&this.interpolateWithThrottle(i?a:this[n],e,this.interpolationDuration,this.interpolationStep,(t=>{this[n]=t}))):this[n]=e}setValues(t,i,s,a){if(t.includes(i)){this[this.getPropNameCamelCased(i)]=a}t.includes(i)||this.triggerInterpolation(i,s,a)}getPropNameCamelCased(t){return t.substring(5).replace(/-(.)/g,((t,i)=>i.toUpperCase()))}resizeCanvas(){const t=Number.parseInt(this.getAttribute("data-width"))||this.parentElement.clientWidth,i=Number.parseInt(this.getAttribute("data-height"))||this.parentElement.clientHeight;t===this.width&&i===this.height||(this.width=t,this.height=i,this.canvas.width=t,this.canvas.height=i,this.canvas.style.backgroundColor=`rgb(${this.backgroundColor})`),this.fullyInitialized=!0}}class s{constructor(t,i,s,a,e,n){this.size=t,this.speed=i,this.color=s,this.radius=a,this.angle=e,this.lifespan=n,this.x=0,this.y=0,this.born=Date.now(),this.fadeInDuration=.25*this.lifespan,this.fadeOutDuration=.5*this.lifespan}update(t,i){this.angle+=this.speed,this.x=t+Math.cos(this.angle)*this.radius,this.y=i+Math.sin(this.angle)*this.radius}draw(t){t.beginPath(),t.arc(this.x,this.y,this.size,0,2*Math.PI,!1),t.fillStyle=this.color,t.globalAlpha=this.calculateOpacity(),t.shadowBlur=10,t.shadowColor=this.color,t.fill(),t.globalAlpha=1,t.shadowBlur=0}isDead(){return Date.now()-this.born>this.lifespan}calculateOpacity(){const t=Date.now()-this.born;return t<this.fadeInDuration?t/this.fadeInDuration:t>this.lifespan-this.fadeOutDuration?(this.lifespan-t)/this.fadeOutDuration:1}}function a(t,i){return Math.random()*(i-t)+t}function e(t,i){return t=Math.ceil(t),i=Math.floor(i),Math.floor(Math.random()*(i-t+1)+t)}function n(t,i,s,a,e){t<i&&(t=i),t>s&&(t=s);return(t-i)/(s-i)*(e-a)+a}function r(t=[0,360],i=[0,100],s=[40,60]){return`hsl(${e(t[0],t[1])}, ${e(i[0],i[1])}%, ${e(s[0],s[1])}%)`}class o extends i{constructor(){super(),this.ignoredProps=["data-characters","data-background-color"],this.backgroundColor="0, 0, 0",this.starSizeMin=.5,this.starSizeMax=1.5,this.starSpeedMin=.025,this.starSpeedMax=.05,this.starColorHueStart=30,this.starColorHueEnd=75,this.starColorSaturationStart=100,this.starColorSaturationEnd=100,this.starColorLightnessStart=50,this.starColorLightnessEnd=50,this.starRadiusMin=1,this.starRadiusMax=100,this.starLifespanMin=1e3,this.starLifespanMax=1e4,this.numStars=1e3,this.trailOpacity=.1}connectedCallback(){this.shadowR.appendChild(this.canvas),this.resizeCanvas(),this.initialize(),this.startAnimation(this.animation.bind(this)),window.addEventListener("resize",this.debouncedResizeCanvas.bind(this))}disconnectedCallback(){window.removeEventListener("resize",this.debouncedResizeCanvas.bind(this)),null!==this.animationFrameId&&window.cancelAnimationFrame(this.animationFrameId)}initialize(){this.stars=[],this.starRadiusMax=Math.max(this.width,this.height)/2,this.animationFrameId=null;for(let t=0;t<this.numStars;t++)this.stars.push(this.createStar())}animation(){for(this.ctx.fillStyle=`rgba(${this.backgroundColor}, ${this.trailOpacity})`,this.ctx.fillRect(0,0,this.width,this.height),this.stars=this.stars.filter((t=>!t.isDead()));this.stars.length<this.numStars;)this.stars.push(this.createStar());this.stars.forEach((t=>{t.update(this.width/2,this.height/2),t.draw(this.ctx)}))}createStar(){const t=a(this.starSizeMin,this.starSizeMax),i=a(n(this.starSpeedMin,-1,1,-.04,.04),n(this.starSpeedMax,-1,1,-.04,.04)),e=a(this.starLifespanMin,this.starLifespanMax),o=r([this.starColorHueStart,this.starColorHueEnd],[this.starColorSaturationStart,this.starColorSaturationEnd],[this.starColorLightnessStart,this.starColorLightnessEnd]),h=a(0,2*Math.PI),l=this.starRadiusMax-this.starRadiusMin,d=Math.sqrt(Math.random())*l+this.starRadiusMin;return new s(t,i,o,d,h,e)}attributeChangedCallback(t,i,s){this.setValues(this.ignoredProps,t,i,s)}}o.observedAttributes=["data-fps","data-background-color","data-star-size-min","data-star-size-max","data-star-speed-min","data-star-speed-max","data-star-color-hue-start","data-star-color-hue-end","data-star-color-saturation-start","data-star-color-saturation-end","data-star-color-lightness-start","data-star-color-lightness-end","data-star-radius-min","data-star-radius-max","data-star-lifespan-min","data-star-lifespan-max","data-num-stars"],window.customElements.get("bb-star-trail")||window.customElements.define("bb-star-trail",o);class h{constructor(t,i
|
|
1
|
+
!function(t,i){"object"==typeof exports&&"undefined"!=typeof module?i(exports):"function"==typeof define&&define.amd?define(["exports"],i):i((t="undefined"!=typeof globalThis?globalThis:t||self).version={})}(this,(function(t){"use strict";class i extends HTMLElement{constructor(){super(),this.interpolationDuration=1e3,this.interpolationStep=100,this.fullyInitialized=!1,this.shadowR=this.attachShadow({mode:"open"}),this.shadowR.innerHTML="\n <style>\n :host {\n display: flex;\n }\n </style>\n ",this.lastTime=0,this.fps=30,this.timer=0,this.resizeDebounceTimer=null,this.canvas=document.createElement("canvas"),this.ctx=this.canvas.getContext("2d"),this.backgroundColor="0, 0, 0"}debouncedResizeCanvas(){clearTimeout(this.resizeDebounceTimer),this.resizeDebounceTimer=setTimeout((()=>{this.resizeCanvas()}),1e3)}startAnimation(t){const i=s=>{const a=s-this.lastTime;this.lastTime=s,this.timer>1e3/this.fps?(t(),this.timer=0):this.timer+=a,this.animationFrameId=requestAnimationFrame(i)};this.animationFrameId=requestAnimationFrame(i)}interpolateWithThrottle(t,i,s,a,e){const n=Math.ceil(s/a),r=(i-t)/n;let o=t,h=0,l=!0,d=null;!function t(){h<s&&l?(e(o,!1),o+=r,h+=a,d=window.setTimeout(t,a)):l&&(e(i,!0),l=!1,d&&window.clearTimeout(d))}()}triggerInterpolation(t,i,s){const a=Number(i),e=Number(s),n=this.getPropNameCamelCased(t);var r,o;a!==e&&this.fullyInitialized?(r=this,o=n,Object.prototype.hasOwnProperty.call(r,o)&&this.interpolateWithThrottle(i?a:this[n],e,this.interpolationDuration,this.interpolationStep,(t=>{this[n]=t}))):this[n]=e}setValues(t,i,s,a){if(t.includes(i)){this[this.getPropNameCamelCased(i)]=a}t.includes(i)||this.triggerInterpolation(i,s,a)}getPropNameCamelCased(t){return t.substring(5).replace(/-(.)/g,((t,i)=>i.toUpperCase()))}resizeCanvas(){const t=Number.parseInt(this.getAttribute("data-width"))||this.parentElement.clientWidth,i=Number.parseInt(this.getAttribute("data-height"))||this.parentElement.clientHeight;t===this.width&&i===this.height||(this.width=t,this.height=i,this.canvas.width=t,this.canvas.height=i,this.canvas.style.backgroundColor=`rgb(${this.backgroundColor})`),this.fullyInitialized=!0}}class s{constructor(t,i,s,a,e,n){this.size=t,this.speed=i,this.color=s,this.radius=a,this.angle=e,this.lifespan=n,this.x=0,this.y=0,this.born=Date.now(),this.fadeInDuration=.25*this.lifespan,this.fadeOutDuration=.5*this.lifespan}update(t,i){this.angle+=this.speed,this.x=t+Math.cos(this.angle)*this.radius,this.y=i+Math.sin(this.angle)*this.radius}draw(t){t.beginPath(),t.arc(this.x,this.y,this.size,0,2*Math.PI,!1),t.fillStyle=this.color,t.globalAlpha=this.calculateOpacity(),t.shadowBlur=10,t.shadowColor=this.color,t.fill(),t.globalAlpha=1,t.shadowBlur=0}isDead(){return Date.now()-this.born>this.lifespan}calculateOpacity(){const t=Date.now()-this.born;return t<this.fadeInDuration?t/this.fadeInDuration:t>this.lifespan-this.fadeOutDuration?(this.lifespan-t)/this.fadeOutDuration:1}}function a(t,i){return Math.random()*(i-t)+t}function e(t,i){return t=Math.ceil(t),i=Math.floor(i),Math.floor(Math.random()*(i-t+1)+t)}function n(t,i,s,a,e){t<i&&(t=i),t>s&&(t=s);return(t-i)/(s-i)*(e-a)+a}function r(t=[0,360],i=[0,100],s=[40,60]){return`hsl(${e(t[0],t[1])}, ${e(i[0],i[1])}%, ${e(s[0],s[1])}%)`}class o extends i{constructor(){super(),this.ignoredProps=["data-characters","data-background-color"],this.backgroundColor="0, 0, 0",this.starSizeMin=.5,this.starSizeMax=1.5,this.starSpeedMin=.025,this.starSpeedMax=.05,this.starColorHueStart=30,this.starColorHueEnd=75,this.starColorSaturationStart=100,this.starColorSaturationEnd=100,this.starColorLightnessStart=50,this.starColorLightnessEnd=50,this.starRadiusMin=1,this.starRadiusMax=100,this.starLifespanMin=1e3,this.starLifespanMax=1e4,this.numStars=1e3,this.trailOpacity=.1}connectedCallback(){this.shadowR.appendChild(this.canvas),this.resizeCanvas(),this.initialize(),this.startAnimation(this.animation.bind(this)),window.addEventListener("resize",this.debouncedResizeCanvas.bind(this))}disconnectedCallback(){window.removeEventListener("resize",this.debouncedResizeCanvas.bind(this)),null!==this.animationFrameId&&window.cancelAnimationFrame(this.animationFrameId)}initialize(){this.stars=[],this.starRadiusMax=Math.max(this.width,this.height)/2,this.animationFrameId=null;for(let t=0;t<this.numStars;t++)this.stars.push(this.createStar())}animation(){for(this.ctx.fillStyle=`rgba(${this.backgroundColor}, ${this.trailOpacity})`,this.ctx.fillRect(0,0,this.width,this.height),this.stars=this.stars.filter((t=>!t.isDead()));this.stars.length<this.numStars;)this.stars.push(this.createStar());this.stars.forEach((t=>{t.update(this.width/2,this.height/2),t.draw(this.ctx)}))}createStar(){const t=a(this.starSizeMin,this.starSizeMax),i=a(n(this.starSpeedMin,-1,1,-.04,.04),n(this.starSpeedMax,-1,1,-.04,.04)),e=a(this.starLifespanMin,this.starLifespanMax),o=r([this.starColorHueStart,this.starColorHueEnd],[this.starColorSaturationStart,this.starColorSaturationEnd],[this.starColorLightnessStart,this.starColorLightnessEnd]),h=a(0,2*Math.PI),l=this.starRadiusMax-this.starRadiusMin,d=Math.sqrt(Math.random())*l+this.starRadiusMin;return new s(t,i,o,d,h,e)}attributeChangedCallback(t,i,s){this.setValues(this.ignoredProps,t,i,s)}}o.observedAttributes=["data-fps","data-background-color","data-star-size-min","data-star-size-max","data-star-speed-min","data-star-speed-max","data-star-color-hue-start","data-star-color-hue-end","data-star-color-saturation-start","data-star-color-saturation-end","data-star-color-lightness-start","data-star-color-lightness-end","data-star-radius-min","data-star-radius-max","data-star-lifespan-min","data-star-lifespan-max","data-num-stars"],window.customElements.get("bb-star-trail")||window.customElements.define("bb-star-trail",o);class h{constructor(t,i){this.x=t,this.y=i}update(t,i,s){this.y*s>t&&Math.random()>i?this.y=0:this.y+=1}draw(t,i,s,a){t.fillStyle=s,t.fillText(i,this.x*a,this.y*a)}}class l extends i{constructor(){super(),this.ignoredProps=["data-characters","data-background-color"],this.fontSize=24,this.fontColorHueStart=60,this.fontColorHueEnd=this.fontColorHueStart+90,this.fontColorSaturationStart=90,this.fontColorSaturationEnd=100,this.fontColorLightnessStart=50,this.fontColorLightnessEnd=50,this.randomness=.975,this.trailOpacity=.1,this.characters='ヲアウエオカキケコサシスセソタツテナニヌネハヒホマミムメモヤユラリワ13579ヲイクコソチトノフヤヨルレロンZ:."¦=*+-<>|リçδ╘'}connectedCallback(){this.shadowR.appendChild(this.canvas),this.resizeCanvas(),this.initialize(),this.startAnimation(this.animation.bind(this)),window.addEventListener("resize",this.debouncedResizeCanvas.bind(this))}disconnectedCallback(){window.removeEventListener("resize",this.debouncedResizeCanvas.bind(this)),null!==this.animationFrameId&&window.cancelAnimationFrame(this.animationFrameId)}initialize(){this.symbols=[],this.createSymbols()}createSymbols(){this.columns=Math.floor(this.width/this.fontSize);for(let t=0;t<this.columns;t++)this.symbols[t]=this.createSymbol(t)}updateSymbols(){const t=Math.floor(this.width/this.fontSize);for(let i=this.symbols.length;i<t;i++)this.symbols[i]=this.createSymbol(i);this.symbols.length>t&&(this.symbols.length=t)}animation(){this.updateSymbols(),this.ctx.fillStyle=`rgba(${this.backgroundColor}, ${this.trailOpacity})`,this.ctx.fillRect(0,0,this.width,this.height),this.ctx.textAlign="center",this.ctx.font=`${this.fontSize}px monospace`,this.symbols.forEach((t=>{t.draw(this.ctx,this.characters.charAt(Math.floor(Math.random()*this.characters.length)),r([this.fontColorHueStart,this.fontColorHueEnd],[this.fontColorSaturationStart,this.fontColorSaturationEnd],[this.fontColorLightnessStart,this.fontColorLightnessEnd]),this.fontSize),t.update(this.height,this.randomness,this.fontSize)}))}createSymbol(t){return new h(t,e(0,-this.height/this.fontSize))}attributeChangedCallback(t,i,s){this.setValues(this.ignoredProps,t,i,s)}}l.observedAttributes=["data-fps","data-characters","data-randomness","data-font-size","data-font-color-hue-start","data-font-color-hue-end","data-font-color-saturation-start","data-font-color-saturation-end","data-font-color-lightness-start","data-font-color-lightness-end","data-background-color"],window.customElements.get("bb-digital-rain")||window.customElements.define("bb-digital-rain",l),t.BbDigitalRain=l,t.BbStarTrail=o}));
|
|
@@ -18,6 +18,8 @@ export declare class BbDigitalRain extends BB {
|
|
|
18
18
|
protected connectedCallback(): void;
|
|
19
19
|
protected disconnectedCallback(): void;
|
|
20
20
|
protected initialize(): void;
|
|
21
|
+
private createSymbols;
|
|
22
|
+
private updateSymbols;
|
|
21
23
|
protected animation(): void;
|
|
22
24
|
protected createSymbol(index: number): Symbol;
|
|
23
25
|
attributeChangedCallback(name: string, oldValue: string, newValue: string): void;
|
|
@@ -32,23 +32,36 @@ export class BbDigitalRain extends BB {
|
|
|
32
32
|
}
|
|
33
33
|
initialize() {
|
|
34
34
|
this.symbols = [];
|
|
35
|
+
this.createSymbols();
|
|
36
|
+
}
|
|
37
|
+
createSymbols() {
|
|
35
38
|
this.columns = Math.floor(this.width / this.fontSize);
|
|
36
39
|
for (let i = 0; i < this.columns; i++) {
|
|
37
40
|
this.symbols[i] = this.createSymbol(i);
|
|
38
41
|
}
|
|
39
42
|
}
|
|
43
|
+
updateSymbols() {
|
|
44
|
+
const currentColumns = Math.floor(this.width / this.fontSize);
|
|
45
|
+
for (let i = this.symbols.length; i < currentColumns; i++) {
|
|
46
|
+
this.symbols[i] = this.createSymbol(i);
|
|
47
|
+
}
|
|
48
|
+
if (this.symbols.length > currentColumns) {
|
|
49
|
+
this.symbols.length = currentColumns;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
40
52
|
animation() {
|
|
53
|
+
this.updateSymbols();
|
|
41
54
|
this.ctx.fillStyle = `rgba(${this.backgroundColor}, ${this.trailOpacity})`;
|
|
42
55
|
this.ctx.fillRect(0, 0, this.width, this.height);
|
|
43
56
|
this.ctx.textAlign = 'center';
|
|
44
57
|
this.ctx.font = `${this.fontSize}px monospace`;
|
|
45
58
|
this.symbols.forEach((symbol) => {
|
|
46
|
-
symbol.draw(this.ctx, this.characters.charAt(Math.floor(Math.random() * this.characters.length)));
|
|
47
|
-
symbol.update(this.height, this.randomness);
|
|
59
|
+
symbol.draw(this.ctx, this.characters.charAt(Math.floor(Math.random() * this.characters.length)), randomColor([this.fontColorHueStart, this.fontColorHueEnd], [this.fontColorSaturationStart, this.fontColorSaturationEnd], [this.fontColorLightnessStart, this.fontColorLightnessEnd]), this.fontSize);
|
|
60
|
+
symbol.update(this.height, this.randomness, this.fontSize);
|
|
48
61
|
});
|
|
49
62
|
}
|
|
50
63
|
createSymbol(index) {
|
|
51
|
-
return new Symbol(index, getRandomInt(0, -this.height / this.fontSize)
|
|
64
|
+
return new Symbol(index, getRandomInt(0, -this.height / this.fontSize));
|
|
52
65
|
}
|
|
53
66
|
attributeChangedCallback(name, oldValue, newValue) {
|
|
54
67
|
this.setValues(this.ignoredProps, name, oldValue, newValue);
|
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
export default class Symbol {
|
|
2
2
|
protected x: number;
|
|
3
3
|
protected y: number;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
update(height: number, randomness: number): void;
|
|
8
|
-
draw(ctx: CanvasRenderingContext2D, text: string): void;
|
|
4
|
+
constructor(x: number, y: number);
|
|
5
|
+
update(height: number, randomness: number, fontSize: number): void;
|
|
6
|
+
draw(ctx: CanvasRenderingContext2D, text: string, color: string, fontSize: number): void;
|
|
9
7
|
}
|
|
@@ -1,20 +1,18 @@
|
|
|
1
1
|
export default class Symbol {
|
|
2
|
-
constructor(x, y
|
|
2
|
+
constructor(x, y) {
|
|
3
3
|
this.x = x;
|
|
4
4
|
this.y = y;
|
|
5
|
-
this.fontSize = fontSize;
|
|
6
|
-
this.color = color;
|
|
7
5
|
}
|
|
8
|
-
update(height, randomness) {
|
|
9
|
-
if (this.y *
|
|
6
|
+
update(height, randomness, fontSize) {
|
|
7
|
+
if (this.y * fontSize > height && Math.random() > randomness) {
|
|
10
8
|
this.y = 0;
|
|
11
9
|
}
|
|
12
10
|
else {
|
|
13
11
|
this.y += 1;
|
|
14
12
|
}
|
|
15
13
|
}
|
|
16
|
-
draw(ctx, text) {
|
|
17
|
-
ctx.fillStyle =
|
|
18
|
-
ctx.fillText(text, this.x *
|
|
14
|
+
draw(ctx, text, color, fontSize) {
|
|
15
|
+
ctx.fillStyle = color;
|
|
16
|
+
ctx.fillText(text, this.x * fontSize, this.y * fontSize);
|
|
19
17
|
}
|
|
20
18
|
}
|