Javascript-Canvas实现三角函数曲线动画图
以本图为例,要做这张图,需要一些数学知识(三角函数sin,cos),有canvas的基础知识
Html
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <meta charset="utf-8" /> <script src="Script/jquery-1.10.2.js"></script> <script src="Script/testStudy.js"></script> </head> <body style="width:100%; height:100%;"> <canvas id="study" width="600" height="360">您的浏览器不支持 HTML5 canvas 标签。</canvas> <script> curve(); </script> </body> </html>
JavaScript
var curve = function () { var c = document.getElementById("study"); var ctx = c.getContext("2d"); //线条主色 var mianColor = "#e2dedb" //画图函数 var Drawing = { line: function (starxy, endxy, lineWidth, color) { ctx.strokeStyle = color; ctx.lineWidth = lineWidth; ctx.beginPath(); ctx.moveTo(starxy[0], starxy[1]); ctx.lineTo(endxy[0], endxy[1]); ctx.stroke(); }, ract: function (starxy, ractWH, color) { ctx.fillStyle = color; ctx.fillRect(starxy[0], starxy[1], ractWH[0], ractWH[1]); }, ractBorder: function (starxy, ractWH, color, lineWidth) { ctx.lineWidth = lineWidth; ctx.strokeStyle = color; ctx.strokeRect(starxy[0], starxy[1], ractWH[0], ractWH[1]); }, /*Drawing.triCurve(triangle, star, cp, color) triangle:三角函数 star:{x:开始x坐标,y:开始的y坐标} cp:{xw:控制x的宽度,yh:控制y的高度,s:位移} color:线条颜色 */ triCurve: function (triangle, star, cp, color) { ctx.strokeStyle = color; ctx.beginPath(); ctx.moveTo(star.x, star.y); //中心判断 var change = false; //中点计算 var count = cp.yh; for (var i = cp.s; i * cp.xw - cp.xw * cp.s + star.x < 600 ; i += 0.1) { count += 0.5; } var center = parseInt(count / 2); //三角函数曲线 for (var i = cp.s; i * cp.xw - cp.xw * cp.s + star.x < 600; i += 0.1) { if (change === false && cp.yh < center) { cp.yh += 0.5; } if (change === false && cp.yh === center) { change = true; cp.yh -= 0.5; } if (change === true && cp.yh < center) { cp.yh -= 0.5; } var x = i * cp.xw - cp.xw * cp.s + star.x; var y = Math[triangle](i * cp.angle) * cp.yh + star.y; ctx.lineTo(x, y); } ctx.stroke(); ctx.closePath(); }, } //背景填充 Drawing.ract([0, 0], [600, 360], "#333333"); //播放条 var play = function (btn) { ctx.clearRect(0, 0, 600, 130); Drawing.ract([0, 0], [600, 130], "#333333"); var playLineStar = [93, 105]; var playLineWH = [494, 105]; var playBtnWH = [11, 21]; Drawing.line(playLineStar, playLineWH, 4, mianColor); var playBtnStarX; var playBtnStarY = playLineWH[1] - playBtnWH[1] / 2; btn == undefined ? playBtnStarX = (playLineWH[0] + playLineStar[0]) / 2 - playBtnWH[0] / 2 : playBtnStarX = 4 * btn + playLineStar[0]; Drawing.ractBorder([playBtnStarX, playBtnStarY], playBtnWH, "#9d9996", 1); Drawing.ract([playBtnStarX += 1, playBtnStarY += 1], [playBtnWH[0] - 2, playBtnWH[1] - 2], mianColor); } play(); //鼠标滚动控制播放条 var waveGo; var distance = 0; var scrollFunc = function (e) { e = e || window.event; var mouseMove; e.wheelDelta ? mouseMove = e.wheelDelta : mouseMove = e.detail; if (mouseMove > 0) { distance += 5; if (distance >= 100) distance = 100; }//up if (mouseMove < 0) { distance -= 5; if (distance <= 0) distance = 0; }//down play(distance); clearInterval(waveGo); waveGo = setInterval(function () { wave(distance); }, 100) } if (document.addEventListener) {//firefox document.addEventListener('DOMMouseScroll', scrollFunc, false); } window.onmousewheel = document.onmousewheel = scrollFunc;//IE/Opera/Chrome //三角函数曲线动画 var dong = 1, time = 1000 / 60, cango; var wave = function (ad) { var adjust; ad == undefined ? adjust = 28 : adjust = ad + 15 >= 100 ? 100 : ad + 15; console.log(adjust); ctx.clearRect(0, 130, 600, 360); Drawing.ract([0, 130], [600, 360], "#333333"); if (dong === 1) { cango = false; } if (!cango && dong < 3) { dong += 1; } if (dong === 3) { cango = true; } if (cango && dong <= 3) { dong -= 1; } time++; Drawing.triCurve("sin", { x: 0, y: 238 }, { xw: adjust, yh: 5 * (1 + dong / 10), angle: Math.PI / 2, s: time / 10 }, "#ffffff"); Drawing.triCurve("cos", { x: 0, y: 238 }, { xw: adjust + 12, yh: 5 * (1 + dong / 10), angle: Math.PI / 2, s: time / 10 }, "#424242"); Drawing.triCurve("sin", { x: 0, y: 238 }, { xw: adjust + 22, yh: 5 * (1 + dong / 10), angle: Math.PI / 2, s: time / 10 }, "#5e5e5e"); Drawing.triCurve("cos", { x: 0, y: 238 }, { xw: adjust + 22, yh: 5 * (1 + dong / 10), angle: Math.PI / 2, s: time / 10 }, "#a2a2a2"); Drawing.triCurve("sin", { x: 0, y: 238 }, { xw: adjust, yh: 5 * (1 + dong / 10), angle: Math.PI / 2, s: time / 10 }, "#9a9a9a"); } waveGo = setInterval(wave, 100) }
最终一开始出来呈现的图像应该是,播放条居中,曲线图与实例图相似。
而我们的播放条,简单实现了利用鼠标滚动去控制曲线图的弯曲幅度。
作者:leona
原文链接:http://www.cnblogs.com/leona-d/p/6369905.html
版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接