JS는 마우스를 따라가는 아름다운 색상 버블 효과를 구현합니다(코드 첨부).

青灯夜游
풀어 주다: 2020-06-16 10:26:33
앞으로
2563명이 탐색했습니다.

다음 기사에서는 JS를 사용하여 마우스를 따라가는 아름다운 색상 거품 효과를 얻는 방법을 소개합니다. 기사의 샘플 코드가 매우 자세히 소개됩니다. 도움이 필요한 친구들이 모두 참고할 수 있기를 바랍니다.

JS는 마우스를 따라가는 아름다운 색상 버블 효과를 구현합니다(코드 첨부).

특정 코드:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
 
<style>
 *{
 margin:0;padding:0;
 }
 body{overflow:hidden;}
 #canvas{
 background-color:black;
 /*width:100%;
 height:100vh;*/
 }
  
</style>
 
</head>
<body>
 
<canvas id="canvas" ></canvas> 
  
</body>
 
<script>
var canvas = document.querySelector(&#39;#canvas&#39;);
var ctx = canvas.getContext("2d");
var starlist = [];
function init(){
 canvas.width = window.innerWidth;
 canvas.height = window.innerHeight;
}
init();
window.onresize = init;
 
canvas.addEventListener(&#39;mousemove&#39;,function(e){
 starlist.push(new Star(e.offsetX,e.offsetY));
 console.log(starlist)
})
 
function random(min,max){
 return Math.floor((max-min)*Math.random()+ min);
}
 
function Star(x,y){
 this.x = x;
 this.y = y;
 this.vx = (Math.random()-0.5)*3;
 this.vy = (Math.random()-0.5)*3;
 this.color = &#39;rgb(&#39;+random(0,256)+&#39;,&#39;+random(0,256)+&#39;,&#39;+random(0,256)+&#39;)&#39;;
 this.a = 1;
 console.log(this.color);
 this.draw();
}
Star.prototype={
 draw:function(){
 ctx.beginPath();
 ctx.fillStyle = this.color;
 ctx.globalCompositeOperation=&#39;lighter&#39;
 ctx.globalAlpha= this.a;
 ctx.arc(this.x,this.y,30,0,Math.PI*2,false);
 ctx.fill();
 this.updata();
 },
 updata(){
 this.x+=this.vx;
 this.y+=this.vy;
 this.a*=0.98;
 }
}
console.log(new Star(150,200));
function render(){
 ctx.clearRect(0,0,canvas.width,canvas.height)
  
 starlist.forEach((item,i)=>{
 item.draw();
 if(item.a<0.05){
 starlist.splice(i,1);
 }
 })
  
 requestAnimationFrame(render);
}
render(); 
 
</script>
<div style="text-align:center;">
</div>
 
</html>
로그인 후 복사

더 많은 jQuery 및 Javascript 특수 효과를 보려면 js 특수 효과 컬렉션을 방문하는 것이 좋습니다!

위 내용은 JS는 마우스를 따라가는 아름다운 색상 버블 효과를 구현합니다(코드 첨부).의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
js
원천:jb51.net
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!