 
						Html
| 1 2 3 4 | <section id="ci-particles">    <canvas id="canvas"></canvas>    <h1 id="headline">jQuery</h1></section> | 
CSS样式
为页面添加基本样式。
| 1 2 3 4 5 6 | body {    background-color:#000000;    margin:0;    overflow:hidden;    font-size:0;} | 
Javascript
然后通过下面的js代码来生成canvas粒子文字和交互动画。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | varcanvas = document.querySelector("#canvas"),   ctx = canvas.getContext("2d"),   link = document.createElement('link');   particles = [],   amount = 0,   mouse = { x: -9999, y: -9999 },   radius = 1,   colors = [     "rgba(252,248,254,0.85)", //粒子颜色在这里修改rgb格式     "rgba(220,203,255,0.75)",      "rgba(154,112,124,0.85)",      "rgba(192,213,255,0.85)",      "rgba(244,223,254,0.75)"   ],   headline = document.querySelector("#headline"),   ww = window.innerWidth,   wh = window.innerHeight;functionParticle(x, y) { this.x = Math.random() * ww; this.y = Math.random() * wh; this.dest = { x: x, y: y }; this.r = Math.random() * 2 * Math.PI; this.vx = (Math.random() - 0.5) * 25; this.vy = (Math.random() - 0.5) * 25; this.accX = 0; this.accY = 0; this.friction = Math.random() * 0.025 + 0.94; this.color = colors[Math.floor(Math.random() * 2.75)];}Particle.prototype.render = function() { this.accX = (this.dest.x - this.x) / 1000; this.accY = (this.dest.y - this.y) / 1000; this.vx += this.accX; this.vy += this.accY; this.vx *= this.friction; this.vy *= this.friction; this.x += this.vx; this.y += this.vy; ctx.fillStyle = this.color; ctx.beginPath(); ctx.arc(this.x, this.y, this.r, Math.PI * 2, false); ctx.fill(); vara = this.x - mouse.x; varb = this.y - mouse.y; vardistance = Math.sqrt(a * a + b * b); if(distance < (radius * 75)) {   this.accX = (this.x - mouse.x) / 100;   this.accY = (this.y - mouse.y) / 100;   this.vx += this.accX;   this.vy += this.accY; }}functiononMouseMove(e) { mouse.x = e.clientX; mouse.y = e.clientY; } functiononTouchMove(e) { if(e.touches.length > 0) {   mouse.x = e.touches[0].clientX;   mouse.y = e.touches[0].clientY; }}functiononTouchEnd(e) { mouse.x = -9999; mouse.y = -9999;}functioninitScene() { ww = canvas.width = window.innerWidth; wh = canvas.height = window.innerHeight; ctx.clearRect(0, 0, canvas.width, canvas.height); link.rel = 'stylesheet'; link.type = 'text/css'; document.getElementsByTagName('head')[0].appendChild(link); ctx.font = 'bold 26vw "Abril Fatface"'; ctx.textAlign = "center"; ctx.fillText(headline.innerHTML, ww / 2, wh / 1.6); vardata = ctx.getImageData(0, 0, ww, wh).data; ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.globalCompositeOperation = "screen"; particles = []; for(vari = 0; i < ww; i += Math.round(ww / 200)) {   for(varj = 0; j < wh; j += Math.round(ww / 200)) {     if(data[((i + j * ww) * 4) + 3] > 200) {             particles.push(newParticle(i, j));     }   } } amount = particles.length;} | 
特别申明:
			本站所有资源都是由网友投稿发布,或转载各大下载站,请自行检测软件的完整性!
			本站所有资源仅供学习与参考,请勿用于商业用途,否则产生的一切后果将由您自己承担!
			如有侵权请联系我们删除下架,联系方式:lei1294551502@163.com