w = canvas.width = window.innerWidth, h = canvas.height = window.innerHeight,
时间: 2024-03-19 07:42:59 浏览: 17
这段代码将浏览器窗口的宽高分别赋值给 canvas 画布的宽高,使得 canvas 画布的大小和浏览器窗口大小一致,从而实现全屏显示。其中,window.innerWidth 和 window.innerHeight 分别表示浏览器窗口的宽和高。通过将这两个值赋给 canvas 的宽高属性,可以使得 canvas 画布的大小自适应浏览器窗口大小的变化。
相关问题
export default function canvasBg(){ window.requestAnimFrame = (function() { return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function(callback) { window.setTimeout(callback, 1000 / 60); }; })(); var c = document.getElementById('canvas'); var $ = c.getContext('2d'); var w = c.width = window.innerWidth; var h = c.height = window.innerHeight; var _w = w * 0.5; var _h = h * 0.5; var arr = []; var cnt = 0; window.addEventListener('load', resize); window.addEventListener('resize', resize, false); function resize() { c.width = w = window.innerWidth; c.height = h = window.innerHeight; c.style.position = 'absolute'; c.style.left = (window.innerWidth - w) * .01 + 'px'; c.style.top = (window.innerHeight - h) * .01 + 'px'; } function anim() { cnt++; if (cnt % 6) draw(); window.requestAnimFrame(anim); } anim(); function draw() { var splot = { x: rng(_w - 900, _w + 900), y: rng(_h - 900, _h + 900), r: rng(20, 80), spX: rng(-1, 1), spY: rng(-1, 1) }; arr.push(splot); while (arr.length > 100) { arr.shift(); } $.clearRect(0, 0, w, h); for (var i = 0; i < arr.length; i++) { splot = arr[i];; $.fillStyle = rndCol(); $.beginPath(); $.arc(splot.x, splot.y, splot.r, 0, Math.PI * 2, true); $.shadowBlur = 80; $.shadowOffsetX = 2; $.shadowOffsetY = 2; $.shadowColor = rndCol(); $.globalCompositeOperation = 'lighter'; $.fill(); splot.x = splot.x + splot.spX; splot.y = splot.y + splot.spY; splot.r = splot.r * 0.96; } } function rndCol() { var r = Math.floor(Math.random() * 180); var g = Math.floor(Math.random() * 60); var b = Math.floor(Math.random() * 100); return "rgb(" + r + "," + g + "," + b + ")"; } function rng(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } }
这是一个用 JavaScript 实现的 canvas 背景效果,它会在 canvas 元素上绘制一些圆形的粒子,并给这些粒子添加阴影和颜色渐变,形成一个动态的背景效果。具体实现方式是通过 setInterval() 方法不断调用 draw() 函数来更新画布。在 anim() 函数中通过 requestAnimationFrame() 方法来循环调用 draw() 函数,以达到动态更新的效果。resize() 函数会在页面大小发生改变时调用,重新设置 canvas 元素的宽高和位置。rng() 函数用来生成一个指定范围内的随机数,rndCol() 函数用来生成一个随机的颜色值。
帮我看下这段生长粒子树的代码,页面显示不出来,怎么解决? <!DOCTYPE html> <html> <head> <style> #canvas { position: absolute; top: 0; left: 0; width: 100%; height: 100%; } </style> </head> <body> <canvas id="canvas"></canvas> <script> // 使用 ES6 的类来表示粒子 class Particle { constructor(position, parentPosition) { this.position = position; this.parentPosition = parentPosition; this.children = []; } addChild(child) { this.children.push(child); } } // 创建画布 const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); // 设置画布大小 canvas.width = window.innerWidth; canvas.height = window.innerHeight; // 生成粒子生长树 function generateTree(numParticles, maxDepth, growthStep) { const particles = [new Particle({ x: canvas.width / 2, y: canvas.height }, null)]; for (let i = 0; i < maxDepth; i++) { const newParticles = []; for (const particle of particles) { for (let j = 0; j < numParticles; j++) { const deltaX = Math.random() * growthStep - growthStep / 2; const deltaY = Math.random() * growthStep - growthStep / 2; const newPosition = { x: particle.position.x + deltaX, y: particle.position.y - growthStep + deltaY }; const newParticle = new Particle(newPosition, particle.position); particle.addChild(newParticle); newParticles.push(newParticle); } } particles.push(...newParticles); } return particles; } // 绘制粒子生长树 function drawTree(particles) { ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.strokeStyle = 'black'; function drawParticle(particle) { ctx.beginPath(); ctx.moveTo(particle.parentPosition.x, particle.parentPosition.y); ctx.lineTo(particle.position.x, particle.position.y); ctx.stroke(); for (const child of particle.children) { drawParticle(child); } } for (const particle of particles) { drawParticle(particle); } } // 示例用法 const numParticles = 3; const maxDepth = 5; const growthStep = 50; const particles = generateTree(numParticles, maxDepth, growthStep); drawTree(particles); </script> </body> </html>
这段代码是用来生成和绘制生长粒子树的。从你提供的代码来看,可能无法显示页面的原因是因为没有将canvas元素添加到HTML中。
你可以尝试将以下代码添加到<body>标签内:
```html
<canvas id="canvas"></canvas>
```
这样就可以将canvas元素添加到页面中,然后重新加载页面看看是否能够显示生长粒子树了。