使用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建模技术就是为了解决...
CSS3原生变量(Native Variables)新特性简介
对Web开发者来说,一个盼望已久的特性是CSS终于支持原生变量了!
变量是程序语言中用来解决代码重复和进行表达式计算的关键概念(想想数学方程式中的x)。...React JSX语法简介
JSX是一种类似XML的标签语法,用来简化代码,我们可以不使用JSX,但了解并使用也没什么坏处:)在React中,JSX是一个使用 React.createElement() API的快捷方式...
通过实例深入理解HTML5/CSS3/SVG/WebGL的技术本质
学习使用CSS制作进度条
进度条是基础的界面控件,可用于多种场合,比如任务完成进度,手机充电状态等。本文介绍一个简单实用的进度条制作方法。预期效果如下图所示:直观上,我们可以把该进度条控件分为2个部分,外部的边界用来表示固定的目标范围,里面的条形部分用来表示当前进度。外部目标范围元素的CSS代码编写如下:.pb-scope
S3TC(S3 Texture Compression)纹理压缩格式详解
使用S3TC格式存储的压缩纹理是以4X4的纹理单元块(texel blocks)为基本单位存储的,每纹理单元块(texel blocks)有64bit或者128bit的纹理单元数据(texel data)。这...
如何使用WebGL实现空气高温热变形动画特效
我们在炎炎夏日,或者在火堆旁,经常会观察到热源周围空气的不稳定波动现象。本文将讲解如何通过WebGL来实现这个特效。该效果可用于热变形、波浪、水面波光等场...
如何实现SVG clipPath自适应被裁剪对象
CSS3中引入的clip-path(裁剪路径)属性是一个很强大的特性。
clip-path的含义如下图所示,好比剪纸一样,你用剪刀沿着某条路径把目标对象(图像或元素)裁...WebGL入门教程2 - GPU基本概念和工作流水线(渲染管道)
如何基于Canvas来模拟真实雨景Part2:重力掉落和雨滴融合
Canvas实例教程:图像移动、大小调整和裁剪
本文介绍如何使用JavaScript和HTML5 Canvas元素来移动、调整大小和...
使用纯CSS3实现一个3D旋转的书本
有一些前沿的电商网站已经开始使用3D模型来展示商品并支持在线定制,而其中图书的展示是最为简单的一种,无需复杂的建模过程,使用图片和CSS3的一些变换即可实现...
在PHP网页程序中执行Sass/Compass命令
我们需要在wow云开发平台支持sass/compass等预编译样式语言,为此我们首先尝试了scssphp扩展,但是在支持最新语法上,经常会出现异常。所以我们采用了代理的方式...
更多...