使用requestAnimationFrame和Canvas给按钮添加绕边动画
要给按钮添加酷炫的绕边动画,可以使用Canvas来实现。
基本的思路是创建一个和按钮大小相同的Canvas元素,内置在按钮元素中。然后在Canvas上实现边线环绕的动画。
requestAnimationFrame接口
我们使用requestAnimationFrame接口来实现动画帧的绘制,该接口告诉浏览器在重画(repaint)之前先执行一个动画回调函数。
通过这样的回调机制,浏览器可以把多个动画函数执行完成后再统一绘制,提高动画渲染性能。
(function() { for (var d = 0, a = ["webkit", "moz"], b = 0; b < a.length && !window.requestAnimationFrame; ++b) window.requestAnimationFrame = window[a[b] + "RequestAnimationFrame"], window.cancelAnimationFrame = window[a[b] + "CancelAnimationFrame"] || window[a[b] + "CancelRequestAnimationFrame"]; window.requestAnimationFrame || (window.requestAnimationFrame = function(b) { var a = (new Date).getTime(), c = Math.max(0, 16 - (a - d)), e = window.setTimeout(function() { b(a + c) }, c); d = a + c; return e }); window.cancelAnimationFrame || (window.cancelAnimationFrame = function(a) { clearTimeout(a) }) })();
上面实现的是一个兼容各浏览器并可以安全降级的requestAnimationFrame版本,当浏览器不支持时,回退到setTimeout/clearTimeout定时函数。
Border对象
现在我们需要创建一个Border对象,用来表示绕边的那个动画对象。该Border对象的宽高和按钮元素接近,并包含一个canvas对象。
function Border(opt) { // 参数opt即为传入的button元素 this.elem = opt.elem; this.active = false; this.canvas = document.createElement('canvas');//创建画布 this.ctx = this.canvas.getContext('2d');//获取画布上下文 this.width = this.canvas.width = this.elem.outerWidth();//设置宽 this.height = this.canvas.height = this.elem.outerHeight();//设置高 this.borderSize = parseInt(this.elem.css('border-left-width'), 10);//设置边宽 this.waypoints = [ [0, 0], [this.width - this.borderSize, 0], [this.width - this.borderSize, this.height - this.borderSize], [0, this.height - this.borderSize] ];//设置路标数组 this.tracer = { x: 0, y: 0, color: opt.color, speed: opt.speed, waypoint: 0 };//设置路径对象 this.canvas.style.top = -this.borderSize + 'px'; this.canvas.style.left = -this.borderSize + 'px'; this.elem.append($(this.canvas));//把canvas内置到button元素中。 }
然后我们实现循环绕边的函数,由于这是Border对象的一个行为,我们把它实现为其原型对象的一个方法:
Border.prototype.loop = function() { requestAnimationFrame($.proxy(this.loop, this));//定时调用自身 this.ctx.globalCompositeOperation = 'source-over';//源覆盖目标的合成模式 this.ctx.fillStyle = this.tracer.color; this.ctx.fillRect(this.tracer.x, this.tracer.y, this.borderSize, this.borderSize);//在当前路径上绘制小方块(本例是4*4px) //下面这段代码是通用算法,用来计算某段路径上x/y方向上的速度分量,同时判断下一步是否需要拐弯(在路标处) var previousWaypoint = (this.tracer.waypoint == 0) ? this.waypoints[this.waypoints.length - 1] : this.waypoints[this.tracer.waypoint - 1], dxTotal = previousWaypoint[0] - this.waypoints[this.tracer.waypoint][0], dyTotal = previousWaypoint[1] - this.waypoints[this.tracer.waypoint][1], distanceTotal = Math.sqrt(dxTotal * dxTotal + dyTotal * dyTotal),//该段路径总长度 angle = Math.atan2(this.waypoints[this.tracer.waypoint][1] - this.tracer.y, this.waypoints[this.tracer.waypoint][0] - this.tracer.x), vx = Math.cos(angle) * this.tracer.speed, vy = Math.sin(angle) * this.tracer.speed, dxFuture = previousWaypoint[0] - (this.tracer.x + vx), dyFuture = previousWaypoint[1] - (this.tracer.y + vy), distanceFuture = Math.sqrt(dxFuture * dxFuture + dyFuture * dyFuture);//在该路径上的总移动距离 if (distanceFuture >= distanceTotal) {//已移动距离超过路径长度,则需要拐弯,即更新路标 this.tracer.x = this.waypoints[this.tracer.waypoint][0]; this.tracer.y = this.waypoints[this.tracer.waypoint][1]; this.tracer.waypoint = (this.tracer.waypoint == this.waypoints.length - 1) ? 0 : this.tracer.waypoint + 1; } else {//否则,在当前路径上移动位置 this.tracer.x += vx; this.tracer.y += vy; } }
有了Border对象后,我们来实际使用它。(在C++等面向对象语言中,我们通常称上面定义了一个类Class,在实际使用时,需要创建类的实例,即对象Object)。
var button = $("#your_button_id")[0]; $this = $(button); var border = new Border({ elem: $this, color: $this.data('color'), speed: $this.data('speed') }); $(border.canvas).stop(true).animate({ 'opacity': 1 }, 400); border.loop();
这样我们就实现了一个按钮的绕边动画。
在线实例
你可以通过在线实例自己试试看,还可以基于这个简化版本添加渐变效果和鼠标悬停交互处理,乃至实现不规则形状按钮的描边动画。


最新评论
- 相关文章
3D感知和建模关键硬件技术:双目、3D结构光和TOF
无论VR、AR和3D打印,其核心技术包含3D成像和建模。而3D建模属于劳动密集型的工作,耗时耗力,凡这类工作都会是被新技术革命的地方,自动3D建模技术就是为了解决...
踏得网精选2016年度10大最佳HTML5动画
踏得网精选2016年度最酷最新的HTML5动画集,评选标准为:创意新颖度+实现技术难度+趣味程度。使用一些在线H5生成工具的作品,因其主要使用图片和CSS3套路动画,...
Blender2.7给平面模型添加纹理贴图
在blender中给模型添加纹理,需要有2个步骤:首先在对象属性栏中给该对象添加材料和纹理建立纹理映射添加材料和纹理这是常见操作,略过步骤。但是仅仅这样操作,...
HTML5、Hybrid APP、Native APP对比和技术选型
HTML5和Native APP都很容易理解。为了获得HTML5的移植性和移动本地应用的高性能,搞出来一些混合APP的解决方案。比如Apache的Cordova(也就是以前的PhoneGap),...
深度贴图(depth map)概念简介和生成流程
Depth map 深度图是一张2D图片,每个像素都记录了从视点(viewpoint)到遮挡物表面(遮挡物就是阴影生成物体)的距离,这些像素对应的顶点对于观察者而言是“可...
粒子运动模拟 - Verlet积分算法简介
Verlet算法是经典力学(牛顿力学)中的一种最为普遍的积分方法,被广泛运用在分子运动模拟(Molecular Dynamics Simulation),行星运动以及织物变形模拟等领域...
S3TC(S3 Texture Compression)纹理压缩格式详解
使用S3TC格式存储的压缩纹理是以4X4的纹理单元块(texel blocks)为基本单位存储的,每纹理单元块(texel blocks)有64bit或者128bit的纹理单元数据(texel data)。这...
WebGL入门教程5 - 详解纹理滤镜(Texture Filter)
WebGL中使用纹理贴图来实现细腻的物体表面观感,其中一个重要的参数是纹理滤镜(Texture Filter)。
这个参数用来处理当对象出现缩放时,纹理如何处理中间...WebGL入门教程1 - 3D绘图基础知识
现代浏览器努力使得Web用户体验更为丰富,而WebGL正处于这样的技术生态系统的中心位置。其应用范围覆盖在线游戏、大数据可视化、计算机辅助设计、虚拟现实以及数...
jQuery Ribbles - 基于WebGL的水面涟漪动效插件
使用jQuery
如何使用CSS3实现一个平滑的3D文本标题
要实现3D文本,基本上有3种方法:1. 使用CSS3的投影滤镜(filter: drop-shadow)2. 使用3d建模和CSS3 3d变换来实现(最真实)3. 使用CSS3 text-shadow属性来实现...
更多...