SVG灯塔
程序员文章站
2022-05-03 20:03:45
...
SVG灯塔
使用svg图形引用、裁切、蒙版制作的小页面。(仅仅作为慕课网学习svg的记录,非原创代码)。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title></title>
<style>
html, body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
background-color: #001122;
line-height: 0;
font-size: 0;
}
</style>
</head>
<body>
<svg width="100%" height="100%" viewBox="-400 -300 800 600" preserveAspectRatio="xMidYMid slice">
<defs>
<polygon id="star" points="0 -10 2 -2 10 0 2 2 0 10 -2 2 -10 0 -2 -2" style="fill: #ffffff;"></polygon>
</defs>
<g id="real">
<g id="star-group"></g>
<g id="moon-group">
<mask id="moon-mask">
<circle cx="-250" cy="-100" r="80" fill="yellow"></circle>
<circle cx="-200" cy="-150" r="80" fill="black"></circle>
</mask>
<circle cx="-250" cy="-100" r="100" fill="yellow" mask="url(#moon-mask)"></circle>
</g>
<g id="light-tower" transform="translate(250,0)">
<defs>
<linearGradient id="tower" x1="0" y1="0" x2="1" y2="0">
<stop offset="0" stop-color="#999"></stop>
<stop offset="1" stop-color="#333"></stop>
</linearGradient>
<clipPath id="light-clip">
<polygon points="0 0 -500 -15 -400 15" fill="rgba(255,0,0,.5)">
<animateTransform
attributeName="transform"
attributeType="XML"
type="rotate"
from="0"
to="360"
dur="10s"
repeatCount="indefinite">
</animateTransform>
</polygon>
<circle cx="0" cy="0" r="2"></circle>
</clipPath>
<radialGradient id="light" cx="0.5" cy="0.5" r="0.5">
<stop offset="0" stop-color="rgba(255,255,255,.8)"></stop>
<stop offset="1" stop-color="rgba(255,255,255,0)"></stop>
</radialGradient>
</defs>
<polygon points="0 0 5 50 -5 50" style="fill: url(#tower);"></polygon>
<ellipse cx="0" cy="0" rx="300" ry="100" fill="url(#light)" clip-path="url(#light-clip)"></ellipse>
</g>
</g>
<g id="reflact" transform="translate(0,50)" mask="url(#fading)">
<defs>
<linearGradient id="fade" x1="0" y1="0" x2="0" y2="1">
<stop offset="0" stop-color="rgba(255,255,255,.3)"></stop>
<stop offset="0.5" stop-color="rgba(255,255,255,0)"></stop>
</linearGradient>
<mask id="fading">
<rect x="-400" y="0" width="800" height="300" fill="url(#fade)"></rect>
</mask>
</defs>
<use xlink:href="#real" transform="scale(1,-1) translate(0 -50)" />
</g>
</svg>
<script>
var SVG_NS = "http://www.w3.org/2000/svg";
var XLINK_NS = "http://www.w3.org/1999/xlink"
var paper = document.querySelector('svg');
var startCount = 500;
var width = document.body.clientWidth;
var height = document.body.clientHeight;
renderStart();
/*生成use标签*/
function use(origin) {
var _use = document.createElementNS(SVG_NS, 'use');
_use.setAttributeNS(XLINK_NS, 'xlink:href', '#'+origin.id);
return _use;
}
/*生成区间内的随机数*/
function random (min, max) {
return min + (max - min) * Math.random();
}
/*绘制星星*/
function renderStart() {
var starRef = document.getElementById("star");
var starGroup = document.getElementById("star-group");
var star;
while (startCount -- ) {
star = use(starRef);
star.setAttribute('opacity', random(0.1,0.4));
star.setAttribute('transform','translate('+random(-400, 400)+','+random(-300,50)+')'+'scale('+random(0.1,0.6)+')');
starGroup.append(star);
}
}
</script>
</body>
</html>