当前位置: 首页 > ai >正文

二次函数图像动画展示

1.功能说明

这个HTML页面具有以下功能:

  1. 交互式参数控制

    • 可以调整二次函数的三个系数a、b、c

    • 可以调整动画速度

  2. 动画效果

    • 点击"播放动画"按钮,函数图像会从当前状态平滑过渡到新的参数设置

    • 动画速度可调

  3. 数学信息显示

    • 实时显示二次函数的数学表达式

    • 计算并显示顶点坐标

  4. 可视化图表

    • 使用Chart.js库绘制函数图像

    • 坐标轴清晰标注

    • 图像平滑连接

2.使用方法

  1. 将上述代码复制到一个文本文件中,保存为.html后缀

  2. 用浏览器打开该文件

  3. 调整参数后点击"播放动画"按钮观察函数图像变化

  4. 点击"重置"按钮恢复默认设置

3.代码

<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>二次函数图像动画展示</title><script src="https://cdn.jsdelivr.net/npm/chart.js"></script><style>body {font-family: 'Arial', sans-serif;max-width: 900px;margin: 0 auto;padding: 20px;background-color: #f5f5f5;color: #333;}h1 {color: #2c3e50;text-align: center;}.container {background-color: white;border-radius: 8px;padding: 20px;box-shadow: 0 2px 10px rgba(0,0,0,0.1);margin-bottom: 20px;}.controls {display: flex;flex-wrap: wrap;gap: 15px;margin-bottom: 20px;align-items: center;}.control-group {display: flex;flex-direction: column;}label {margin-bottom: 5px;font-weight: bold;}input {padding: 8px;border: 1px solid #ddd;border-radius: 4px;width: 80px;}button {background-color: #3498db;color: white;border: none;padding: 10px 15px;border-radius: 4px;cursor: pointer;font-weight: bold;transition: background-color 0.3s;}button:hover {background-color: #2980b9;}.chart-container {position: relative;height: 400px;width: 100%;}.function-display {text-align: center;font-size: 24px;margin: 20px 0;font-family: 'Times New Roman', serif;}.vertex-info {text-align: center;font-size: 16px;margin-top: 10px;color: #7f8c8d;}</style>
</head>
<body><div class="container"><h1>二次函数图像动画展示</h1><div class="controls"><div class="control-group"><label for="a">a 系数</label><input type="number" id="a" step="0.1" value="1"></div><div class="control-group"><label for="b">b 系数</label><input type="number" id="b" step="0.1" value="0"></div><div class="control-group"><label for="c">c 系数</label><input type="number" id="c" step="0.1" value="0"></div><div class="control-group"><label for="speed">动画速度</label><input type="number" id="speed" min="1" max="10" value="5"></div><button id="animateBtn">播放动画</button><button id="resetBtn">重置</button></div><div class="function-display" id="functionDisplay">f(x) = x²</div><div class="vertex-info" id="vertexInfo">顶点坐标: (0, 0)</div><div class="chart-container"><canvas id="quadraticChart"></canvas></div></div><script>// 获取DOM元素const aInput = document.getElementById('a');const bInput = document.getElementById('b');const cInput = document.getElementById('c');const speedInput = document.getElementById('speed');const animateBtn = document.getElementById('animateBtn');const resetBtn = document.getElementById('resetBtn');const functionDisplay = document.getElementById('functionDisplay');const vertexInfo = document.getElementById('vertexInfo');const ctx = document.getElementById('quadraticChart').getContext('2d');// 初始化图表let chart = new Chart(ctx, {type: 'line',data: {datasets: [{label: '二次函数',borderColor: 'rgb(75, 192, 192)',backgroundColor: 'rgba(75, 192, 192, 0.1)',borderWidth: 2,pointRadius: 0,fill: true,tension: 0.1}]},options: {responsive: true,maintainAspectRatio: false,scales: {x: {type: 'linear',position: 'center',title: {display: true,text: 'x'},min: -10,max: 10},y: {type: 'linear',position: 'center',title: {display: true,text: 'f(x)'},min: -10,max: 10}},animation: {duration: 0}}});// 计算二次函数值function quadraticFunction(x, a, b, c) {return a * x * x + b * x + c;}// 更新函数显示function updateFunctionDisplay(a, b, c) {let display = 'f(x) = ';if (a !== 0) {if (a === 1) display += 'x²';else if (a === -1) display += '-x²';else display += a + 'x²';}if (b !== 0) {if (b > 0 && a !== 0) display += ' + ';else if (b < 0) display += ' - ';if (Math.abs(b) !== 1 || a === 0) display += Math.abs(b);display += 'x';}if (c !== 0) {if (c > 0 && (a !== 0 || b !== 0)) display += ' + ';else if (c < 0) display += ' - ';display += Math.abs(c);}// 如果没有显示任何项,说明所有系数都是0if (display === 'f(x) = ') display = 'f(x) = 0';functionDisplay.textContent = display;}// 计算并显示顶点坐标function updateVertexInfo(a, b, c) {if (a === 0) {vertexInfo.textContent = "a=0时不是二次函数";return;}const h = -b / (2 * a);const k = c - (b * b) / (4 * a);vertexInfo.textContent = `顶点坐标: (${h.toFixed(2)}, ${k.toFixed(2)})`;}// 生成数据点function generateData(a, b, c) {const data = [];for (let x = -10; x <= 10; x += 0.1) {data.push({x: x,y: quadraticFunction(x, a, b, c)});}return data;}// 更新图表数据function updateChart(a, b, c) {chart.data.datasets[0].data = generateData(a, b, c);chart.update();updateFunctionDisplay(a, b, c);updateVertexInfo(a, b, c);}// 动画函数function animateToTarget(targetA, targetB, targetC, speed) {const currentA = parseFloat(aInput.value);const currentB = parseFloat(bInput.value);const currentC = parseFloat(cInput.value);const stepA = (targetA - currentA) / (20 / speed);const stepB = (targetB - currentB) / (20 / speed);const stepC = (targetC - currentC) / (20 / speed);let currentStep = 0;const totalSteps = 20;const animation = setInterval(() => {if (currentStep >= totalSteps) {clearInterval(animation);aInput.value = targetA;bInput.value = targetB;cInput.value = targetC;updateChart(targetA, targetB, targetC);animateBtn.disabled = false;return;}const newA = currentA + stepA * currentStep;const newB = currentB + stepB * currentStep;const newC = currentC + stepC * currentStep;updateChart(newA, newB, newC);currentStep++;}, 50);}// 事件监听animateBtn.addEventListener('click', () => {const targetA = parseFloat(document.getElementById('a').value);const targetB = parseFloat(document.getElementById('b').value);const targetC = parseFloat(document.getElementById('c').value);const speed = parseInt(document.getElementById('speed').value);animateBtn.disabled = true;animateToTarget(targetA, targetB, targetC, speed);});resetBtn.addEventListener('click', () => {aInput.value = 1;bInput.value = 0;cInput.value = 0;updateChart(1, 0, 0);});// 输入变化时更新图表(无动画)[aInput, bInput, cInput].forEach(input => {input.addEventListener('change', () => {const a = parseFloat(aInput.value);const b = parseFloat(bInput.value);const c = parseFloat(cInput.value);updateChart(a, b, c);});});// 初始化图表updateChart(1, 0, 0);</script>
</body>
</html>

http://www.xdnf.cn/news/16266.html

相关文章:

  • 在Power Automate Desktop中执行PowerShell获取SharePoint online某个文件夹的用户权限列表
  • excel删除重复项场景
  • 算法竞赛阶段二-数据结构(35)数据结构单链表模拟实现
  • Node.js 模拟 Linux 环境
  • 【每天一个知识点】GAN(生成对抗网络,Generative Adversarial Network)
  • Whisper语音转文字
  • 【洛谷】单向链表、队列安排、约瑟夫问题(list相关算法题)
  • 互联网应用主流框架整合 Spring Boot开发
  • Linux DNS 服务器正反向解析
  • 【IMMCKF】基于容积卡尔曼滤波(CKF)的多模型交互的定位程序,模型为CV和CT,三维环境,matlab代码|附下载链接
  • Nestjs框架: 基于Mongodb的多租户功能集成和优化
  • 算子推理是什么
  • 电脑开机后网络连接慢?
  • (Python)文件储存的认识,文件路径(文件储存基础教程)(Windows系统文件路径)(基础教程)
  • 【17】C# 窗体应用WinForm ——【文本框TextBox、富文本框RichTextBox 】属性、方法、实例应用
  • C++:list(2)list的模拟实现
  • Java中配置两个r2db连接不同的数据库
  • JavaScript:现代Web开发的核心动力
  • Mistral AI开源 Magistral-Small-2507
  • C++查询mysql数据
  • Codeforces Round 181 (Rated for Div. 2)
  • Bert项目--新闻标题文本分类
  • DAY31 整数矩阵及其运算
  • 告别镜像拉取慢!CNB无痛加速方案,一键起飞
  • [论文阅读] 人工智能 + 软件工程 | NoCode-bench:评估LLM无代码功能添加能力的新基准
  • JVM常见工具
  • swagger基本注解@Tag、@Operation、@Parameters、@Parameter、@ApiResponse、@Schema
  • 基于图神经网络的星间路由与计算卸载强化学习算法设计与实现
  • 【Linux手册】操作系统如何管理存储在外设上的文件
  • 基于 Claude Code 与 BrowserCat MCP 的浏览器自动化全链路构建实践