动画原理——线性来回运动&&波动
书籍名称:HTML5-Animation-with-JavaScript
书籍源码:https://github.com/lamberta/html5-animation
1.在正选函数中,随角度的增大,sin的值徘徊在正一和负一之间。如下图。这可以用做物体的来回运动。
2.动画源码
index.html
- <!doctype html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>Bobbing 2</title>
- <link rel="stylesheet" href="../include/style.css">
- </head>
- <body>
- <header>
- Example from <a href="http://amzn.com/1430236655?tag=html5anim-20"><em>Foundation HTML5 Animation with JavaScript</em></a>
- </header>
- <canvas id="canvas" width="400" height="400"></canvas>
- <script src="../include/utils.js"></script>
- <script src="./classes/ball.js"></script>
- <script>
- window.onload = function () {
- var canvas = document.getElementById('canvas'),
- context = canvas.getContext('2d'),
- ball = new Ball(),
- angle = 0,
- centerY = 200,
- range = 50,
- speed = 0.05;
- ball.x = canvas.width / 2;
- (function drawFrame () {
- window.requestAnimationFrame(drawFrame, canvas);
- context.clearRect(0, 0, canvas.width, canvas.height);
- ball.y = centerY + Math.sin(angle) * range;
- angle += speed;
- ball.draw(context);
- }());
- };
- </script>
- </body>
- </html>
style.css
- /* Some HTML5 Tags
- */
- aside, footer, header, nav, section {
- display: block;
- }
- /* Examples
- */
- body {
- background-color: #bbb;
- color: #383838;
- }
- #canvas {
- background-color: #fff;
- }
- header {
- padding-bottom: 10px;
- }
- header a {
- color: #30f;
- text-decoration: none;
- }
- aside {
- padding-top: 6px;
- }
- /* Index page
- */
- #index-body {
- background-color: #fdeba1;
- font-family: "Vollkorn", serif;
- color: #000;
- }
- #index-body a {
- text-decoration: none;
- color: #b30300;
- }
- #index-body #description, #index-body #exercises {
- overflow: auto;
- max-width: 900px;
- margin: 0px auto 20px auto;
- padding-left: 15px;
- padding-bottom: 15px;
- background-color: #fff;
- border-radius: 15px;
- }
- #index-body #description {
- margin-top: 40px;
- }
- #index-body h1 {
- color: #b30300;
- }
- #index-body #description h2 {
- margin-bottom:;
- }
- #index-body h1 a {
- text-decoration: underline;
- color: #b30300;
- }
- #index-body li h2, #index-body li h3, #index-body li h4 {
- color: #000;
- }
- #index-body li h3 {
- margin-bottom: 0px;
- }
- #index-body #description ul {
- margin:;
- padding:;
- list-style-type: none;
- }
- #index-body #description ul li {
- padding-bottom: 0.6em;
- }
- .container {
- display: table;
- width: 100%;
- height: auto;
- }
- .container .text {
- display:table-cell;
- height:100%;
- vertical-align:middle;
- }
- .container img {
- padding: 0 20px;
- display: block;
- float: right;
- }
- .container .clear {
- clear: both;
- }
- #exercises ul {
- margin:;
- padding: 4px 20px 10px 20px;
- }
- #exercises ol {
- margin: 0 20px 10px 0;
- padding:;
- list-style-type: none;
- }
- #exercises ol li {
- padding-top: 5px;
- }
- #exercises ol ol ol {
- padding-left: 60px;
- list-style-type: decimal-leading-zero;
- }
- #exercises ol ol ol li img, #exercises ol ol li img {
- margin-left: 4px;
- margin-bottom: -10;
- }
- #exercises h2 {
- margin: 10px 0 0 0;
- }
utils.js
- /**
- * Normalize the browser animation API across implementations. This requests
- * the browser to schedule a repaint of the window for the next animation frame.
- * Checks for cross-browser support, and, failing to find it, falls back to setTimeout.
- * @param {function} callback Function to call when it's time to update your animation for the next repaint.
- * @param {HTMLElement} element Optional parameter specifying the element that visually bounds the entire animation.
- * @return {number} Animation frame request.
- */
- if (!window.requestAnimationFrame) {
- window.requestAnimationFrame = (window.webkitRequestAnimationFrame ||
- window.mozRequestAnimationFrame ||
- window.msRequestAnimationFrame ||
- window.oRequestAnimationFrame ||
- function (callback) {
- return window.setTimeout(callback, 17 /*~ 1000/60*/);
- });
- }
- /**
- * ERRATA: 'cancelRequestAnimationFrame' renamed to 'cancelAnimationFrame' to reflect an update to the W3C Animation-Timing Spec.
- *
- * Cancels an animation frame request.
- * Checks for cross-browser support, falls back to clearTimeout.
- * @param {number} Animation frame request.
- */
- if (!window.cancelAnimationFrame) {
- window.cancelAnimationFrame = (window.cancelRequestAnimationFrame ||
- window.webkitCancelAnimationFrame || window.webkitCancelRequestAnimationFrame ||
- window.mozCancelAnimationFrame || window.mozCancelRequestAnimationFrame ||
- window.msCancelAnimationFrame || window.msCancelRequestAnimationFrame ||
- window.oCancelAnimationFrame || window.oCancelRequestAnimationFrame ||
- window.clearTimeout);
- }
- /* Object that contains our utility functions.
- * Attached to the window object which acts as the global namespace.
- */
- window.utils = {};
- /**
- * Keeps track of the current mouse position, relative to an element.
- * @param {HTMLElement} element
- * @return {object} Contains properties: x, y, event
- */
- window.utils.captureMouse = function (element) {
- var mouse = {x: 0, y: 0, event: null},
- body_scrollLeft = document.body.scrollLeft,
- element_scrollLeft = document.documentElement.scrollLeft,
- body_scrollTop = document.body.scrollTop,
- element_scrollTop = document.documentElement.scrollTop,
- offsetLeft = element.offsetLeft,
- offsetTop = element.offsetTop;
- element.addEventListener('mousemove', function (event) {
- var x, y;
- if (event.pageX || event.pageY) {
- x = event.pageX;
- y = event.pageY;
- } else {
- x = event.clientX + body_scrollLeft + element_scrollLeft;
- y = event.clientY + body_scrollTop + element_scrollTop;
- }
- x -= offsetLeft;
- y -= offsetTop;
- mouse.x = x;
- mouse.y = y;
- mouse.event = event;
- }, false);
- return mouse;
- };
- /**
- * Keeps track of the current (first) touch position, relative to an element.
- * @param {HTMLElement} element
- * @return {object} Contains properties: x, y, isPressed, event
- */
- window.utils.captureTouch = function (element) {
- var touch = {x: null, y: null, isPressed: false, event: null},
- body_scrollLeft = document.body.scrollLeft,
- element_scrollLeft = document.documentElement.scrollLeft,
- body_scrollTop = document.body.scrollTop,
- element_scrollTop = document.documentElement.scrollTop,
- offsetLeft = element.offsetLeft,
- offsetTop = element.offsetTop;
- element.addEventListener('touchstart', function (event) {
- touch.isPressed = true;
- touch.event = event;
- }, false);
- element.addEventListener('touchend', function (event) {
- touch.isPressed = false;
- touch.x = null;
- touch.y = null;
- touch.event = event;
- }, false);
- element.addEventListener('touchmove', function (event) {
- var x, y,
- touch_event = event.touches[0]; //first touch
- if (touch_event.pageX || touch_event.pageY) {
- x = touch_event.pageX;
- y = touch_event.pageY;
- } else {
- x = touch_event.clientX + body_scrollLeft + element_scrollLeft;
- y = touch_event.clientY + body_scrollTop + element_scrollTop;
- }
- x -= offsetLeft;
- y -= offsetTop;
- touch.x = x;
- touch.y = y;
- touch.event = event;
- }, false);
- return touch;
- };
- /**
- * Returns a color in the format: '#RRGGBB', or as a hex number if specified.
- * @param {number|string} color
- * @param {boolean=} toNumber=false Return color as a hex number.
- * @return {string|number}
- */
- window.utils.parseColor = function (color, toNumber) {
- if (toNumber === true) {
- if (typeof color === 'number') {
- return (color | 0); //chop off decimal
- }
- if (typeof color === 'string' && color[0] === '#') {
- color = color.slice(1);
- }
- return window.parseInt(color, 16);
- } else {
- if (typeof color === 'number') {
- color = '#' + ('00000' + (color | 0).toString(16)).substr(-6); //pad
- }
- return color;
- }
- };
- /**
- * Converts a color to the RGB string format: 'rgb(r,g,b)' or 'rgba(r,g,b,a)'
- * @param {number|string} color
- * @param {number} alpha
- * @return {string}
- */
- window.utils.colorToRGB = function (color, alpha) {
- //number in octal format or string prefixed with #
- if (typeof color === 'string' && color[0] === '#') {
- color = window.parseInt(color.slice(1), 16);
- }
- alpha = (alpha === undefined) ? 1 : alpha;
- //parse hex values
- var r = color >> 16 & 0xff,
- g = color >> 8 & 0xff,
- b = color & 0xff,
- a = (alpha < 0) ? 0 : ((alpha > 1) ? 1 : alpha);
- //only use 'rgba' if needed
- if (a === 1) {
- return "rgb("+ r +","+ g +","+ b +")";
- } else {
- return "rgba("+ r +","+ g +","+ b +","+ a +")";
- }
- };
- /**
- * Determine if a rectangle contains the coordinates (x,y) within it's boundaries.
- * @param {object} rect Object with properties: x, y, width, height.
- * @param {number} x Coordinate position x.
- * @param {number} y Coordinate position y.
- * @return {boolean}
- */
- window.utils.containsPoint = function (rect, x, y) {
- return !(x < rect.x ||
- x > rect.x + rect.width ||
- y < rect.y ||
- y > rect.y + rect.height);
- };
- /**
- * Determine if two rectangles overlap.
- * @param {object} rectA Object with properties: x, y, width, height.
- * @param {object} rectB Object with properties: x, y, width, height.
- * @return {boolean}
- */
- window.utils.intersects = function (rectA, rectB) {
- return !(rectA.x + rectA.width < rectB.x ||
- rectB.x + rectB.width < rectA.x ||
- rectA.y + rectA.height < rectB.y ||
- rectB.y + rectB.height < rectA.y);
- };
ball.js
- function Ball (radius, color) {
- if (radius === undefined) { radius = 40; }
- if (color === undefined) { color = "#ff0000"; }
- this.x = 0;
- this.y = 0;
- this.radius = radius;
- this.rotation = 0;
- this.scaleX = 1;
- this.scaleY = 1;
- this.color = utils.parseColor(color);
- this.lineWidth = 1;
- }
- Ball.prototype.draw = function (context) {
- context.save();
- context.translate(this.x, this.y);
- context.rotate(this.rotation);
- context.scale(this.scaleX, this.scaleY);
- context.lineWidth = this.lineWidth;
- context.fillStyle = this.color;
- context.beginPath();
- //x, y, radius, start_angle, end_angle, anti-clockwise
- context.arc(0, 0, this.radius, 0, (Math.PI * 2), true);
- context.closePath();
- context.fill();
- if (this.lineWidth > 0) {
- context.stroke();
- }
- context.restore();
- };
3.波动实际是在上下移动的基础上x一直递增。
在原页面index.html的基础上简单修改一下就可以
- <!doctype html>
- <html>
- <head>
- <meta charset="utf-8">
- <title>Bobbing 2</title>
- <link rel="stylesheet" href="../include/style.css">
- </head>
- <body>
- <header>
- Example from <a href="http://amzn.com/1430236655?tag=html5anim-20"><em>Foundation HTML5 Animation with JavaScript</em></a>
- </header>
- <canvas id="canvas" width="400" height="400"></canvas>
- <script src="../include/utils.js"></script>
- <script src="./classes/ball.js"></script>
- <script>
- window.onload = function () {
- var canvas = document.getElementById('canvas'),
- context = canvas.getContext('2d'),
- ball = new Ball(),
- angle = 0,
- centerY = 200,
- range = 50,
- xspeed = 1,
- speed = 0.05;
- ball.x = canvas.width / 2;
- (function drawFrame () {
- window.requestAnimationFrame(drawFrame, canvas);
- context.clearRect(0, 0, canvas.width, canvas.height);
- ball.x += xspeed;
- ball.y = centerY + Math.sin(angle) * range;
- angle += speed;
- ball.draw(context);
- }());
- };
- </script>
- </body>
- </html>
动画原理——线性来回运动&&波动的更多相关文章
- Atitit 视频编码与动画原理attilax总结
Atitit 视频编码与动画原理attilax总结 1.1. 第一步:实现有损图像压缩和解压1 1.2. 接着将其量化,所谓量化,就是信号采样的步长,1 1.3. 第二步:实现宏块误差计算2 1.4. ...
- iOS动画原理
1. iOS动画原理 本质:动画对象(这里是UIView)的状态,基于时间变化的反应 分类:可以分为显式动画(关键帧动画和逐帧动画)和隐式动画 关键帧和逐帧总结:关键帧动画的实现方式,只需要修改某个属 ...
- Unity3D 骨骼动画原理学习笔记
最近研究了一下游戏中模型的骨骼动画的原理,做一个学习笔记,便于大家共同学习探讨. ps:最近改bug改的要死要活,博客写的吭哧吭哧的~ 首先列出学习参考的前人的文章,本文较多的参考了其中的表述: 1. ...
- OpenGL10-骨骼动画原理篇(2)
接上一篇的内容,上一篇,简单的介绍了,骨骼动画的原理,给出来一个 简单的例程,这一例程将给展示一个最初级的人物动画,具备多细节内容 以人走路为例子,当人走路的从一个站立开始,到迈出一步,这个过程是 一 ...
- js中动画原理
现如今,许多页面上均有一些动画效果.适当的动画效果可以在一定程度上提高页面的美观度,具有提示效果的动画可以增强页面的易用性. 实现页面动画的途径一般有两种. 一种是通过操作JavaScript间接操作 ...
- SVG描边动画原理
SVG描边动画原理其实很简单,主要利用以下两个属性 stroke-dasharray 制作虚线,使得黑白相间, stroke-dashoffset 使得虚线向开头偏移,这里的1500不精确,是我随便取 ...
- JS实现动画原理一(闭包方式)
前提: 你必须了解js的闭包(否则你看不懂滴) 我们就来做一个js实现jq animate的动画效果来简单探索一下,js动画实现的简单原理: html代码 <div id=&q ...
- JS 实现无缝滚动动画原理(初学者入)
这段时间在教培训班的学生使用原生javascript实现无缝滚动的动画案例,做了这个原理演示的动画,分享给自学JS的朋友!博主希望对你们有帮助! 在讲解之前先看一下demo: demo:https:/ ...
- OpenGL10-骨骼动画原理篇(3)-Shader版本代码已经上传
视频教程请关注 http://edu.csdn.net/lecturer/lecturer_detail?lecturer_id=440 接上一个例程OpenGL10-骨骼动画原理篇(2),对骨骼动画 ...
随机推荐
- SQL Server 2008数据库的一些基本概念 区、页、行
原文地址:http://www.cnblogs.com/liuzhendong/archive/2011/10/11/2207361.html 以前总是没弄明白这些基本概念,现在整理如下: 1.区: ...
- Java报表开发组件DynamicReports
DynamicReports 是一个基于 JasperReports 进行扩展的 Java 报表库,可用它来快速创建报表而无需可视化报表设计工具. From : http://www.oschina ...
- Unable to resolve target 'android-XX'的问题解决
1.问题现象(Unable to resolve target 'android-XX') 将低版本的代码导入eclipse时,常遇到这样的问题:Unable to resolve target 'a ...
- hdu 4686 Arc of Dream(矩阵快速幂乘法)
Problem Description An Arc of Dream is a curve defined by following function: where a0 = A0 ai = ai- ...
- Spring Http Invoker
配置例如以下: ①web.xml配置 <servlet> <servlet-name>remote</servlet-name> <servlet-class ...
- Bctf-pwn_ruin-re_lastflower
Pwn-ruin 用几个词来概括下漏洞原理:Arm+heap overflow(house of force)+dl-resolve Info leak: 在printf key8时,泄漏堆上地址(s ...
- git clone 命令报错 +diffie-hellman-group1-sha1
解决方法: 在.ssh目录下新建文件config , 添加 Host * KexAlgorithms +diffie-hellman-group1-sha1 到文件config,即可.
- mysql 查询表的行数和空间使用及其它信息
use information_schema; select concat(round(sum(DATA_LENGTH/1024/1024), 2), 'MB') as data from TABLE ...
- 用apiCloud开发应用
使用apiCloud开发应用就是用html5写页面,css实现样式,js写功能.一套代码在android和ios上都能运行.节省开发周期和人员开销. 代码可以放到云服务器,可以云端打包,云端更新. a ...
- 下载PHPDroid: 基于WebView和PHP内置HTTP服务器开发Android应用
基于Android上的PHP(比如我打包的PHPDroid),寥寥几行PHP代码,就能实现一个支持无线局域网用浏览器访问的Android手机的Shell,用于执行命令和PHP代码. 个人在 ...