Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Web Components? Make it so!
Danny Engelman
Danny Engelman

Posted on • Edited on

     

Web Components? Make it so!


Zach his <snow-fall> Web Component

Is asingle<snow-fall> Web Component creating 200 snow-ball DIVs falling down the screen.


Which made me wonder...

...

What if instead ofONE Web Component...

... each snowball would be aunique <snow-flake>?

... How many <snow-flake> Components can my CPU handle?


https://make-it-snow.github.io/

GitHub source code


<make-it-snow> Web Component

an experiment in more and more Web Components

  • Every snowflake is a unique Web Component <snow-flake> with an SVG image.
  • Snowflakes are animated with CSS to fall down rotating and drift sideways.
  • Snowflakes are removed from the DOM when they reach the bottom of the screen (onanimationend).
  • Instead of a fixed amount of snowflakesmore snowflakes are added every second.
  • CSS animation FPS (Frames Per Second) is tracked.When FPS is above athresholdmore new snowflakes (Web Components) are added at the top of the screen (5 more every second). Below thetreshold less snowflakes are added.

FPS can be changed with UP and DOWN arrow keys (once a second, hold down CTRL for 10x).

On my slow? system I get anequilibrium at around400 total snowflakes on screen:

alt text


https://make-it-snow.github.io/

GitHub source code



Creating a unique SVG snowflake

Create one "spike" from 3 SVG paths with random width and opacity

<gid="spike">["M70 70v-60","M45 28l25 18l28-16","M50 11l20 20l20-20"].map((dpath)=>{letwidth=random(3,9);letopacity=random(0.2,1);return`<path opacity="${opacity}" stroke-width="${width}" d="${dpath}"/>`;})</g>
Enter fullscreen modeExit fullscreen mode

Rotate eachspike 4,6 or 8 times

Array(spikecount-1)// from random([4,6,8]).fill(360/spikecount)// calculate degrees offset for each spike.map((degrees,idx)=>{letspikerotation=(idx+1)*degrees;// all spikes make up a snowflake// every snowflake is in a shadowDOM, so its save to reference ID values!!return`<use href="#spike" transform="rotate(${spikerotation} 70 70)"/>`;})
Enter fullscreen modeExit fullscreen mode

That will create all unique SVG snowflakes:


https://make-it-snow.github.io/

GitHub source code


URL parameters and Attributes

The <make-it-snow> Web Component can be configured with URL parameters or Attributes. Also see index.html Page Source.

URL examples

parameters

URL parameters only

ParameterDescriptionDefault Value
flakecountNumber of snow flakes to start with300
addflakesNumber of snow flakes to add every second20
maxflakesA treshold whennot to add more<snow-flake>1000
fpsFPS threshold, change with (ctrl) arrow up/down keys30
minstrokewidthminimal SVG path width3
maxstrokewidthmaximum SVG path width3

<snow-flake> Attributes OR URL parameters

Seehttps://make-it-snow.github.io/index.html page source

AttributeDescriptionDefault Value
color(string/csv) snowflake colorrandom[internal colors]
size(decimal) snowflake size2
rotate(int) initial start state90
spikecount(int/array) number of snowflake spike[4,6,8]
speed(int) fall down speed (seconds)[4 - 12]
drift(int) sideways motion[-5 - 5]
sizedefault size1
opacitydefault opacity1
cssrotationCSS rotationrandom(-360,360)

https://make-it-snow.github.io/

GitHub source code


Observations

Feel free to add more in the comments

  • 300<snow-flake> are 3 STYLE elements + 1 SVG in a shadowDOM
    That is 5 DOM elements for every <snow-flake>

  • I did not do much animation optimization. Feel free to suggest low hanging fruit
    CSSwill-change:transform (or not) doesn't seem to have a notable effect

  • Its all CSS animation, I wonder what a fullscreen SVG with SMIL animation would do (requires some refactoring)

  • I now re-create a<snow-flake>; wonder what happens when every new snowflake will also become a totally new Web Component<snow-flake-[nr]>. Since we can not uncreate Web Components, will thecustomElementRegistry eventually cause a Stack Overflow error?




Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Online since 1990 Yes! I started with Gopher. I do modern Web Component Development with technologies supported by **all** WHATWG partners (Apple, Google, Microsoft & Mozilla)
  • Location
    Amsterdam, the Netherlands
  • Education
    TRS-80 Scriptkiddie '79+ 🎓 Computer Science '87 - '92 ❤️ Online since 1990
  • Work
    I guide people with PTSD, Autism, or general burnout towards a career in web development
  • Joined

More fromDanny Engelman

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp