简体   繁体   English

画布跟随光标

[英]Canvas Following cursor

I want that firework follow Mouse/cursor, I have tired so much but cannot find solution. 我希望烟花跟随鼠标/光标移动,我已经很累了,但是找不到解决方法。

 <canvas id="c"></canvas> <script> /* Starter js1k Template Code */ var b = document.body; var c = document.getElementsByTagName('canvas')[0]; var a = c.getContext('2d'); document.body.clientWidth; /* js1k Entry Code - Color Shift */ function p(){this.x=~~(Math.random()*(ee/3-e/3+1))+e/3,this.y=~~(Math.random()*(hh/3-h/3+1))+h/3,this.a=.1,this.c=this.d=1,this.b=(e+h)/7}var e=h=c.width=c.height=250,f=c.style,g=[],j=0;k=this.requestAnimationFrame||this.webkitRequestAnimationFrame||this.mozRequestAnimationFrame||this.oRequestAnimationFrame||this.msRequestAnimationFrame||function(a){setTimeout(a,1e3/60)},b.style.backgroundColor="#000",f.position="absolute",f.left=f.top="50%",f.marginLeft=f.marginTop="-125px",function q(){k(q,c),a.globalCompositeOperation="destination-out",a.fillStyle="rgba(0, 0, 0, .05)",a.fillRect(0,0,e,h),a.globalCompositeOperation="lighter";var b=g.length;for(0==j%2&&g.push(new p);b--;){var d=g[b],f=b,i=(db-da)/db;dc=.5+dd*i,d.a+=dc,d.alpha=2.5*i,a.beginPath(),a.arc(dx,dy,da,0,2*Math.PI),a.fillStyle="hsla("+(~~(Math.random()*(j+100-(j-100)+1))+(j-100))+", 100%, 1%, "+d.alpha+")",a.fill(),da>db&&g.splice(f,1)}j++}(); </script> 

Add this to the code - it will move the canvas element itself along with the mouse. 将其添加到代码中-它将随画布一起移动canvas元素本身。 You might have to adjust other CSS to avoid scrollbar (overflow: hidden): 您可能需要调整其他CSS以避免滚动条(溢出:隐藏):

window.onmousemove = function(e) {
  c.style.left = e.clientX + "px"
  c.style.top = e.clientY + "px";
};

 /* Starter js1k Template Code */ var b = document.body; var c = document.getElementsByTagName('canvas')[0]; var a = c.getContext('2d'); window.onmousemove = function(e) { c.style.left = e.clientX + "px" c.style.top = e.clientY + "px"; }; /* js1k Entry Code - Color Shift */ function p(){this.x=~~(Math.random()*(ee/3-e/3+1))+e/3,this.y=~~(Math.random()*(hh/3-h/3+1))+h/3,this.a=.1,this.c=this.d=1,this.b=(e+h)/7}var e=h=c.width=c.height=250,f=c.style,g=[],j=0;k=this.requestAnimationFrame||this.webkitRequestAnimationFrame||this.mozRequestAnimationFrame||this.oRequestAnimationFrame||this.msRequestAnimationFrame||function(a){setTimeout(a,1e3/60)},b.style.backgroundColor="#000",f.position="absolute",f.left=f.top="50%",f.marginLeft=f.marginTop="-125px",function q(){k(q,c),a.globalCompositeOperation="destination-out",a.fillStyle="rgba(0, 0, 0, .05)",a.fillRect(0,0,e,h),a.globalCompositeOperation="lighter";var b=g.length;for(0==j%2&&g.push(new p);b--;){var d=g[b],f=b,i=(db-da)/db;dc=.5+dd*i,d.a+=dc,d.alpha=2.5*i,a.beginPath(),a.arc(dx,dy,da,0,2*Math.PI),a.fillStyle="hsla("+(~~(Math.random()*(j+100-(j-100)+1))+(j-100))+", 100%, 1%, "+d.alpha+")",a.fill(),da>db&&g.splice(f,1)}j++}(); 
 <canvas id="c"></canvas> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM