자바스크립트로 자연스럽게 내리는 눈 효과 만들기
Html
<div class="snow">
<canvas id="canv"></canvas>
</div>
css
body,div{
margin:0;
padding: 0;
}
.snow{
width:100%;
height: 100vh;
background-image: url(https://images.unsplash.com/photo-1491002052546-bf38f186af56?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=2200&q=80);
background-size:cover;
}
JS
function Snowy() {
function t() {
window.requestAnimationFrame(t), $.clearRect(0, 0, w, h), $.fillStyle = "hsla(255,255%,255%,0)", $.fillRect(0, 0, w, h), $.fill();
for (var i = 0; i < a.length; ++i) f = a[i], f.t += .05, f.t = f.t >= 2 * Math.PI ? 0 : f.t, f.y += f.sp, f.x += Math.sin(f.t * s) * (.3 * f.sz), f.y > h + 50 && (f.y = -10 - Math.random() * r), f.x > w + r && (f.x = -r), f.x < -r && (f.x = w + r), f.draw()
}
function i() {
this.draw = function() {
this.g = $.createRadialGradient(this.x, this.y, 0, this.x, this.y, this.sz), this.g.addColorStop(0, "hsla(255,255%,255%,1)"), this.g.addColorStop(1, "hsla(255,255%,255%,0)"), $.moveTo(this.x, this.y), $.fillStyle = this.g, $.beginPath(), $.arc(this.x, this.y, this.sz, 0, 2 * Math.PI, !0), $.fill()
}
}
for (var n, a = [], e = 300, s = 1, o = 1, d = 1.3, r = 20, l = 1, c = 0; c < e; ++c) n = new i, n.y = Math.random() * (h + 50), n.x = Math.random() * w, n.t = Math.random() * (2 * Math.PI), n.sz = 100 / (10 + 100 * Math.random()) * d, n.sp = .15 * Math.pow(.8 * n.sz, 2) * o, n.sp = n.sp < l ? l : n.sp, a.push(n);
t()
} // 300 을 조절 하면 눈의 양 조절 가능
var c = document.getElementById("canv"),
$ = c.getContext("2d"),
w = c.width = window.innerWidth,
h = c.height = window.innerHeight;
Snowy(), window.addEventListener("resize", function() {
c.width = w = window.innerWidth, c.height = h = window.innerHeight
}, !1)
소스 출처
https://codepen.io/tmrDevelops/pen/PPgjwz
Midnight Snow
`#50Lines||Less` [ squeeezed ] <br> `#CodeVember Day 20`...
codepen.io
728x90
반응형
'퍼블리싱' 카테고리의 다른 글
모달팝업, 모달창, 이미지 슬라이드 제이쿼리 플러그인 - 무료(MIT) (0) | 2023.06.16 |
---|---|
[CSS 활용] css로 블러 효과(포토샵 blur 효과 주기) (0) | 2023.06.15 |
CSS 이미지사이즈 맞추기 - object-fit & background 비교 (0) | 2022.12.21 |
[css] flex로 간단하게 수직&수평 가운데 정렬 (0) | 2022.12.20 |
[JS 활용] 제이쿼리, 상단으로 자연스럽게 가는 버튼만들기, scrollTop, fadeIn, fadeOut (0) | 2022.12.08 |
댓글