以本图为例,要做这张图,需要一些数学知识(三角函数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

版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接

最新文章

  1. 剑指Offer面试题:23.二叉树中和为某一值的路径
  2. 第1讲 Redis部署与基本操作
  3. Javascript定时器(三)——setTimeout(func, 0)
  4. H5-表格、表单
  5. OSGI框架学习
  6. java.util.Scanner的日常用法
  7. rebar
  8. Vsftpd3.0--FTP服务器搭建之本地用户篇
  9. NEO GUI 多方签名使用
  10. Exp4 恶意代码分析 20164303 景圣
  11. __FILES__
  12. is,as,类库
  13. General Test Scenarios
  14. VS2012打开项目——已停止工作
  15. Linux设置桌面图标 (双击运行jar包)
  16. lua -- 商店的数据管理类
  17. MySQL命令行导出、导入数据库,备份数据库表
  18. BOM和DOM(cs)
  19. YII assets使用
  20. 快速掌握Java中Lambda表达式的用法

热门文章

  1. Java 继承、多态与类的复用
  2. FusionCharts:tooltip分行显示
  3. 网络中常见的ping命令协议
  4. 自己动手写CPU之第五阶段(2)——OpenMIPS对数据相关问题的解决措施
  5. CORS解决WebApi跨域问题(转)
  6. 实验c语言不同类型的指针互用(不推荐只是学习用)
  7. [elk]elastalert邮箱告警
  8. 从零搭建Web网站
  9. JAVA工具类 UUID
  10. PHP命名空间规则解析及高级功能