js 动画库、2048核心逻辑、面试题add[1][2][3]+4

article/2025/7/31 9:33:48

1、js 动画库 web animation api

(1)初始化代码

  • hmtl、css 部分
    • 初始化全局背景黑色
    • 初始化黄色小球
  • js 部分
    • 监听全局点击事件
    • 创建并添加元素 class="pointer" 的 div 标签
      • 设置 left、top 位置
      • 监听动画结束事件,移除该元素
    • 定位小球到画面中心
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><link rel="stylesheet" href="./css/js动画库.css">
</head>
<body><div class="ball"></div><script src="./js/js动画库.js"></script>
</body>
</html>
const ball = document.querySelector('.ball');function init() {const x = window.innerWidth / 2;const y = window.innerHeight / 2;ball.style.transform = `translate(${x}px, ${y}px)`
}
init()
body {padding: 0;margin: 0;width: 100%;height: 100vh;background-color: #000;
}
.ball {width: 100px;height: 100px;background-color: yellow;border-radius: 50%;
}

 (2)实现步骤

①点击小球移动

animate():不改变 dom 树,因此不需要浏览器主线程管理,就不会阻塞主线程

window.addEventListener("click", (e) => {const x = e.clientX;const y = e.clientY;move(x, y);
});function move(x, y) {ball.animate([{transform: `translate(0px, 0px)`,},{transform: `translate(${x}px, ${y}px)`,}],2000)
}

②小球不回到中心位置
ball.animate([{transform: `translate(0px, 0px)`,},{transform: `translate(${x}px, ${y}px)`,}],{duration: 1000,fill: 'forwards',}
)

③获取小球当前位置
function move(x, y) {// 获取小球位置const rect = ball.getBoundingClientRect();const ballX = rect.left;const ballY = rect.top;ball.animate([{transform: `translate(${ballX}px, ${ballY}px)`,},{transform: `translate(${x}px, ${y}px)`,}],{duration: 1000,fill: 'forwards',})
}

④清除上一个动画
// 注意:只能在获取小球位置后清除
ball.getAnimations().forEach(animation=>{animation.cancel();
})
⑤小球先原地拉伸,后移动
ball.animate([{transform: `translate(${ballX}px, ${ballY}px)`,},{transform: `translate(${ballX}px, ${ballY}px) scaleX(1.5)`,offset: 0.6, // 该部分动画所占时长},{transform: `translate(${x}px, ${y}px) scaleX(1.5)`,offset: 0.8,},{transform: `translate(${x}px, ${y}px)`,}],{duration: 1000,fill: 'forwards',}
)

⑤拉伸角度跟随鼠标位置
const rad = Math.atan2(y - ballY, x - ballX);
const deg = (rad * 180) / Math.PI;
ball.animate([{transform: `translate(${ballX}px, ${ballY}px) rotate(${deg}deg)`,},{transform: `translate(${ballX}px, ${ballY}px) rotate(${deg}deg) scaleX(1.5)`,offset: 0.6, // 该部分动画所占时长},{transform: `translate(${x}px, ${y}px) rotate(${deg}deg) scaleX(1.5)`,offset: 0.8,},{transform: `translate(${x}px, ${y}px) rotate(${deg}deg)`,}],{duration: 1000,fill: 'forwards',}
)

(3)完整代码

const ball = document.querySelector(".ball");function init() {const x = window.innerWidth / 2;const y = window.innerHeight / 2;ball.style.transform = `translate(${x}px, ${y}px)`;
}
init();window.addEventListener("click", (e) => {const x = e.clientX;const y = e.clientY;move(x, y);
});function move(x, y) {// 获取小球位置const rect = ball.getBoundingClientRect();const ballX = rect.left;const ballY = rect.top;// 注意:只能在获取小球位置后清除ball.getAnimations().forEach((animation)=>{animation.cancel();})const rad = Math.atan2(y - ballY, x - ballX);const deg = (rad * 180) / Math.PI;ball.animate([{transform: `translate(${ballX}px, ${ballY}px) rotate(${deg}deg)`,},{transform: `translate(${ballX}px, ${ballY}px) rotate(${deg}deg) scaleX(1.5)`,offset: 0.6, // 该部分动画所占时长},{transform: `translate(${x}px, ${y}px) rotate(${deg}deg) scaleX(1.5)`,offset: 0.8,},{transform: `translate(${x}px, ${y}px) rotate(${deg}deg)`,}],{duration: 1000,fill: 'forwards',})
}

2、实现 2048 的核心逻辑——合并

Array.prototype.print = function () {console.log(this.join("\n"));
};// 棋盘
const matrix = [[0, 2, 2, 0],[0, 0, 2, 2],[2, 4, 4, 2],[2, 4, 4, 4],
];/*** 移动* @param {*} matrix 棋盘* @param {*} direction 方向*/
function move(matrix, direction) {const rows = matrix.length;const cols = matrix[0].length;/*** 是否越界* @param {*} i* @param {*} j* @returns*/function _inRange(i, j) {return i >= 0 && i < rows && j >= 0 && j < cols;}// 配置方向const nexts = {up: (i, j) => [i + 1, j],down: (i, j) => [i - 1, j],left: (i, j) => [i, j + 1],right: (i, j) => [i, j - 1],};/*** 下一个位置* @param {*} i* @param {*} j* @returns*/function _nextPos(i, j) {const [ni, nj] = nexts[direction](i, j);if (!_inRange(ni, nj)) {return null;}return [ni, nj];}/*** 得到下一个非0的位置* @param {*} i* @param {*} j* @returns*/function _nextNonZeroPos(i, j) {const pos = _nextPos(i, j);if (!pos) {return null;}const [ni, nj] = pos;if (matrix[ni][nj] !== 0) {return pos;}return _nextNonZeroPos(ni, nj); // 递归}/*** 计算某个位置最新的值,同时计算该行或该列所有的最新值* @param {*} i* @param {*} j*/function _calc(i, j) {const pos = _nextNonZeroPos(i, j);if (!pos) {return;}const [ni, nj] = pos;const v = matrix[i][j]; // 当前位置值const nv = matrix[ni][nj]; // 下一个位置值if (v === 0) {matrix[i][j] = nv;matrix[ni][nj] = 0;_calc(i, j); // 递归} else if (v === nv) {matrix[i][j] *= 2;matrix[ni][nj] = 0;}const nextPos = _nextPos(i, j);_calc(...nextPos);}if(direction === 'up') {for (let i = 0; i < cols; i++) {_calc(0, i);}}if(direction === 'down') {for (let i = 0; i < cols; i++) {_calc(rows - 1, i);}}if(direction === 'left') {for (let i = 0; i < cols; i++) {_calc(i, 0);}}if(direction === 'right') {for (let i = 0; i < cols; i++) {_calc(i, cols - 1);}}
}matrix.print();
move(matrix, "down");
console.log("======================");
matrix.print();

3、实现 add

让下面代码成立

const r1 = add[1][2][3] + 4; // 期望结果 10
const r2 = add[10][20] + 30; // 期望结果 60
const r3 = add[100][200][300] + 400; // 期望结果 1000

(1)实现思路

  • 从 add 的调用得出,add 是对象且属性是未知的
  • 如何读取未知属性对象呢?——完成一个代理辅助函数

(2)实现步骤

①读值
function createProxy() {return new Proxy({},{get(target, prop) {console.log(prop);},});
}const add = createProxy();
add[100]; // 100
②返回值

步骤思路1:由 add[1][2][3] 可以看出返回值也必须是一个代理,这样在执行完 add[1] 后能继续执行后面的

const a1 = add[1];
const a2 = a1[2];
const a3 = a2[3];

步骤思路2:累计计算

以下代码报错的原因是 add[1] 是对象,而 2 是原始类型,二者无法相加

function createProxy(value = 0) {return new Proxy({},{get(target, prop) {console.log(prop);return createProxy(value + Number(prop));},});
}const add = createProxy();
const a = add[1] + 2;
console.log(a); // 报错 TypeError: Cannot convert a Symbol value to a number

步骤思路3:对象转为原始类型

function createProxy(value = 0) {const handler = () => value;return new Proxy({},{get(target, prop) {if(prop === Symbol.toPrimitive) {return handler;}return createProxy(value + Number(prop));},});
}const add = createProxy();
const a = add[1] + 2;
console.log(a); // 3
③完整代码-结果验证
function createProxy(value = 0) {const handler = () => value;return new Proxy({},{get(target, prop) {if(prop === Symbol.toPrimitive) {return handler;}return createProxy(value + Number(prop));},});
}// 实现add
const add = createProxy();
// 让下面代码成立
const r1 = add[1][2][3] + 4; // 期望结果 10
const r2 = add[10][20] + 30; // 期望结果 60
const r3 = add[100][200][300] + 400; // 期望结果 1000
console.log(r1); // 10
console.log(r2); // 60
console.log(r3); // 1000

4、Sass 星空

(1)初始化代码

注意:html 引入的是 css 文件

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><link rel="stylesheet" href="./css/Sass星空.css">
</head>
<body><div class="layer1"></div><div class="layer2"></div><div class="layer3"></div><div class="layer4"></div><div class="layer5"></div><div class="title">Sass 星空</div>
</body>
</html>

编写的 css 是在 scss 文件中,后续再通过编译生成对应的 css 文件即可

body {padding: 0;margin: 0;background-color: #10141A;
}.title {font-size: 40px;background: linear-gradient(to top, #000 0%, #fff 100%);-webkit-background-clip: text;background-clip: text;-webkit-text-fill-color: transparent;color: transparent;position: absolute;top: 50%;left: 50%;transform: translate(-50%, -50%);
}

(2)实现步骤

①用 函数+阴影 实现 n 个随机星星
@function star($n) {$result: '#{random(100)}vw #{random(100)}vh 0 #fff';@for $i from 2 through $n {$result: '#{$result},#{random(100)}vw #{random(100)}vh 0 #fff'}@return unquote($result);
}.layer1 {position: fixed;width: 20px;height: 20px;border-radius: 50%;box-shadow: star(100);
}

 

②scss 编译成 css
  • 在 scss 文件目录中下载 sass
  • npm install sass
  • 执行编译命令
  • sass Sass星空.scss Sass星空.css
③让星星动起来
.layer1 {position: fixed;width: 20px;height: 20px;border-radius: 50%;box-shadow: star(100);animation: moveUp 10s linear infinite;
}
@keyframes moveUp {to {transform: translateY((-100vh));}
}

④断层无感处理
.layer1 {position: fixed;width: 20px;height: 20px;left: 0;top: 0;border-radius: 50%;box-shadow: star(100);animation: moveUp 10s linear infinite;
}
.layer1::after {content: '';position: inherit;width: inherit;height: inherit;border-radius: inherit;box-shadow: inherit;left: 0;top: 100vh;
}

⑤循环多层
$n: 5;
$duration: 400s;
$count: 1000;
@for $i from 1 through $n {$duration: floor($duration / 2);$count: floor($count / 2);.layer#{$i} {position: fixed;width: #{$i}px;height: #{$i}px;left: 0;top: 0;border-radius: 50%;box-shadow: star($count);animation: moveUp $duration linear infinite;}.layer#{$i}::after {content: '';position: inherit;width: inherit;height: inherit;border-radius: inherit;box-shadow: inherit;left: 0;top: 100vh;}
}

(3)完整代码

body {padding: 0;margin: 0;width: 100%;height: 100vh;background-color: #10141A;
}.title {font-size: 40px;background: linear-gradient(to top, #000 0%, #fff 100%);-webkit-background-clip: text;background-clip: text;-webkit-text-fill-color: transparent;color: transparent;position: absolute;top: 50%;left: 50%;transform: translate(-50%, -50%);
}@function star($n) {$result: '#{random(100)}vw #{random(100)}vh 0 #fff';@for $i from 2 through $n {$result: '#{$result},#{random(100)}vw #{random(100)}vh 0 #fff'}@return unquote($result);
}$n: 5;
$duration: 400s;
$count: 1000;
@for $i from 1 through $n {$duration: floor($duration / 2);$count: floor($count / 2);.layer#{$i} {position: fixed;width: #{$i}px;height: #{$i}px;left: 0;top: 0;border-radius: 50%;box-shadow: star($count);animation: moveUp $duration linear infinite;}.layer#{$i}::after {content: '';position: inherit;width: inherit;height: inherit;border-radius: inherit;box-shadow: inherit;left: 0;top: 100vh;}
}@keyframes moveUp {to {transform: translateY((-100vh));}
}

http://www.hkcw.cn/article/tWVRqYAxSq.shtml

相关文章

Linux:Shell脚本基础

一、变量的命名规则 变量名称中只能包含数字、大小写字母以及下划线 二、变量的调用方法 三、字符的转义及引用 批量转义用“” 双引号是弱引用&#xff0c;单引号是强引用 四、用命令的执行结果定义变量 $1 $2 $3代表脚本后的第几个字节 $* $表示的是所有字节&#xff0c;但…

【Linux】深入解析:云服务器连接 XShell、Linux 目录结构和常用命令

XShell 使用阿里云服务器连接 XShell 使用Xshell连接阿里云服务器&#xff08;超详细截图版&#xff09;-CSDN博客 连接成功后 xshell 页面&#xff1a; 清空页面内容&#xff0c;直接输入 clear 即可&#xff1a; 关于 XShell 下的复制粘贴 复制&#xff1a;Ctrl Insert&…

掌握Java Runtime类的核心用法

Runtime 类的基本概念 Java 的 Runtime 类位于 java.lang 包中&#xff0c;用于与 Java 运行时环境&#xff08;JVM&#xff09;交互。每个 Java 应用程序都有一个 Runtime 实例&#xff0c;可以通过静态方法 Runtime.getRuntime() 获取。Runtime 类提供了与系统资源交互、执行…

如何设计自动化测试框架?

&#x1f345; 点击文末小卡片&#xff0c;免费获取软件测试全套资料&#xff0c;资料在手&#xff0c;涨薪更快 关于测试框架的好处&#xff0c;比如快速回归提高测试效率&#xff0c;提高测试覆盖率等这里就不讨论了。这里主要讨论自动化框架包含哪些内容&#xff0c;以及如…

历年西安交通大学计算机保研上机真题

2025西安交通大学计算机保研上机真题 2024西安交通大学计算机保研上机真题 2023西安交通大学计算机保研上机真题 在线测评链接&#xff1a;https://pgcode.cn/school 计算圆周率近似值 题目描述 根据公式 π / 4 1 − 1 / 3 1 / 5 − 1 / 7 … \pi / 4 1 - 1/3 1/5 - …

历年武汉大学计算机保研上机真题

2025武汉大学计算机保研上机真题 2024武汉大学计算机保研上机真题 2023武汉大学计算机保研上机真题 在线测评链接&#xff1a;https://pgcode.cn/school 分段函数计算 题目描述 写程序计算如下分段函数&#xff1a; 当 x > 0 x > 0 x>0 时&#xff0c; f ( x ) …

LearnOpenGL-笔记-其十

今天我们要进入高级光照的环节了&#xff1a; Advanced Lighting 之前的学习中&#xff0c;我们的光照模型采用的是比较简单的phong光照模型&#xff0c;也就是光照强度由环境光加上漫反射光加上镜面反射组成。 用一张图足以解释&#xff1a; 就这么简单&#xff0c;针对夹…

大话软工笔记—分离原理

1. 基本概念 1.1 定义 分离原理&#xff0c;将研究对象中符合相同设计方法的同类项进行分离、归集。 1.2 作用 分离原理的主要作用是&#xff1a; &#xff08;1&#xff09;降低了需求分析与软件设计的复杂程度。 &#xff08;2&#xff09;提出了对企业构成内容按照设计…

扫地机器人苦寻新引擎,大疆们却已攻入腹地

原创 科技新知 前沿科技组 作者丨江篱 编辑丨樱木、九黎 竞争激烈的扫地机器人赛道&#xff0c;迎来了新玩家。 据近日相关报道&#xff0c;大疆扫地机器人产品已开始量产&#xff0c;预计将于6月份发布。消息称大疆研发扫地机器人已超过四年&#xff0c;即将上市的产品是扫…

【Docker管理工具】部署Docker管理面板DweebUI

【Docker管理工具】部署Docker管理面板DweebUI 一、DweebUI介绍1.1 DweebUI 简介1.2 主要特点1.3 使用场景 二、本次实践规划2.1 本地环境规划2.2 本次实践介绍 三、本地环境检查3.1 检查Docker服务状态3.2 检查Docker版本3.3 检查docker compose 版本 四、下载DweebUI镜像五、…

《系统集成项目管理工程师(第三版)》高效学习方法

文章目录 一、学习周期规划(总时长:3-4个月)二、每日学习时间分配表三、重难点突破策略1. 五星必考重点(占分60%+)2. 高频易错点四、高效记忆法(附实例)1. 口诀记忆法2. 联想记忆法3. 对比记忆法五、阶段目标检测六、十大管理高频整理一、十大管理ITTO高频考点表🔥 必…

新松机械臂 2001端口服务的客户端例程

初级代码游戏的专栏介绍与文章目录-CSDN博客 我的github&#xff1a;codetoys&#xff0c;所有代码都将会位于ctfc库中。已经放入库中我会指出在库中的位置。 这些代码大部分以Linux为目标但部分代码是纯C的&#xff0c;可以在任何平台上使用。 源码指引&#xff1a;github源…

HarmonyOS-ArkUI固定样式弹窗(1)

固定样式弹窗指的就是ArkUI中为我们提供的一些具备界面模板性质的弹窗。样式是固定的,我们可以决定在这些模板里输入什么样的内容。常见的有,警告弹窗, 列表选择弹窗, 选择器弹窗,对话框,操作菜单。 下图是本文中要讲到的基类固定样式弹窗,其中选择器弹窗没有包含在内,…

Dify运行本地和在线模型

1、运行本地模型 1.1、下载Ollama 官网下载&#xff1a;Ollama 1.2、安装LLM大模型 Ollama官网搜索模型&#xff0c;复制命令&#xff1a; 在终端中粘贴下载&#xff0c;下载完成后可以输入对话&#xff0c;表示安装成功。 使用命令&#xff1a;ollama list 可以列出已经安装…

manus对比ChatGPT-Deep reaserch进行研究类论文数据分析!谁更胜一筹?

目录 没有账号&#xff0c;只能挑选一个案例 1、manus的效果 Step-1&#xff1a;直接看结果 Step-2&#xff1a;看看其他文件的细节 Step-3&#xff1a;看最终报告 2、Deep reaserch 3、Deep reaserch进行行业分析 总结一下&#xff1a; 大家好这里是学术Anan&#xff…

2025年大一ACM训练-尺取

2025年大一ACM训练-尺取 ​​尺取法&#xff08;Sliding Window&#xff09;&#xff1a; ​​1. 基本概念​​   尺取法&#xff08;又称滑动窗口法&#xff09;是一种​​通过维护窗口的左右边界来高效解决子区间问题​​的算法技巧&#xff0c;常用于&#xff1a;   1…

第十二章 MQTT会话

系列文章目录 第一章 总体概述 第二章 在实体机上安装ubuntu 第三章 Windows远程连接ubuntu 第四章 使用Docker安装和运行EMQX 第五章 Docker卸载EMQX 第六章 EMQX客户端MQTTX Desktop的安装与使用 第七章 EMQX客户端MQTTX CLI的安装与使用 第八章 Wireshark工具的安装与使用 …

【C++】C++入门基础

本文是小编巩固自身而作&#xff0c;如有错误&#xff0c;欢迎指出&#xff01; 1.C的第一个程序 C兼容C语⾔绝⼤多数的语法&#xff0c;所以C语⾔实现的hello world依旧可以运⾏&#xff0c;C中需要把定义⽂件 代码后缀改为.cpp&#xff0c;vs编译器看到是.cpp就会调⽤C编译…

iEKF的二维应用实例

如果熟悉 EKF 与卡尔曼的推导的话&#xff0c;iEKF 就比较容易理解&#xff0c;关于卡尔曼滤波的推导以及EKF&#xff0c;可以参考以前的文章&#xff1a; 卡尔曼滤波原理&#xff1a;https://blog.csdn.net/a_xiaoning/article/details/130564473?spm1001.2014.3001.5502 E…

[IMX] 10.串行外围设备接口 - SPI

代码链接&#xff1a;GitHub - maoxiaoxian/imx 参考资料&#xff1a; https://zhuanlan.zhihu.com/p/290620901 SPI协议详解 - bujidao1128 - 博客园 SPI总线协议及SPI时序图详解 - Ady Lee - 博客园 目录 1.SPI 简介 2.I.MX6U ECSPI 简介 2.1.控制寄存器 1 - ECSPIx_CO…