beautiful-backgrounds 0.0.2 → 0.0.5
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.
- package/README.md +64 -5
- package/dist/beautiful-backgrounds.min.js +1 -1
- package/dist/components/BeautifulBackground.d.ts +1 -0
- package/dist/components/BeautifulBackground.js +10 -1
- package/dist/components/DigitalRain.d.ts +26 -0
- package/dist/components/DigitalRain.js +78 -0
- package/dist/components/StarTrail.d.ts +17 -11
- package/dist/components/StarTrail.js +16 -5
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
# Beautiful Backgrounds
|
|
2
2
|
|
|
3
|
+
[![Build][build-badge]][build]
|
|
4
|
+
|
|
3
5
|
The "Beautiful Backgrounds" library offers a collection of customizable web components designed to enhance web pages with visually appealing animated backgrounds. Easy to integrate and use, these components are perfect for adding dynamic and interactive elements to your web applications.
|
|
4
6
|
|
|
7
|
+
[EXAMPLES][examples]
|
|
8
|
+
|
|
5
9
|
## Features
|
|
6
10
|
|
|
7
11
|
- **Multiple Background Components**: A variety of background components, each offering unique visual effects.
|
|
@@ -10,17 +14,30 @@ The "Beautiful Backgrounds" library offers a collection of customizable web comp
|
|
|
10
14
|
|
|
11
15
|
## Installation
|
|
12
16
|
|
|
13
|
-
|
|
17
|
+
Using [`npm`][npm]:
|
|
14
18
|
|
|
15
|
-
|
|
19
|
+
```sh
|
|
20
|
+
npm i beautiful-backgrounds --save
|
|
21
|
+
```
|
|
16
22
|
|
|
17
23
|
```js
|
|
18
24
|
import { BbStarTrail } from 'beautiful-backgrounds';
|
|
19
25
|
```
|
|
20
26
|
|
|
27
|
+
In browsers with [`esm.sh`][esmsh]:
|
|
28
|
+
|
|
29
|
+
```html
|
|
30
|
+
<script type="module">
|
|
31
|
+
import { BbStarTrail } from 'https://esm.sh/beautiful-backgrounds';
|
|
32
|
+
</script>
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Usage
|
|
36
|
+
|
|
37
|
+
### Star Trail
|
|
38
|
+
|
|
21
39
|
```html
|
|
22
40
|
<bb-star-trail
|
|
23
|
-
class="bb-star-trail"
|
|
24
41
|
data-num-stars="567"
|
|
25
42
|
data-star-size-min="0.1"
|
|
26
43
|
data-star-size-max="1.5"
|
|
@@ -44,13 +61,55 @@ import { BbStarTrail } from 'beautiful-backgrounds';
|
|
|
44
61
|
- `data-star-size-max`: Maximum size of a star.
|
|
45
62
|
- `data-star-speed-min`: Minimum speed of a star.
|
|
46
63
|
- `data-star-speed-max`: Maximum speed of a star.
|
|
47
|
-
- `data-star-color-hue-start`: Starting hue for star color.
|
|
48
|
-
- `data-star-color-hue-end`: Ending hue for star color.
|
|
64
|
+
- `data-star-color-hue-start`: Starting hue for star color. Should be a value between 0 and 360.
|
|
65
|
+
- `data-star-color-hue-end`: Ending hue for star color. Should be a value between 0 and 360.
|
|
66
|
+
- `data-star-color-saturation-start`: Specifies the starting color saturation for star color. Should be a value between 0 and 100.
|
|
67
|
+
- `data-star-color-saturation-start`: Specifies the ending color saturation for star color. Should be a value between 0 and 100.
|
|
68
|
+
- `data-star-color-lightness-start`: Specifies the starting color lightness for star color. Should be a value between 0 and 100.
|
|
69
|
+
- `data-star-color-lightness-start`: Specifies the ending color lightness for star color. Should be a value between 0 and 100.
|
|
49
70
|
- `data-radius-min`: Minimum radius for star orbit.
|
|
50
71
|
- `data-radius-max`: Maximum radius for star orbit.
|
|
51
72
|
- `data-star-lifespan-min`: Minimum lifespan of a star in milliseconds.
|
|
52
73
|
- `data-star-lifespan-max`: Maximum lifespan of a star in milliseconds.
|
|
53
74
|
- `data-width`: Width of the star trail component, remove to fit the size of the container
|
|
54
75
|
- `data-height`: height of the star trail component, remove to fit the size of the container
|
|
76
|
+
- `data-background-color`: Background color of the canvas element
|
|
77
|
+
|
|
78
|
+
### Digital Rain
|
|
79
|
+
|
|
80
|
+
```html
|
|
81
|
+
<bb-digital-rain
|
|
82
|
+
data-characteras="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
83
|
+
data-font-size="16"
|
|
84
|
+
data-font-color-hue-start="#0F0"
|
|
85
|
+
data-font-color-hue-end="#0F0"
|
|
86
|
+
data-speed="1"
|
|
87
|
+
></bb-star-trail>
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
#### Attributes for bb-digital-rain
|
|
91
|
+
|
|
92
|
+
- `data-characters`: Sets the characters for the digital rain effect.
|
|
93
|
+
- `data-font-size`: Defines the font size of the falling characters.
|
|
94
|
+
- `data-font-color-hue-start`: Specifies the starting color hue for the rain. Should be a value between 0 and 360.
|
|
95
|
+
- `data-font-color-hue-end`: Sets the ending color hue for the rain. Should be a value between 0 and 360.
|
|
96
|
+
- `data-font-color-saturation-start`: Specifies the starting color saturation for the rain. Should be a value between 0 and 100.
|
|
97
|
+
- `data-font-color-saturation-start`: Specifies the ending color saturation for the rain. Should be a value between 0 and 100.
|
|
98
|
+
- `data-font-color-lightness-start`: Specifies the starting color lightness for the rain. Should be a value between 0 and 100.
|
|
99
|
+
- `data-font-color-lightness-start`: Specifies the ending color lightness for the rain. Should be a value between 0 and 100.
|
|
100
|
+
- `data-speed`: Controls the falling speed of the digital rain.
|
|
101
|
+
- `data-background-color`: Background color of the canvas element
|
|
55
102
|
|
|
56
103
|
[](https://ko-fi.com/A0A3QJPZ9)
|
|
104
|
+
|
|
105
|
+
<!-- Links -->
|
|
106
|
+
|
|
107
|
+
[build-badge]: https://github.com/niekes/beautiful-backgrounds/workflows/main/badge.svg
|
|
108
|
+
|
|
109
|
+
[build]: https://github.com/niekes/beautiful-backgrounds/actions
|
|
110
|
+
|
|
111
|
+
[esmsh]: https://esm.sh
|
|
112
|
+
|
|
113
|
+
[npm]: https://www.npmjs.com/package/beautiful-backgrounds
|
|
114
|
+
|
|
115
|
+
[examples]: https://codepen.io/collection/aMPozo
|
|
@@ -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.resizeDebounceTimer=null,this.canvas=document.createElement("canvas"),this.ctx=this.canvas.getContext("2d")}debouncedResizeCanvas(){clearTimeout(this.resizeDebounceTimer),this.resizeDebounceTimer=setTimeout((()=>{this.resizeCanvas()}),1e3)}interpolateWithThrottle(t,i,s,a,e){const h=Math.ceil(s/a),n=(i-t)/h;let r=t,o=0,l=!0,d=null;!function t(){o<s&&l?(e(r,!1),r+=n,o+=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),h=this.getPropNameCamelCased(t);var n,r;a!==e&&this.fullyInitialized?(n=this,r=h,Object.prototype.hasOwnProperty.call(n,r)&&this.interpolateWithThrottle(i?a:this[h],e,this.interpolationDuration,this.interpolationStep,(t=>{this[h]=t}))):this[h]=e}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.canvas.width=t,this.
|
|
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.resizeDebounceTimer=null,this.canvas=document.createElement("canvas"),this.ctx=this.canvas.getContext("2d")}debouncedResizeCanvas(){clearTimeout(this.resizeDebounceTimer),this.resizeDebounceTimer=setTimeout((()=>{this.resizeCanvas()}),1e3)}interpolateWithThrottle(t,i,s,a,e){const h=Math.ceil(s/a),n=(i-t)/h;let r=t,o=0,l=!0,d=null;!function t(){o<s&&l?(e(r,!1),r+=n,o+=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),h=this.getPropNameCamelCased(t);var n,r;a!==e&&this.fullyInitialized?(n=this,r=h,Object.prototype.hasOwnProperty.call(n,r)&&this.interpolateWithThrottle(i?a:this[h],e,this.interpolationDuration,this.interpolationStep,(t=>{this[h]=t}))):this[h]=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.fullyInitialized=!0}}function s(t,i){return Math.random()*(i-t)+t}function a(t,i){return t=Math.ceil(t),i=Math.floor(i),Math.floor(Math.random()*(i-t+1)+t)}function e(t,i,s,a,e){t<i&&(t=i),t>s&&(t=s);return(t-i)/(s-i)*(e-a)+a}class h{constructor(t,i,e,h,n,r){this.amplitudeX=a(-350,350),this.amplitudeY=a(-350,350),this.frequencyX=s(-1,1),this.frequencyY=s(-1,1),this.time=0,this.dampingFactor=.1,this.phaseX=a(10,70),this.phaseY=a(10,400),this.heartScale=a(10,20),this.deltaRadius=1,this.dx=1,this.size=t,this.speed=i,this.color=e,this.radius=h,this.angle=n,this.lifespan=r,this.x=0,this.y=0,this.born=Date.now(),this.fadeInDuration=.25*this.lifespan,this.fadeOutDuration=.5*this.lifespan}heartShapeMotion(t,i){this.x=t+this.heartScale*(16*Math.pow(Math.sin(this.angle),3)),this.y=i-this.heartScale*(13*Math.cos(this.angle)-5*Math.cos(2*this.angle)-2*Math.cos(3*this.angle)-Math.cos(4*this.angle)),this.angle-=this.speed}spiralMotion(t,i){this.radius+=this.deltaRadius,this.x=t+this.radius*Math.cos(this.angle),this.y=i+this.radius*Math.sin(this.angle),this.angle+=this.speed}updatse(t,i){this.x+=this.dx,this.y=this.amplitudeX*Math.sin(this.frequencyX*this.x+this.phaseX)}dampedHarmonicMotion(t,i){let s=this.amplitudeX*Math.pow(this.dampingFactor,this.time),a=this.amplitudeY*Math.pow(this.dampingFactor,this.time);this.x=t+s*Math.sin(this.frequencyX*this.time+this.phaseX),this.y=i+a*Math.sin(this.frequencyY*this.time+this.phaseY),this.time+=this.speed}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}ellipticalMotion(t,i){this.angle+=this.speed,this.x=t+this.radius*Math.cos(this.angle),this.y=i+this.radius*Math.sin(this.angle)}draw(t){const i=this.calculateOpacity();t.beginPath(),t.arc(this.x,this.y,this.size,0,2*Math.PI,!1),t.fillStyle=this.color,t.globalAlpha=i,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 n(t=[0,360],i=[0,100],s=[40,60]){return`hsl(${a(t[0],t[1])}, ${a(i[0],i[1])}%, ${a(s[0],s[1])}%)`}class r 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(),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())}startAnimation(){const t=()=>{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)})),this.animationFrameId=requestAnimationFrame(t)};this.animationFrameId=requestAnimationFrame(t)}createStar(){const t=s(this.starSizeMin,this.starSizeMax),i=s(e(this.starSpeedMin,-1,1,-.04,.04),e(this.starSpeedMax,-1,1,-.04,.04)),a=s(this.starLifespanMin,this.starLifespanMax),r=n([this.starColorHueStart,this.starColorHueEnd],[this.starColorSaturationStart,this.starColorSaturationEnd],[this.starColorLightnessStart,this.starColorLightnessEnd]),o=s(0,2*Math.PI),l=this.starRadiusMax-this.starRadiusMin,d=Math.sqrt(Math.random())*l+this.starRadiusMin;return new h(t,i,r,d,o,a)}attributeChangedCallback(t,i,s){this.setValues(this.ignoredProps,t,i,s)}}r.observedAttributes=["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",r);class o extends i{constructor(){super(),this.ignoredProps=["data-characters","data-background-color"],this.backgroundColor="0, 0, 0",this.fontSize=24,this.fontColorHueStart=60,this.fontColorHueEnd=150,this.fontColorSaturationStart=90,this.fontColorSaturationEnd=100,this.fontColorLightnessStart=50,this.fontColorLightnessEnd=50,this.speed=2,this.randomness=.9,this.trailOpacity=.1,this.characters='ヲアウエオカキケコサシスセソタツテナニヌネハヒホマミムメモヤユラリワ13579ヲイクコソチΦトノフヤヨルレロンZ:・."¦=*+-<>|リçδ╘'}connectedCallback(){this.shadowR.appendChild(this.canvas),this.resizeCanvas(),this.initialize(),this.startAnimation(),window.addEventListener("resize",this.debouncedResizeCanvas.bind(this))}disconnectedCallback(){window.removeEventListener("resize",this.debouncedResizeCanvas.bind(this)),null!==this.animationFrameId&&window.cancelAnimationFrame(this.animationFrameId)}initialize(){this.columns=Math.floor(this.width/this.fontSize),this.rain=Array(this.columns).fill(this.height)}startAnimation(){const t=()=>{this.ctx.fillStyle=`rgba(${this.backgroundColor}, ${this.trailOpacity})`,this.ctx.fillRect(0,0,this.width,this.height),this.ctx.font=`${this.fontSize}px monospace`;for(let t=0;t<this.rain.length;t++){if(this.rain[t]%this.speed==0){const i=this.characters.charAt(Math.floor(Math.random()*this.characters.length));this.ctx.fillStyle=n([this.fontColorHueStart,this.fontColorHueEnd],[this.fontColorSaturationStart,this.fontColorSaturationEnd],[this.fontColorLightnessStart,this.fontColorLightnessEnd]),this.ctx.fillText(i,t*this.fontSize,this.rain[t]/this.speed*this.fontSize),this.rain[t]/this.speed*this.fontSize>this.height&&Math.random()>this.randomness&&(this.rain[t]=0)}this.rain[t]++}this.animationFrameId=requestAnimationFrame(t)};this.animationFrameId=requestAnimationFrame(t)}attributeChangedCallback(t,i,s){this.setValues(this.ignoredProps,t,i,s)}}o.observedAttributes=["data-characters","data-speed","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",o),t.BbDigitalRain=o,t.BbStarTrail=r}));
|
|
@@ -15,6 +15,7 @@ export default abstract class BeautifulBackground extends HTMLElement {
|
|
|
15
15
|
protected debouncedResizeCanvas(): void;
|
|
16
16
|
protected interpolateWithThrottle(startValue: number, newValue: number, durationMs: number, throttleMs: number, onInterpolate: Function): void;
|
|
17
17
|
protected triggerInterpolation(name: string, oldValue: string, newValue: string): void;
|
|
18
|
+
protected setValues(ignoredProps: string[], name: string, oldValue: string, newValue: string): void;
|
|
18
19
|
protected getPropNameCamelCased(dashCased: string): string;
|
|
19
20
|
protected resizeCanvas(): void;
|
|
20
21
|
}
|
|
@@ -62,6 +62,15 @@ export default class BeautifulBackground extends HTMLElement {
|
|
|
62
62
|
this[prop] = target;
|
|
63
63
|
}
|
|
64
64
|
}
|
|
65
|
+
setValues(ignoredProps, name, oldValue, newValue) {
|
|
66
|
+
if (ignoredProps.includes(name)) {
|
|
67
|
+
const propName = this.getPropNameCamelCased(name);
|
|
68
|
+
this[propName] = newValue;
|
|
69
|
+
}
|
|
70
|
+
if (!ignoredProps.includes(name)) {
|
|
71
|
+
this.triggerInterpolation(name, oldValue, newValue);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
65
74
|
getPropNameCamelCased(dashCased) {
|
|
66
75
|
return dashCased
|
|
67
76
|
.substring(5)
|
|
@@ -72,8 +81,8 @@ export default class BeautifulBackground extends HTMLElement {
|
|
|
72
81
|
const newHeight = Number.parseInt(this.getAttribute('data-height')) || this.parentElement.clientHeight;
|
|
73
82
|
if (newWidth !== this.width || newHeight !== this.height) {
|
|
74
83
|
this.width = newWidth;
|
|
75
|
-
this.canvas.width = newWidth;
|
|
76
84
|
this.height = newHeight;
|
|
85
|
+
this.canvas.width = newWidth;
|
|
77
86
|
this.canvas.height = newHeight;
|
|
78
87
|
}
|
|
79
88
|
this.fullyInitialized = true;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import BB from './BeautifulBackground';
|
|
2
|
+
export declare class BbDigitalRain extends BB {
|
|
3
|
+
backgroundColor: string;
|
|
4
|
+
fontSize: number;
|
|
5
|
+
fontColorHueStart: number;
|
|
6
|
+
fontColorHueEnd: number;
|
|
7
|
+
fontColorSaturationStart: number;
|
|
8
|
+
fontColorSaturationEnd: number;
|
|
9
|
+
fontColorLightnessStart: number;
|
|
10
|
+
fontColorLightnessEnd: number;
|
|
11
|
+
columns: number;
|
|
12
|
+
speed: number;
|
|
13
|
+
randomness: number;
|
|
14
|
+
characters: string;
|
|
15
|
+
rain: number[];
|
|
16
|
+
private trailOpacity;
|
|
17
|
+
private animationFrameId;
|
|
18
|
+
static observedAttributes: string[];
|
|
19
|
+
protected ignoredProps: string[];
|
|
20
|
+
constructor();
|
|
21
|
+
protected connectedCallback(): void;
|
|
22
|
+
protected disconnectedCallback(): void;
|
|
23
|
+
protected initialize(): void;
|
|
24
|
+
protected startAnimation(): void;
|
|
25
|
+
attributeChangedCallback(name: string, oldValue: string, newValue: string): void;
|
|
26
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import BB from './BeautifulBackground';
|
|
2
|
+
import { randomColor } from '../utils/color';
|
|
3
|
+
export class BbDigitalRain extends BB {
|
|
4
|
+
constructor() {
|
|
5
|
+
super();
|
|
6
|
+
this.ignoredProps = ['data-characters', 'data-background-color'];
|
|
7
|
+
this.backgroundColor = '0, 0, 0';
|
|
8
|
+
this.fontSize = 24;
|
|
9
|
+
this.fontColorHueStart = 60;
|
|
10
|
+
this.fontColorHueEnd = 150;
|
|
11
|
+
this.fontColorSaturationStart = 90;
|
|
12
|
+
this.fontColorSaturationEnd = 100;
|
|
13
|
+
this.fontColorLightnessStart = 50;
|
|
14
|
+
this.fontColorLightnessEnd = 50;
|
|
15
|
+
this.speed = 2;
|
|
16
|
+
this.randomness = 0.9;
|
|
17
|
+
this.trailOpacity = 0.1;
|
|
18
|
+
this.characters =
|
|
19
|
+
'ヲアウエオカキケコサシスセソタツテナニヌネハヒホマミムメモヤユラリワ13579ヲイクコソチΦトノフヤヨルレロンZ:・."¦=*+-<>|リçδ╘';
|
|
20
|
+
}
|
|
21
|
+
connectedCallback() {
|
|
22
|
+
this.shadowR.appendChild(this.canvas);
|
|
23
|
+
this.resizeCanvas();
|
|
24
|
+
this.initialize();
|
|
25
|
+
this.startAnimation();
|
|
26
|
+
window.addEventListener('resize', this.debouncedResizeCanvas.bind(this));
|
|
27
|
+
}
|
|
28
|
+
disconnectedCallback() {
|
|
29
|
+
window.removeEventListener('resize', this.debouncedResizeCanvas.bind(this));
|
|
30
|
+
if (this.animationFrameId !== null) {
|
|
31
|
+
window.cancelAnimationFrame(this.animationFrameId);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
initialize() {
|
|
35
|
+
this.columns = Math.floor(this.width / this.fontSize);
|
|
36
|
+
this.rain = Array(this.columns).fill(this.height);
|
|
37
|
+
}
|
|
38
|
+
startAnimation() {
|
|
39
|
+
const animate = () => {
|
|
40
|
+
this.ctx.fillStyle = `rgba(${this.backgroundColor}, ${this.trailOpacity})`;
|
|
41
|
+
this.ctx.fillRect(0, 0, this.width, this.height);
|
|
42
|
+
this.ctx.font = `${this.fontSize}px monospace`;
|
|
43
|
+
for (let i = 0; i < this.rain.length; i++) {
|
|
44
|
+
if (this.rain[i] % this.speed === 0) {
|
|
45
|
+
const text = this.characters.charAt(Math.floor(Math.random() * this.characters.length));
|
|
46
|
+
this.ctx.fillStyle = randomColor([this.fontColorHueStart, this.fontColorHueEnd], [this.fontColorSaturationStart, this.fontColorSaturationEnd], [this.fontColorLightnessStart, this.fontColorLightnessEnd]);
|
|
47
|
+
this.ctx.fillText(text, i * this.fontSize, (this.rain[i] / this.speed) * this.fontSize);
|
|
48
|
+
if ((this.rain[i] / this.speed) * this.fontSize > this.height &&
|
|
49
|
+
Math.random() > this.randomness) {
|
|
50
|
+
this.rain[i] = 0;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
this.rain[i]++;
|
|
54
|
+
}
|
|
55
|
+
this.animationFrameId = requestAnimationFrame(animate);
|
|
56
|
+
};
|
|
57
|
+
this.animationFrameId = requestAnimationFrame(animate);
|
|
58
|
+
}
|
|
59
|
+
attributeChangedCallback(name, oldValue, newValue) {
|
|
60
|
+
this.setValues(this.ignoredProps, name, oldValue, newValue);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
BbDigitalRain.observedAttributes = [
|
|
64
|
+
'data-characters',
|
|
65
|
+
'data-speed',
|
|
66
|
+
'data-randomness',
|
|
67
|
+
'data-font-size',
|
|
68
|
+
'data-font-color-hue-start',
|
|
69
|
+
'data-font-color-hue-end',
|
|
70
|
+
'data-font-color-saturation-start',
|
|
71
|
+
'data-font-color-saturation-end',
|
|
72
|
+
'data-font-color-lightness-start',
|
|
73
|
+
'data-font-color-lightness-end',
|
|
74
|
+
'data-background-color'
|
|
75
|
+
];
|
|
76
|
+
if (!window.customElements.get('bb-digital-rain')) {
|
|
77
|
+
window.customElements.define('bb-digital-rain', BbDigitalRain);
|
|
78
|
+
}
|
|
@@ -1,21 +1,27 @@
|
|
|
1
1
|
import BB from './BeautifulBackground';
|
|
2
2
|
import Particle from './Particle';
|
|
3
3
|
export declare class BbStarTrail extends BB {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
4
|
+
backgroundColor: string;
|
|
5
|
+
starSizeMin: number;
|
|
6
|
+
starSizeMax: number;
|
|
7
|
+
starSpeedMin: number;
|
|
8
|
+
starSpeedMax: number;
|
|
9
|
+
starColorHueStart: number;
|
|
10
|
+
starColorHueEnd: number;
|
|
11
|
+
starColorSaturationStart: number;
|
|
12
|
+
starColorSaturationEnd: number;
|
|
13
|
+
starColorLightnessStart: number;
|
|
14
|
+
starColorLightnessEnd: number;
|
|
15
|
+
starRadiusMin: number;
|
|
16
|
+
starRadiusMax: number;
|
|
17
|
+
starLifespanMin: number;
|
|
18
|
+
starLifespanMax: number;
|
|
19
|
+
numStars: number;
|
|
15
20
|
private trailOpacity;
|
|
16
21
|
private stars;
|
|
17
22
|
private animationFrameId;
|
|
18
23
|
static observedAttributes: string[];
|
|
24
|
+
private ignoredProps;
|
|
19
25
|
constructor();
|
|
20
26
|
protected connectedCallback(): void;
|
|
21
27
|
protected disconnectedCallback(): void;
|
|
@@ -5,17 +5,24 @@ import { randomColor } from '../utils/color';
|
|
|
5
5
|
export class BbStarTrail extends BB {
|
|
6
6
|
constructor() {
|
|
7
7
|
super();
|
|
8
|
-
this.
|
|
8
|
+
this.ignoredProps = ['data-characters', 'data-background-color'];
|
|
9
|
+
this.backgroundColor = '0, 0, 0';
|
|
10
|
+
this.starSizeMin = 0.5;
|
|
9
11
|
this.starSizeMax = 1.5;
|
|
10
12
|
this.starSpeedMin = 0.025;
|
|
11
13
|
this.starSpeedMax = 0.05;
|
|
12
14
|
this.starColorHueStart = 30;
|
|
13
15
|
this.starColorHueEnd = 75;
|
|
16
|
+
this.starColorSaturationStart = 100;
|
|
17
|
+
this.starColorSaturationEnd = 100;
|
|
18
|
+
this.starColorLightnessStart = 50;
|
|
19
|
+
this.starColorLightnessEnd = 50;
|
|
14
20
|
this.starRadiusMin = 1;
|
|
15
21
|
this.starRadiusMax = 100;
|
|
16
22
|
this.starLifespanMin = 1000;
|
|
17
23
|
this.starLifespanMax = 10000;
|
|
18
24
|
this.numStars = 1000;
|
|
25
|
+
this.trailOpacity = 0.1;
|
|
19
26
|
}
|
|
20
27
|
connectedCallback() {
|
|
21
28
|
this.shadowR.appendChild(this.canvas);
|
|
@@ -33,7 +40,6 @@ export class BbStarTrail extends BB {
|
|
|
33
40
|
initialize() {
|
|
34
41
|
this.stars = [];
|
|
35
42
|
this.starRadiusMax = Math.max(this.width, this.height) / 2;
|
|
36
|
-
this.trailOpacity = 0.1;
|
|
37
43
|
this.animationFrameId = null;
|
|
38
44
|
for (let i = 0; i < this.numStars; i++) {
|
|
39
45
|
this.stars.push(this.createStar());
|
|
@@ -41,7 +47,7 @@ export class BbStarTrail extends BB {
|
|
|
41
47
|
}
|
|
42
48
|
startAnimation() {
|
|
43
49
|
const animate = () => {
|
|
44
|
-
this.ctx.fillStyle = `rgba(
|
|
50
|
+
this.ctx.fillStyle = `rgba(${this.backgroundColor}, ${this.trailOpacity})`;
|
|
45
51
|
this.ctx.fillRect(0, 0, this.width, this.height);
|
|
46
52
|
this.stars = this.stars.filter((star) => !star.isDead());
|
|
47
53
|
while (this.stars.length < this.numStars) {
|
|
@@ -59,23 +65,28 @@ export class BbStarTrail extends BB {
|
|
|
59
65
|
const size = getRandomFloat(this.starSizeMin, this.starSizeMax);
|
|
60
66
|
const speed = getRandomFloat(interpolateLinear(this.starSpeedMin, -1, 1, -0.04, 0.04), interpolateLinear(this.starSpeedMax, -1, 1, -0.04, 0.04));
|
|
61
67
|
const lifespan = getRandomFloat(this.starLifespanMin, this.starLifespanMax);
|
|
62
|
-
const color = randomColor([this.starColorHueStart, this.starColorHueEnd]);
|
|
68
|
+
const color = randomColor([this.starColorHueStart, this.starColorHueEnd], [this.starColorSaturationStart, this.starColorSaturationEnd], [this.starColorLightnessStart, this.starColorLightnessEnd]);
|
|
63
69
|
const angle = getRandomFloat(0, Math.PI * 2);
|
|
64
70
|
const radiusRange = this.starRadiusMax - this.starRadiusMin;
|
|
65
71
|
const radius = Math.sqrt(Math.random()) * radiusRange + this.starRadiusMin;
|
|
66
72
|
return new Particle(size, speed, color, radius, angle, lifespan);
|
|
67
73
|
}
|
|
68
74
|
attributeChangedCallback(name, oldValue, newValue) {
|
|
69
|
-
this.
|
|
75
|
+
this.setValues(this.ignoredProps, name, oldValue, newValue);
|
|
70
76
|
}
|
|
71
77
|
}
|
|
72
78
|
BbStarTrail.observedAttributes = [
|
|
79
|
+
'data-background-color',
|
|
73
80
|
'data-star-size-min',
|
|
74
81
|
'data-star-size-max',
|
|
75
82
|
'data-star-speed-min',
|
|
76
83
|
'data-star-speed-max',
|
|
77
84
|
'data-star-color-hue-start',
|
|
78
85
|
'data-star-color-hue-end',
|
|
86
|
+
'data-star-color-saturation-start',
|
|
87
|
+
'data-star-color-saturation-end',
|
|
88
|
+
'data-star-color-lightness-start',
|
|
89
|
+
'data-star-color-lightness-end',
|
|
79
90
|
'data-star-radius-min',
|
|
80
91
|
'data-star-radius-max',
|
|
81
92
|
'data-star-lifespan-min',
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "beautiful-backgrounds",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"keywords": [
|
|
5
5
|
"canvas",
|
|
6
6
|
"animation",
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
],
|
|
22
22
|
"author": "Niekes (https://niekes.com)",
|
|
23
23
|
"license": "BSD-3-Clause",
|
|
24
|
-
"description": "A collection of customizable web components for creating dynamic, animated backgrounds, featuring a variety of effects including a star field.",
|
|
24
|
+
"description": "A collection of customizable web components for creating dynamic, animated backgrounds, featuring a variety of effects including a star field and a digital rain. ",
|
|
25
25
|
"type": "module",
|
|
26
26
|
"main": "dist/index.js",
|
|
27
27
|
"types": "dist/index.d.ts",
|