JSON

HTML5 canvas 轴承旋转动画

字号+ 作者:H5之家 来源:H5之家 2015-11-11 13:03 我要评论( )

效果图: 一、HTML代码: !doctype html html head meta charset=utf-8 link href=style/style.css rel=stylesheet type=text/css title/title /head body canvas id=canvas/canvas script src=js/js.js type=text/javascript/script /body /html 二、CSS代

效果图:


一、HTML代码:
<!doctype html>
 <html>
  <head>
   <meta charset="utf-8">
   <link href="style/style.css" rel="stylesheet" type="text/css">
   <title></title>
  </head>
 <body>
  <canvas id="canvas"></canvas>
       <script src="js/js.js" type="text/javascript"></script>
 </body>
</html>

二、CSS代码:
body {
 margin: 0;
 padding: 0;
 overflow: hidden;
}
三、JS代码:
var canvas = document.getElementById('canvas'); var ctx = canvas.getContext('2d'); 'floor|random|round|abs|sqrt|PI|atan2|sin|cos|pow|max|min' .split('|') .forEach(function(p) { window[p] = Math[p]; }); var TAU = PI * 2; function r(n) { return random() * n; } function rrng(lo, hi) { return lo + r(hi - lo); } function rint(lo, hi) { return lo + floor(r(hi - lo + 1)); } function choose() { return arguments[rint(0, arguments.length - 1)]; } /*---------------------------------------------------------------------------*/ var W, H, frame, t0, time; var DPR = devicePixelRatio || 1; function dpr(n) { return n * DPR; } function resize() { var w = innerWidth; var h = innerHeight; canvas.style.width = w + 'px'; canvas.style.height = h + 'px'; W = canvas.width = w * DPR; H = canvas.height = h * DPR; } function loop(t) { frame = requestAnimationFrame(loop); draw(); time++; } function pause() { cancelAnimationFrame(frame); frame = frame ? null : requestAnimationFrame(loop); } function reset() { cancelAnimationFrame(frame); resize(); ctx.clearRect(0, 0, W, H); init(); time = 0; frame = requestAnimationFrame(loop); } /*---------------------------------------------------------------------------*/ function gray(n) { return 'rgba(0, 0, 0,' + lerp(0.2, 0.55, n) + ')'; } function lerp(a, b, n) { return a + (b - a) * n; } function lerpProp(prop, a, b, n) { return lerp(a[prop], b[prop], n); } function Ring(x, y, rad, w) { this.x = x; this.y = y; this.rad = rad; this.w = w; this.from = { start: 0.75 * TAU, end: 0.75 * TAU, color: r(1) }; this.to = { start: 0.75 * TAU, end: 0.75 * TAU, color: this.from.color }; } Ring.prototype.update = function() { this.from = this.to; this.to = { start: (this.to.start + rrng(-TAU, TAU)) % TAU, end: (this.to.end + rrng(-TAU, TAU)) % TAU, color: r(1) }; }; Ring.prototype.draw = function(n) { var start = lerpProp('start', this.from, this.to, n); var end = lerpProp('end', this.from, this.to, n); var color = gray(lerpProp('color', this.from, this.to, n)); ctx.beginPath(); ctx.strokeStyle = color; ctx.lineWidth = this.w; ctx.arc(this.x, this.y, this.rad, start, end, end - start < 0); ctx.stroke(); }; /*---------------------------------------------------------------------------*/ var STEP = 180; var rings; function init() { rings = []; var cx = floor(W / 2); var cy = floor(H / 2); var spc = dpr(150); var ringspc = dpr(10); var ringw = dpr(9); for (var x = cx - spc; x

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • 举例详解HTML5中使用JSON格式提交表单

    举例详解HTML5中使用JSON格式提交表单

    2016-01-15 10:21

  • Jack 张 讲:HTML5 中websocket长连接的具体实现方法

    Jack 张 讲:HTML5 中websocket长连接的具体实现方法

    2016-01-01 17:38

  • 突袭HTML5之Javascript API扩展3—本地存储全新体验

    突袭HTML5之Javascript API扩展3—本地存储全新体验

    2015-11-25 09:21

  • 轻松学习JavaScript三:JavaScript与HTML的结合

    轻松学习JavaScript三:JavaScript与HTML的结合

    2015-11-21 13:15

网友点评
i