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

二次函数图像动画展示

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.dtcms.com/a/299537.html

相关文章:

  • 雨雪雾冰全预警:交通气象站为出行安全筑起“隐形防护网”
  • 【愚公系列】《MIoT.VC》003-构建基本仿真工作站(组件的属性、行为、视频展示)
  • Spring Boot 单元测试进阶:JUnit5 + Mock测试与切片测试实战及覆盖率报告生成
  • inversify
  • 【web大前端】001_前端开发入门:创建你的第一个网页
  • 0基础法考随手笔记 03(刑诉05 刑事证据与证明+06 强制措施)
  • 【智能协同云图库】第三期:实现用户上传图片及审核功能、使用模板方法模式优化上传图片功能、使用 Jsoup 实现批量抓取和上传图片功能
  • 掌握Gemini-2.5:现代AI开发中实用应用的综合指南
  • 2025最新软件测试面试八股文【附文档】
  • 基于动态增强的 LLM 置信度方法研究
  • 存储引擎 RocksDB
  • 速通python加密之SHA加密
  • MySQL进阶学习与初阶复习第三天
  • AWD的攻击和防御手段
  • 在 C# 中,问号 ? 的一些作用
  • 0.深度学习环境配置步骤
  • 前端开发 Vue 结合Sentry 实现性能监控
  • GitHub的免费账户的存储空间有多少?
  • 明辨 JS 中 prototype 与 __proto__
  • 学习嵌入式的第三十天-数据结构-(2025.7.21)网络编程
  • Netty中AbstractChannelHandlerContext源码分析
  • Springboot+MongoDB简单使用示例
  • Java 大视界 -- Java 大数据在智能安防视频监控系统中的视频语义理解与智能检索进阶(365)
  • MySQL 中 VARCHAR(50) 和 VARCHAR(500) 的区别
  • Python训练Day24
  • 机器学习入门:线性回归详解与实战
  • Javaweb————HTTP的九种请求方法介绍
  • VTK交互——CallData
  • MySQL操作进阶
  • setsockopt函数概念和使用案例