1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>函数图像绘制工具</title>
  6. <script type="text/javascript" src="js/funcImg.js"></script>
  7. <style>
  8. #div-img {
  9. /* 此决定画布的宽高 */
  10. width: 500px;
  11. height: 500px;
  12. }
  13. #input-controller {
  14. padding: 10px;
  15. background-color: azure;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <div id="input-controller">
  21. <div id="function">
  22. <button onclick="Add()">添加函数</button>
  23. <span id="mod" style="display:none" name="0">
  24. <input type="color"/>y=
  25. <input type="text" value="x^3" name="Fun"/>
  26. <button onclick="Delete(this.parentNode)">Delete</button>
  27. <input type="checkbox" onclick="reDraw()" checked="checked"/>Draw Line
  28. </span>
  29. </div>
  30. <div id="option">
  31. X:<input id="xLeftValue" /> ~
  32. <input id="xRightValue" />
  33. <br> Y:
  34. <input id="yLeftValue" /> ~
  35. <input id="yRightValue" />
  36. <br>
  37. <span id="show-size">Size:</span>
  38. </div>
  39. <button onclick="change()">画图</button>
  40. </div>
  41. <div id="main">
  42. <h1>函数图像绘制工具</h1>
  43. <div id="div-img">
  44. <canvas id="graph"></canvas>
  45. </div>
  46. </div>
  47. </body>
  48. <script>
  49. const FONT_STYLE = "10px 黑体";
  50. const MIN = 1e-4;
  51. const MAX = 1e8;
  52. const EPS = 1e-12;
  53. const CANVAS = $("graph");
  54. const CONTEXT_2D = CANVAS.getContext("2d");
  55. const FUN_IMG_WIDTH = CANVAS.parentNode.clientWidth;
  56. const FUN_IMG_HEIGHT = CANVAS.parentNode.clientHeight;
  57. var xLeftValue = -FUN_IMG_WIDTH / 100; // x最左的值
  58. var xRightValue = FUN_IMG_WIDTH / 100;
  59. var yLeftValue = -FUN_IMG_HEIGHT / 100;
  60. var yRightValue = FUN_IMG_HEIGHT / 100;
  61. var tableX, tableY, countX, countY;
  62. var funStage = 0,
  63. mouseX, mouseY;
  64. var tmp;
  65. </script>
  66. <script>
  67. CANVAS.width = FUN_IMG_WIDTH;
  68. CANVAS.height = FUN_IMG_HEIGHT;
  69. $("show-size").innerHTML = "Size:" + FUN_IMG_WIDTH + "*" + FUN_IMG_HEIGHT;
  70. CANVAS.onmousedown = function(ob) {
  71. mouseX = ob.layerX;
  72. mouseY = ob.layerY;
  73. funStage = 1;
  74. }
  75. CANVAS.onmousemove = function(ob) {
  76. if(funStage != 1) {
  77. return;
  78. }
  79. var NoX, NoY, det;
  80. NoX = ob.layerX;
  81. NoY = ob.layerY;
  82. det = (mouseX - NoX) / FUN_IMG_WIDTH * (xRightValue - xLeftValue);
  83. xLeftValue += det;
  84. xRightValue += det;
  85. det = (NoY - mouseY) / FUN_IMG_HEIGHT * (yRightValue - yLeftValue);
  86. yLeftValue += det;
  87. yRightValue += det;
  88. mouseX = NoX;
  89. mouseY = NoY;
  90. reDraw();
  91. update();
  92. }
  93. CANVAS.onmouseup = function(ob) {
  94. if(funStage == 1) {
  95. funStage = 0;
  96. reDraw();
  97. }
  98. }
  99. CANVAS.onmouseleave = function(ob) {
  100. if(funStage == 1) {
  101. funStage = 0;
  102. reDraw();
  103. }
  104. }
  105. CANVAS.onmousewheel = function(ob) {
  106. // 取消事件的默认动作
  107. ob.preventDefault();
  108. // 放大的比例
  109. var ScaleRate = 0.9;
  110. var detail;
  111. if(ob.wheelDelta) {
  112. detail = ob.wheelDelta;
  113. } else if(ob.detail) {
  114. detail = ob.detail;
  115. }
  116. if(detail > 0) {
  117. scale(ob.layerX, FUN_IMG_HEIGHT - 1 - ob.layerY, ScaleRate);
  118. } else {
  119. scale(ob.layerX, FUN_IMG_HEIGHT - 1 - ob.layerY, 1 / ScaleRate);
  120. }
  121. reDraw();
  122. update();
  123. }
  124. // 初始化
  125. reDraw();
  126. update();
  127. Add();
  128. </script>
  129. </html>

funcImg.js

  1. function $(id) {
  2. return document.getElementById(id);
  3. }
  4. function getRandomColor() {
  5. var color = '#' + ('00000' + (Math.random() * 0x1000000 << 0).toString(16)).substr(-6);
  6. return color;
  7. }
  8. function FunWork(f, x) {
  9. switch(f) {
  10. case "":
  11. {
  12. return x;
  13. break;
  14. }
  15. case "sin":
  16. {
  17. return Math.sin(x);
  18. break;
  19. }
  20. case "cos":
  21. {
  22. return Math.cos(x);
  23. break;
  24. }
  25. case "tan":
  26. {
  27. return Math.tan(x);
  28. break;
  29. }
  30. case "abs":
  31. {
  32. return Math.abs(x);
  33. break;
  34. }
  35. case "sqrt":
  36. {
  37. return Math.sqrt(x);
  38. break;
  39. }
  40. case "ln":
  41. {
  42. return Math.log(x);
  43. break;
  44. }
  45. case "log":
  46. {
  47. return Math.log(x) / Math.LN2;
  48. break;
  49. } //2为底
  50. case "lg":
  51. {
  52. return Math.log(x) / Math.LN10;
  53. break;
  54. } //10为底
  55. case "floor":
  56. {
  57. return Math.floor(x);
  58. break;
  59. }
  60. case "ceil":
  61. {
  62. return Math.ceil(x);
  63. break;
  64. }
  65. case "int":
  66. {
  67. return parseInt(x);
  68. break;
  69. }
  70. default:
  71. {
  72. return NaN;
  73. break;
  74. }
  75. }
  76. }
  77. function ChangeToPointY(y) {
  78. return FUN_IMG_HEIGHT - 1 - parseInt((y - yLeftValue) / (yRightValue - yLeftValue) * FUN_IMG_HEIGHT);
  79. }
  80. function isChar(c) {
  81. return(c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
  82. }
  83. function isDigit(c) {
  84. return c >= '0' && c <= '9';
  85. }
  86. function priority(c) {
  87. switch(c) {
  88. case '(':
  89. return 0;
  90. break;
  91. case '+':
  92. return 1;
  93. break;
  94. case '-':
  95. return 1;
  96. break;
  97. case '*':
  98. return 2;
  99. break;
  100. case '/':
  101. return 2;
  102. break;
  103. case '^':
  104. return 3;
  105. break;
  106. default:
  107. return -1;
  108. break;
  109. }
  110. }
  111. // 是运算符
  112. function isOpt(c) {
  113. return priority(c) != -1;
  114. }
  115. function singleCalc(c, a, b) {
  116. if(c == '+') {
  117. return a + b;
  118. } else
  119. if(c == '-') {
  120. return a - b;
  121. } else
  122. if(c == '*') {
  123. return a * b;
  124. } else
  125. if(c == '/') {
  126. return a / b;
  127. } else
  128. if(c == '^') {
  129. return Math.pow(a, b);
  130. } else {
  131. return NaN;
  132. }
  133. }
  134. function getTable() {
  135. tmp = (xRightValue - xLeftValue + EPS) / 20;
  136. tableX = 1;
  137. countX = 0;
  138. countY = 0;
  139. while(tableX < tmp) {
  140. tableX *= 10;
  141. }
  142. while(tableX / 10 > tmp) {
  143. tableX /= 10;
  144. countX++;
  145. }
  146. if(tableX >= 1) {
  147. countX = 0;
  148. }
  149. if(tableX / 5 > tmp) {
  150. tableX /= 5;
  151. countX++;
  152. } else if(tableX / 2 > tmp) {
  153. tableX /= 2;
  154. countX++;
  155. }
  156. var i = parseInt(xLeftValue / tableX) + (xLeftValue > 0)
  157. for(; i * tableX < xRightValue; i++) {
  158. if(i == 0) {
  159. // y轴
  160. CONTEXT_2D.fillStyle = "black";
  161. } else {
  162. // 普通竖线
  163. CONTEXT_2D.fillStyle = "#CDB7B5";
  164. }
  165. tmp = (i * tableX - xLeftValue) / (xRightValue - xLeftValue) * FUN_IMG_WIDTH;
  166. var _width = 1;
  167. var _height = FUN_IMG_HEIGHT;
  168. CONTEXT_2D.fillRect(tmp, 0, _width, _height);
  169. // 竖线上的数字
  170. CONTEXT_2D.fillStyle = "red";
  171. CONTEXT_2D.font = FONT_STYLE;
  172. var _text = (i * tableX).toFixed(countX);
  173. var _x = tmp + 2;
  174. var _y = 10;
  175. CONTEXT_2D.fillText(_text, _x, _y);
  176. }
  177. tmp = (yRightValue - yLeftValue + EPS) / 20;
  178. tableY = 1;
  179. while(tableY < tmp) {
  180. tableY *= 10;
  181. }
  182. while(tableY / 10 > tmp) {
  183. tableY /= 10, countY++;
  184. }
  185. if(tableY / 5 > tmp) {
  186. tableY /= 5, countY++;
  187. } else if(tableY / 2 > tmp) {
  188. tableY /= 2, countY++;
  189. }
  190. if(tableY >= 1) {
  191. countY = 0;
  192. }
  193. var i = parseInt(yLeftValue / tableY) + (yLeftValue > 0);
  194. for(; i * tableY < yRightValue; i++) {
  195. // 横线
  196. if(i == 0) {
  197. // x轴
  198. CONTEXT_2D.fillStyle = "black";
  199. } else {
  200. // 普通横线
  201. CONTEXT_2D.fillStyle = "#CDB7B5";
  202. }
  203. tmp = (i * tableY - yLeftValue) / (yRightValue - yLeftValue) * FUN_IMG_HEIGHT;
  204. CONTEXT_2D.fillRect(0, FUN_IMG_HEIGHT - 1 - tmp, FUN_IMG_WIDTH, 1);
  205. // 横线上的数字
  206. CONTEXT_2D.fillStyle = "blue";
  207. CONTEXT_2D.font = FONT_STYLE;
  208. CONTEXT_2D.fillText((i * tableY).toFixed(countY), 0, FUN_IMG_HEIGHT - 1 - tmp);
  209. }
  210. }
  211. function drawArc(x, y) {
  212. CONTEXT_2D.beginPath();
  213. // arc(弧形),画圆
  214. CONTEXT_2D.arc(x, y, 1, 0, Math.PI * 2);
  215. CONTEXT_2D.closePath();
  216. CONTEXT_2D.fill();
  217. }
  218. function drawLine(lx, ly, px, py) {
  219. CONTEXT_2D.beginPath();
  220. CONTEXT_2D.moveTo(lx, ly);
  221. CONTEXT_2D.lineTo(px, py);
  222. CONTEXT_2D.closePath();
  223. CONTEXT_2D.stroke(); // 绘制
  224. }
  225. function reDraw() {
  226. CONTEXT_2D.clearRect(0, 0, FUN_IMG_WIDTH, FUN_IMG_HEIGHT);
  227. getTable();
  228. getFunction();
  229. }
  230. function change() {
  231. xLeftValue = parseFloat($("xLeftValue").value);
  232. xRightValue = parseFloat($("xRightValue").value);
  233. yLeftValue = parseFloat($("yLeftValue").value);
  234. yRightValue = parseFloat($("yRightValue").value);
  235. reDraw();
  236. }
  237. function update() {
  238. $("xLeftValue").value = xLeftValue;
  239. $("xRightValue").value = xRightValue;
  240. $("yLeftValue").value = yLeftValue;
  241. $("yRightValue").value = yRightValue;
  242. }
  243. function scale(x, y, times) {
  244. if(x < 0 || x >= FUN_IMG_WIDTH || y < 0 || y >= FUN_IMG_HEIGHT) return;
  245. if(times < 1 && (xRightValue - xLeftValue < MIN || yRightValue - yLeftValue < MIN)) {
  246. return;
  247. }
  248. if(times > 1 && (xRightValue - xLeftValue > MAX || yRightValue - yLeftValue > MAX)) {
  249. return;
  250. }
  251. x = xLeftValue + (xRightValue - xLeftValue) / FUN_IMG_WIDTH * x;
  252. y = yLeftValue + (yRightValue - yLeftValue) / FUN_IMG_HEIGHT * y;
  253. xLeftValue = x - (x - xLeftValue) * times;
  254. xRightValue = x + (xRightValue - x) * times;
  255. yLeftValue = y - (y - yLeftValue) * times;
  256. yRightValue = y + (yRightValue - y) * times;
  257. }
  258. function Calc(fun, X, Value) {
  259. var number = new Array(),
  260. opt = new Array(),
  261. F = new Array(),
  262. now = 0,
  263. f = "",
  264. tmp, a, b, sign = 1,
  265. base = 0,
  266. j;
  267. for(var i = 0; i < fun.length; i++) {
  268. for(j = 0; j < X.length; j++)
  269. if(X[j] == fun[i]) {
  270. if(i == 0 || isOpt(fun[i - 1])) now = Value[j];
  271. else now *= Value[j];
  272. break;
  273. }
  274. if(j == X.length)
  275. if(fun[i] == '(') F.push(f), f = "", opt.push('(');
  276. else
  277. if(fun[i] == ')') {
  278. number.push(now * sign);
  279. now = 0;
  280. sign = 1;
  281. base = 0;
  282. while((tmp = opt.pop()) != '(') {
  283. b = number.pop();
  284. a = number.pop();
  285. tmp = singleCalc(tmp, a, b);
  286. number.push(tmp);
  287. }
  288. now = FunWork(F.pop(), number.pop());
  289. } else
  290. if(fun[i] == '.') base = 1;
  291. else
  292. if(fun[i] == '+' && (i == 0 || priority(fun[i - 1]) != -1));
  293. else
  294. if(fun[i] == '-' && (i == 0 || priority(fun[i - 1]) != -1)) sign = -1;
  295. else
  296. if(fun[i] == 'e')
  297. if(i == 0 || isOpt(fun[i - 1])) now = Math.E;
  298. else now *= Math.E;
  299. else
  300. if(fun[i] == 'p' && fun[i + 1] == 'i') {
  301. if(i == 0 || isOpt(fun[i - 1])) now = Math.PI;
  302. else now *= Math.PI;
  303. i += 1;
  304. } else
  305. if(isDigit(fun[i]))
  306. if(base == 0) now = now * 10 + (fun[i] - '0');
  307. else base /= 10, now += base * (fun[i] - '0');
  308. else
  309. if(isChar(fun[i])) f += fun[i];
  310. else if(isOpt(fun[i])) {
  311. number.push(now * sign);
  312. now = 0;
  313. sign = 1;
  314. base = 0;
  315. var s = priority(fun[i]);
  316. if(s == -1) return 0;
  317. while(s <= priority(opt[opt.length - 1])) {
  318. b = number.pop();
  319. a = number.pop();
  320. tmp = singleCalc(opt.pop(), a, b);
  321. number.push(tmp);
  322. }
  323. opt.push(fun[i]);
  324. }
  325. }
  326. number.push(now * sign);
  327. while(opt.length > 0) {
  328. b = number.pop();
  329. a = number.pop();
  330. tmp = singleCalc(opt.pop(), a, b);
  331. number.push(tmp);
  332. }
  333. return number.pop();
  334. }
  335. function getFunction() {
  336. // group:函数(可能是复数)
  337. var group = document.getElementsByName("Fun");
  338. var x, y;
  339. var lax, lay;
  340. var px, py
  341. var color, outSide, type
  342. var ValueL, ValueR, ValueS, isDrawLine, tmp, TMP;
  343. for(var k = 1; k < group.length; k++) {
  344. var _funcItem = group[k].parentNode;
  345. outSide = 1;
  346. //type = _funcItem.children[0].value;
  347. // 颜色
  348. color = _funcItem.children[0].value;
  349. // 函数表达式
  350. funcExpression = group[k].value;
  351. // 是否画线(默认画点)
  352. isDrawLine = _funcItem.children[3].checked;
  353. CONTEXT_2D.fillStyle = CONTEXT_2D.strokeStyle = color;
  354. for(var i = 0; i < FUN_IMG_WIDTH; i++) {
  355. x = xLeftValue + (xRightValue - xLeftValue) / FUN_IMG_WIDTH * i;
  356. y = Calc(funcExpression, ['x'], [x]);
  357. if(isNaN(y)) {
  358. continue;
  359. }
  360. px = i;
  361. py = ChangeToPointY(y);
  362. if(y >= yLeftValue && y < yRightValue) {
  363. // 画圆
  364. drawArc(px, py);
  365. if(isDrawLine) {
  366. drawLine(lax, lay, px, py);
  367. }
  368. outSide = 0;
  369. } else {
  370. if(isDrawLine) {
  371. if(!outSide) {
  372. drawLine(lax, lay, px, py);
  373. }
  374. } else {}
  375. outSide = 1;
  376. }
  377. lax = px;
  378. lay = py;
  379. }
  380. }
  381. }
  382. function Add() {
  383. var newInput = $("mod").cloneNode(true);
  384. newInput.style.display = "block";
  385. newInput.children[0].value = getRandomColor();
  386. $("function").appendChild(newInput);
  387. }
  388. function Delete(node) {
  389. node.parentNode.removeChild(node);
  390. }

HTML+JavaScript画函数图像的更多相关文章

  1. Catlike学习笔记(1.2)-使用Unity画函数图像

    『Catlike系列教程』第二篇来了~今天周六,早上(上午11点)醒来去超市买了一周的零食回来以后就玩了一整天游戏非常有负罪感.现在晚上九点天还亮着感觉像下午7点左右的样子好像还不是很晚...所以就写 ...

  2. [Java画图]画函数图像

    利用Graphics类画任意显式函数图像,只需修改代码中的F()函数即可,另外调整timesx和timesy参数来分方向放大或缩小图像.需要重定义坐标系. package test; import j ...

  3. javascript自制函数图像生成器

    出于某种目的想做这个东西,顺便可以提供给GMA的用户&&放在博客园. 实现上只是简单的描点,加上一个相邻两点连线的开关,完全没有技术含量.而且函数图像一旦多起来就会变卡. 瓶颈在隐函数 ...

  4. 用python画函数图像

    import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 1, 50) # 从0到1,等分50分 y = 210*(x ...

  5. Catlike学习笔记(1.3)-使用Unity画更复杂的3D函数图像

    第三篇来了-今天去参加了 Unite 2018 Berlin,感觉就是....非常困...回来以后稍微睡了下清醒了觉得是时候认真学习下了,不过讲的很多东西都是还没有发布或者只有 Preview 的版本 ...

  6. Javascript 随机数函数 学习之二:产生服从正态分布随机数

    一.为什么需要服从正态分布的随机函数 一般我们经常使用的随机数函数 Math.random() 产生的是服从均匀分布的随机数,能够模拟等概率出现的情况,例如 扔一个骰子,1到6点的概率应该相等,但现实 ...

  7. javascript + jquery函数大全

    JAVASCRIPT Array 函数   array创建数组 concat()连接两个或更多的数组,并返回结果. join()把数组中所有元素组成字符串. pop()删除并返回数组的最后一个元素 s ...

  8. python如何画三维图像?

    python三维图像输出的代码如下所示:#画3D函数图像输出from mpl_toolkits.mplot3d import Axes3Dfrom matplotlib import cmimport ...

  9. JavaScript中函数函数的定义与变量的声明<基础知识一>

    1.JavaScript中函数的三种构造方式 a.function createFun(){ } b.var createFun=function (){ } c.var createFun=new ...

随机推荐

  1. JavaScript核心 Dom Bom

    <div id="time">2020-9-27</div> <script> //文档页面从上往下加载,先有标签才能获取元素对象,script ...

  2. 前端小程序——js+canvas 给图片添加水印

    市场上各种各样的图片处理器有很多,那么作为程序员的我们是不是应该自己做一个呢?那就从加水印开始吧 html: <canvas id="shuiyinTest"> < ...

  3. error:docker-ce conflicts with 2:docker-1.13.1-74.git6e3bb8e.el7.centos.x86_64

    问题原因:安装docker之前有安装cockpit-docker服务 解决方法:卸载docker-ce [root@localhost ~]# yum list installed | grep do ...

  4. ViewModel和LiveData问题思考与解答

    嗨,大家好,面试真题系列又来了,今天我们说说MVVM架构里的两大组件:ViewModel和LiveData. 还是老样子,提出问题,做出解答. ViewModel 是什么? ViewModel 为什么 ...

  5. 【故障公告】博客站点再次出现故障,最终回退 .NET 5.0 恢复正常

    自从博客系统升级 .NET 5.0 之后遇到的诡异故障(一.二.三.四),今天它又出现了,就在前天刚刚故障之后, 就在昨天 .NET 5.0 正式版刚刚发布之后,出现了. 今天晚上我们在 19:30 ...

  6. Mac环境MySql初始密码设置

    1. 首先 点击系统偏好设置 -> 点击MySQL, 在弹出的页面中,关闭服务.2. 进入终端命令输出: cd /usr/local/mysql/bin/ 命令,回车.3. 回车后,输入命令:s ...

  7. ECMAScript6标准-简介

    Introduction This Ecma Standard defines the ECMAScript 2015 Language. It is the sixth edition of the ...

  8. win10,安装 vmware 后没有虚拟网卡,导致虚拟机没有 ip

    1.确认关闭windows firewall 服务,最保险的关闭时先把服务改为手动再关闭防止塔自动启动! 2.确认开启Device  Install Service .Device Setup Ser ...

  9. TypeError: react__WEBPACK_IMPORTED_MODULE_2___default.a.createClass is not a function

    在看阮一峰的react入门的时候,写到一段代码,但是写完就报错了,经过多方查找,终于解决掉了 错误描述: 解决方法: 将React.createClass换成React.Component, 但是不知 ...

  10. 判断机器是big-endian、little-endian

    联合体union和大小端(big-endian.little-endian):下边示范了一种用途,代表四个含义的四个变量,但是可以用一个int来操作,直接int赋值,无论内存访问(指针大小的整数倍,访 ...