canvas动画3

时隔很久,本人终于又写博客了(重度拖延症),把之前留下的canvas交互动画讲一讲。

电脑上的交互,指的是鼠标和键盘,我们今天主要用的是鼠标。

既然是鼠标的话,就要获取鼠标的各种事件,这次以mousemove做示例。

我们先定义一个鼠标对象,然后添加mousemove事件:

var mouse = {x: undefined,y: undefined
}
//这样的话控制台就会跟随我们的鼠标移动输出相应的坐标
window.addEventListener("mousemove",function (event) {mouse.x = event.x;mouse.y = event.y;console.log(mouse);
});

我们声明一个初始化函数init(),用于把制造圆的过程封装:

function init() {circleArray = []for (var i = 0; i < 800; i++) {var x = Math.random()*window.innerWidth;var y = Math.random()*window.innerHeight;var dx = (Math.random()-0.5)*2;var dy = (Math.random()-0.5)*2;var radius = Math.random()*3 +1;circleArray.push(new Circle(x, y, dx, dy, radius));}
}
init();

这样的话,按照上一篇文章的做法,我们的canvas会出现一些问题。所以,需要给Circle对象update()里的属性都加上this

function Circle(x, y, dx, dy, radius) {this.x = x;this.y = y;this.dx = dx;this.dy = dy;this.radius = radius;this.draw = function() {ctx.beginPath();ctx.strokeStyle = "#000";ctx.arc(this.x,this.y,this.radius,Math.PI/180*0,Math.PI/180*360,false);ctx.stroke();ctx.fill();}this.update = function() {//圆触碰边界时反弹,偏移值为负if (this.x + this.radius > innerWidth || this.x - this.radius < 0 ) {this.dx = -this.dx;}if (this.y + this.radius > innerHeight || this.y - this.radius < 0 ) {this.dy = -this.dy;}//刷新绘制时圆的偏移运动this.x += this.dx;this.y += this.dy;//根据更新的值进行绘制this.draw();}
}

接下来我们就要用mousemove于动画进行交互了,我们假定圆心在鼠标坐标周围50px以内的时候圆会增大,这段代码应该写在update()里:

if (mouse.x - this.x < 50 && mouse.x - this.x >-50 && mouse.y - this.y < 50 && mouse.y - this.y >-50) {// if (this.radius < maxRadius) {this.radius += 1;// }
}

我们设置了10个圆,把鼠标移上去的时候会看到在控制范围内的圆会不断变大,不会停下来,所以我在前面就设置了一个圆半径的最大值,以免它一直增大,然后把注释的内容去掉,圆就不会无限增大了:

但是有一个问题,圆放大了以后不会缩小,那么我们就让它在离开圆50px半径范围后缩小:

if (mouse.x - this.x < 50 && mouse.x - this.x >-50 && mouse.y - this.y < 50 && mouse.y - this.y >-50) {if (this.radius < maxRadius) {this.radius += 1;}
//其他的所有圆半径不断减小
}else{this.radius -= 1;
}

这时候又有新问题产生了,画面一片空白,因为圆心不在鼠标固定范围内的圆全都变小了,甚至半径为负!显然简单的else是不成立的,还是得加个条件:

if (mouse.x - this.x < 50 && mouse.x - this.x >-50 && mouse.y - this.y < 50 && mouse.y - this.y >-50) {if (this.radius < maxRadius) {this.radius += 1;}
//其他的所有圆半径减小到最小值
}else if (this.radius > this.minRadius) {this.radius -= 1;
}

这里出现了一个新值:minRadius,我加在了Circle对象里this.minRadius = radius;,以原本的初始值作为它的最小值。好了,现在基本效果已经成型了:

接下来就是颜色的问题了,只要懂得canvas的基本api,修改颜色完全就是小儿科。我们设置一个数组,用于存放颜色值。

var colorArray = ['#58D68D','#E67F22','#3598DB','#E84C3D','#9A59B5','#27AE61','#D25400','#BEC3C7','#297FB8'
]

然后在Circle对象里加上一个bg属性:this.bg = colorArray[Math.floor(Math.random()*colorArray.length)];,再往Circle的绘制函数添上一句ctx.fillStyle = this.bg;,然后ctx.fill();,多彩运动的圆圈canvas就做完了。

这是一个运用mousemove事件做的canvas交互动画,有兴趣的可以尝试其他事件(制作游戏用的键盘事件以及其他鼠标事件),或者思考如何给球加重力,如何检测碰撞事件,canvas的世界并不只有这么一点,相关资料的话,给大家推荐本书《canvas开发详解》。

本文的最终js代码如下:

/*** * @authors dkplus (dkplus@qq.com)* @date    2017-10-01 20:37:26* @version $1.0$*/
/*** 获取canvas对象,设置宽度高度自适应* @type {[type]}*/
var canvas = document.querySelector("#canvas");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;var ctx = canvas.getContext("2d");
/*** 屏幕鼠标坐标* @type {Object}*/
var mouse = {x: undefined,y: undefined
}
/*** @param  {鼠标移动事件,回调函数,赋值给鼠标坐标}* @return {[type]}*/
window.addEventListener("mousemove",function (event) {mouse.x = event.x;mouse.y = event.y;// console.log(mouse);
});
/*** @param  {重新设置窗口大小,使canvas宽高自适应屏幕}* @return {[type]}*/
window.addEventListener("resize",function () {canvas.width = window.innerWidth;canvas.height = window.innerHeight;//初始化canvasinit();
})
//绘制圆的最大半径
var maxRadius = 40;
// var minRadius = 2;//圆的颜色数组
var colorArray = ['#58D68D','#E67F22','#3598DB','#E84C3D','#9A59B5','#27AE61','#D25400','#BEC3C7','#297FB8'
]
/*** @param {x圆中心的x坐标}* @param {y圆中心的y坐标}* @param {dx圆运动的x偏移量}* @param {dy圆运动的y偏移量}* @param {radius圆的半径}* minRadius圆的最小半径* bg圆的背景颜色* draw绘制函数* update圆运动偏移*/
function Circle(x, y, dx, dy, radius) {this.x = x;this.y = y;this.dx = dx;this.dy = dy;this.radius = radius;this.minRadius = radius;this.bg = colorArray[Math.floor(Math.random()*colorArray.length)];this.draw = function() {ctx.beginPath();ctx.strokeStyle = "#777";ctx.fillStyle = this.bg;ctx.arc(this.x,this.y,this.radius,Math.PI/180*0,Math.PI/180*360,false);// ctx.stroke();ctx.fill();}this.update = function() {//圆触碰边界时反弹,偏移值为负if (this.x + this.radius > innerWidth || this.x - this.radius < 0 ) {this.dx = -this.dx;}if (this.y + this.radius > innerHeight || this.y - this.radius < 0 ) {this.dy = -this.dy;}//刷新绘制时圆的偏移运动this.x += this.dx;this.y += this.dy;//鼠标半径50像素范围内的圆,它们的半径逐步增加到最大值if (mouse.x - this.x < 50 && mouse.x - this.x >-50 && mouse.y - this.y < 50 && mouse.y - this.y >-50) {if (this.radius < maxRadius) {this.radius += 1;}//其他的所有圆半径减小到最小值}else if (this.radius > this.minRadius) {this.radius -= 1;}//根据更新的值进行绘制this.draw();}
}
//圆的对象数组
var circleArray = [];
/*** 初始化函数,制造800个随机坐标、偏移速度和半径的圆,加入到对象数组* @return {[type]}*/
function init() {circleArray = []for (var i = 0; i < 800; i++) {var x = Math.random()*window.innerWidth;var y = Math.random()*window.innerHeight;var dx = (Math.random()-0.5)*2;var dy = (Math.random()-0.5)*2;var radius = Math.random()*3 +1;circleArray.push(new Circle(x, y, dx, dy, radius));}
}
init();
/*** 动画函数* @return {[type]}*/
function animate() {//更新前清楚画布ctx.clearRect(0,0,window.innerWidth,window.innerHeight);requestAnimationFrame(animate);//每个圆都调用update()方法for (var i = 0; i < circleArray.length; i++) {circleArray[i].update();}}
animate();

转载于:https://www.cnblogs.com/dkplus/p/7887482.html

canvas动画3:交互相关推荐

  1. canvas动画 时钟动画 太阳系动画 动态蚂蚁线 全景照片

    canvas给我们提供了绘图API,这些API基于JavaScript实现,那么我们可以方便的实现一些动画,在这里我们将展示几个经典的动画绘制技巧,包括:时钟,太阳系,动态蚂蚁线,全景照片 动画绘制的 ...

  2. canvas动画:自由落体运动

    经过前面的文章,我们已经能够在canvas画布上画出各种炫酷的图形和画面,但是这些画面都是禁止的,怎么样才能让他们动起来呢? 如何绘制基本图形可以参考:canvas基本图形绘制 如何对基本图形移动旋转 ...

  3. Canvas 动画引擎解析与微信小程序中的应用

    点击观看大咖分享 抗击疫情,腾讯云在行动.在开发微信小程序的过程中,我们经常需要展现一些图形和图表.目前市面上有好几款常用的图形库,在这些图形库的底层都有渲染引擎在支撑. ZRender 是其中一款非 ...

  4. typescript中函数_如何在TypeScript中合成Canvas动画

    typescript中函数 by Changhui Xu 徐昌辉 如何在TypeScript中合成Canvas动画 (How to Compose Canvas Animations in TypeS ...

  5. 妙味canvas动画揭、秘交互式动画设计视频 教程

    ├─第1章-运动和三角函数 │    01-动画的基本概念.avi │    02-常用的三角函数.avi │    03-Math.atan与Math.atan2.avi │    04-箭头跟随鼠 ...

  6. canvas使用滑杆交互_如何使用JavaScript和Canvas开发交互式文件上传器

    canvas使用滑杆交互 介绍 (Introduction) How nice or fun can we make the interactions on a website or web appl ...

  7. 干货 | React 中的 Canvas 动画

    作者简介 掺水的酱油,携程软件技术专家,关注大前端及移动端相关技术. 移动端硬件性能越来越好的今天,页面的交互也越来越丰富,Web 体验在不断向原生应用靠拢,加入了越来越多的手势与动画.除了常见的 C ...

  8. 10、《每周一点canvas动画》——移动物体(2)

    本系列文章代码文件 在上一节<每周一点canvas动画>--移动物体(1)中我们介绍了如何捕获一个物体,并且对物体进行拖拽.首先,我们在小球的原型对象上添加了一个方法getBounds() ...

  9. 构造函数 + 原型链继承 + 临摹面向对象模式的canvas动画框架

    感谢谢帅shawn分享的canvas动画框架,我再来分一次 //动画框架 http://neekey.net/blog/2011/05/11/canvas-%E7%AE%80%E5%8D%95%E5% ...

最新文章

  1. 比特币交易追踪溯源技术介绍
  2. 关于将一个数输出为原码、反码和补码的解惑
  3. 在线场景感知:图像稀疏表示-ScSPM和LLC总结(lasso族、岭回归)
  4. Java Double类doubleToLongBits()方法与示例
  5. 笨办法学 Python · 续 练习 1:流程
  6. Vendor Prefix:为什么需要浏览器引擎前缀
  7. MFC三大dll使用总结
  8. jquery基础选择器和层次选择器
  9. 高大上必备!D3.js对产品的贡献度剖析
  10. Linux 内核的网络协议栈
  11. JDK1.8HashMap底层实现原理
  12. 系统规划与管理师——IT服务规划设计
  13. 产品经理从专能到全能——不再虚无缥缈的用户体验
  14. server多笔记录拼接字符串 sql_第四章、SQL Server数据库查询大全(单表查询、多表连接查询、嵌套查询、关联子查询、拼sql字符串的查询、交叉查询)...
  15. 营造平安 信息化使公交事业更加人性化
  16. 移动式护栏巡逻机器人_重磅!移动式护栏巡逻执法机器人上岗!专盯高速乱停乱行!...
  17. 小程序和html5开发的差异
  18. salmon:sailfish的升级版本
  19. 动态规划解二维多重背包问题
  20. 应广单片机 PMS150G、FPC161 基础例子【GPIO设置】

热门文章

  1. kubesphere发布应用到应用商店完整步骤
  2. Linux隐藏文件标识
  3. docker 容器重命名
  4. Java设计模式--使用内部类实现线程安全且懒加载的单例模式
  5. C++11:move移动语义
  6. MQTT连接阿里云IoT(四)
  7. 【设置字符集】Win7 64位系统安装MySQL5.5.21图解教程
  8. centos 日志切割_centos自带的日志切割工具 --- logrotate
  9. python递归详解_Python理解递归的方法总结
  10. 2.2.2 操作系统之进程调度的时机(主动放弃与被动放弃)、切换与过程(广义与狭义)、方式(非剥夺与剥夺)