When I see some cool animations, I always want to realize it, but I have no choice but to have a little less code in my belly, so I have to do it without realizing it. My big round eyes are full of The desire for new things.
To embed animations in web pages, Falsh, Java Applet and Microsoft's SilverLight were popular in the past, but they had shortcomings such as web page security, compatibility, difficulty in operation and file size. Many web developers love and hate it. Gif is still a good choice, but unfortunately you cannot use DOM to control its elements. Since the advent of SVG, HTML5 and CSS3, the implementation of some simple flat animations has become convenient and simple. Although they are still not completely satisfactory in some aspects such as compatibility and operability, they are enough to make web developers excited. , so ideas such as "graceful degradation" and "progressive enhancement" became popular. In terms of Web drawing, WebGL is relatively cool. It can render cool three-dimensional animations on the page, but its native interface has a steep learning curve, and WebGL developers need a certain graphics foundation, and the entry barrier is high. , as a result, some frameworks that encapsulate the WebGL native interface have emerged one after another, the most famous of which should be three.js. This article is going to use SVG and JavaScript to make a cool DEMO, so that the browser can automatically render a sky full of stars. (Don’t ask me that nothing is loaded in IE8 and below browsers. This is because SVG is only compatible with IE9 and above and other mainstream browsers. However, this does not stop the charm of SVG~)
This is what we want the effect to be made.<svg width="100%" height="100%" viewBox="-400 -300 800 600" preserveAspectRation="xMinYMid slice"><!-- 绘制星星原型 --><defs><polygon id="star" points="0 -10 2 -2 10 0 2 2 0 10 -2 2 -10 0 -2 -2" fill="white"></polygon></defs><!-- 装载满天星辰 --><g id="star-group"></g></svg>
html, body {margin: 0; padding: 0; width: 100%; height: 100%; background: #012; font-size: 0; line-height: 0;}
window.onload=function () {// 页面加载完毕后,执行满天星辰的渲染rederStar();}// SVG元素的命名空间var svgNS = "http://www.w3.org/2000/svg";var XLINK_NS = "http://www.w3.org/1999/xlink";// 将use属性封装成函数,以便调用function use(origin) {var _use = document.createElementNS(svgNS, "use");_use.setAttributeNS(XLINK_NS, "xlink:href", "#" + origin.id);return _use;}// 创建任意两个数之间的随机数function random(min, max) {return min + (max - min) * Math.random();}// 绘制满天星星function rederStar() {// 获取星星原型和装载满天星星的容器star-groupvar starRef = document.getElementById("star");var starGroup = document.getElementById("star-group");// 星星的总数量var starCount = 500;// 循环,画出 starCount 个星星for(var i = 1; i < starCount; i++) {// star引用starRef原型var star;star = use(starRef);// 让星星的透明度、位置和大小随机变化star.setAttribute("opacity", random(0.1, 0.4));star.setAttribute("transform", "translate(" + random(-1000, 1000) + "," + random(-400, 400) + ") " + "scale(" + random(0.1, 0.6) + ")");// 将绘制好星星置入starGroup的DOM子元素中starGroup.appendChild(star);}}
Here is the Demo.(2KB)
I originally wanted to use AI to draw something myself, but my AI got convulsed, so I took some SVG nude pictures from an unknown person to use. Modify the source code and add some animation effects of wind blowing.
See the Pen &amp;amp;amp;amp;lt;a href="http://codepen.io/dengzhirong/pen/YXVVEV /" data-ke-src="http://codepen.io/dengzhirong/pen/YXVVEV/"&amp;amp;amp;amp;amp;gt;YXVVEV&amp; amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;amp;gt; by dengzhirong (&amp;amp;amp;amp;amp;lt; a href="http://codepen.io/dengzhirong" data-ke-src="http://codepen.io/dengzhirong"&amp;amp;amp;amp;amp;gt;@ dengzhirong&amp;amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;amp;gt;) on &amp;amp;amp ;amp;amp;lt;a href="http://codepen.io" data-ke-src="http://codepen.io"&amp;amp;amp;amp;amp; gt;CodePen&amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;amp;gt;.
Here is the Demo .(7KB)