带你读《现代Javascript高级教程》三十一、requestAnimationFrame:优化动画和渲染的利器(2)

简介: 带你读《现代Javascript高级教程》三十一、requestAnimationFrame:优化动画和渲染的利器(2)

带你读《现代Javascript高级教程》三十一、requestAnimationFrame:优化动画和渲染的利器(1)https://developer.aliyun.com/article/1349490?groupCode=tech_library


4. 使用requestAnimationFrame的示例

下面通过几个示例来演示如何使用requestAnimationFrame来实现动画和渲染效果。

1 实现平滑的滚动效果

下面的示例代码演示了如何使用requestAnimationFrame实现平滑的滚动效果:

 

function smoothScrollTo(targetY, duration) {
  const startY = window.pageYOffset;
  const distance = targetY - startY;
  const startTime = performance.now();
  function step(currentTime) {
    const elapsedTime = currentTime - startTime;
    const progress = Math.min(elapsedTime / duration, 1);
    const ease = easingFunction(progress);
    window.scrollTo(0, startY + distance * ease);
    if (elapsedTime < duration) {
      requestAnimationFrame(step);
    }
  }
  requestAnimationFrame(step);}
function easingFunction(t) {
  return t * t * t;}
// 使用示例const button = document.querySelector('scrollButton');
button.addEventListener('click', () => {
  smoothScrollTo(1000, 1000);});

 

在上述代码中,我们定义了一个smoothScrollTo函数,用于实现平滑的滚动效果。该函数接收目标位置targetY和滚动的持续时间duration作为参数。在函数内部,我们获取当前的滚动位置startY和目标位置与起始位置之间的距离distance

 

然后,我们使用performance.now()获取当前的时间戳startTime,并定义一个step函数用于更新滚动位置。在step函数中,我们根据时间的流逝计算出进度progress,并使用缓动函数easingFunction来调整进度。最后,我们使用requestAnimationFrame调度step函数的执行,并在滚动动画完成之前不断更新滚动位置。

2 实现粒子动画效果

下面的示例代码演示了如何使用requestAnimationFrame实现粒子动画效果:

 

const canvas = document.querySelector('canvas');const ctx = canvas.getContext('2d');
const particles = [];
function Particle(x, y, speedX, speedY, radius, color) {
  this.x = x;
  this.y = y;
  this.speedX = speedX;
  this.speedY = speedY;
  this.radius = radius;
  this.color = color;}
Particle.prototype.update = function() {
  this.x += this.speedX;
  this.y += this.speedY;
  if (this.x + this.radius < 0 || this.x - this.radius > canvas.width) {
    this.speedX = -this.speedX;
  }
  if (this.y + this.radius < 0 || this.y - this.radius > canvas.height) {
    this.speedY = -this.speedY;
  }};
Particle.prototype.draw = function() {
  ctx.beginPath();
  ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
  ctx.fillStyle = this.color;
  ctx.fill();
  ctx.closePath();};
function createParticles() {
  for (let i = 0; i < 100; i++) {
    const x = Math.random() * canvas.width;
    const y = Math.random() * canvas.height;
    const speedX = Math.random() * 4 - 2;
    const speedY = Math.random() * 4 - 2;
    const radius = Math.random() * 5 + 1;
    const color = getRandomColor();
    particles.push(new Particle(x, y, speedX, speedY, radius, color));
  }}
function updateParticles() {
  particles.forEach((particle) => {
    particle.update();
  });}
function drawParticles() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  particles.forEach((particle) => {
    particle.draw();
  });
  requestAnimationFrame(drawParticles);}
// 使用示例createParticles();drawParticles();
function getRandomColor() {
  const letters = '0123456789ABCDEF';
  let color = '';
  for (let i = 0; i < 6; i++) {
    color += letters[Math.floor(Math.random() * 16)];
  }
  return color;}

 

带你读《现代Javascript高级教程》三十一、requestAnimationFrame:优化动画和渲染的利器(3)https://developer.aliyun.com/article/1349488?groupCode=tech_library

相关文章
|
5天前
|
前端开发 JavaScript UED
使用JavaScript实现动画效果
【4月更文挑战第21天】使用JavaScript实现动画效果
28 10
|
5天前
|
JavaScript 前端开发 网络安全
【网络安全 | 信息收集】JS文件信息收集工具LinkFinder安装使用教程
【网络安全 | 信息收集】JS文件信息收集工具LinkFinder安装使用教程
16 4
|
5天前
|
Web App开发 JavaScript 前端开发
《手把手教你》系列技巧篇(三十九)-java+ selenium自动化测试-JavaScript的调用执行-上篇(详解教程)
【5月更文挑战第3天】本文介绍了如何在Web自动化测试中使用JavaScript执行器(JavascriptExecutor)来完成Selenium API无法处理的任务。首先,需要将WebDriver转换为JavascriptExecutor对象,然后通过executeScript方法执行JavaScript代码。示例用法包括设置JS代码字符串并调用executeScript。文章提供了两个实战场景:一是当时间插件限制输入时,用JS去除元素的readonly属性;二是处理需滚动才能显示的元素,利用JS滚动页面。还给出了一个滚动到底部的代码示例,并提供了详细步骤和解释。
32 10
|
2天前
|
前端开发 JavaScript
JavaScript新科技:PostCSS的安装和使用,2024年最新2024网易Web前端高级面试题总结
JavaScript新科技:PostCSS的安装和使用,2024年最新2024网易Web前端高级面试题总结
|
2天前
|
JavaScript 前端开发
web前端JS高阶面试题(1),高级开发工程师面试
web前端JS高阶面试题(1),高级开发工程师面试
|
3天前
|
前端开发 JavaScript UED
CSS顶部与JS后写:网页渲染的奥秘
CSS顶部与JS后写:网页渲染的奥秘
|
5天前
|
资源调度
在 Next.js 中使用自定义服务器框架进行服务器端渲染
在 Next.js 中使用自定义服务器框架进行服务器端渲染
Next.js 的服务器端渲染框架集成
Next.js 的服务器端渲染框架集成
|
5天前
|
存储 JavaScript 前端开发
Javascript教程
Javascript教程
9 0
|
5天前
|
JavaScript 前端开发 Java
《手把手教你》系列技巧篇(四十)-java+ selenium自动化测试-JavaScript的调用执行-下篇(详解教程)
【5月更文挑战第4天】本文介绍了如何使用JavaScriptExecutor在自动化测试中实现元素高亮显示。通过创建并执行JS代码,可以改变元素的样式,例如设置背景色和边框,以突出显示被操作的元素。文中提供了一个Java示例,展示了如何在Selenium中使用此方法,并附有代码截图和运行效果展示。该技术有助于跟踪和理解测试过程中的元素交互。
13 0