canvas动画3:交互
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;
//初始化canvas
init();
})
//绘制圆的最大半径
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();
canvas动画3:交互的更多相关文章
- canvas动画:自由落体运动
经过前面的文章,我们已经能够在canvas画布上画出各种炫酷的图形和画面,但是这些画面都是禁止的,怎么样才能让他们动起来呢? 如何绘制基本图形可以参考:canvas基本图形绘制 如何对基本图形移动旋转 ...
- 《每周一点canvas动画》——3D点线与水波动画
<每周一点canvas动画>--差分函数的妙用 每周一点canvas动画代码文件 好像上次更新还是十一前,这唰唰唰的就过去大半个月了,现在才更新实在不好意思.这次我们不涉及canvas 3 ...
- 2015.4.23 贪吃蛇、canvas动画,各种上传工具,url信息匹配以及最全前端面试题等
1.面向对象贪吃蛇 2.css中:hover 改变图片 页面加载完 第一次鼠标移入会闪一下 这是为啥? 解决方法:你把两张图合成一张图或者是先把图片加载到页面上,然后再hover出来. 解析:图片 ...
- HTML动画分类 HTML5动画 SVG库 SVG工具 Canvas动画工具
1.js配合传统css属性控制,可以使用setTimeout或者高级的requestAnimationFrame 2.css3 3.svg 4.canvas(当然,这个还是要配合js) 也许这么 ...
- 7 个顶级的 HTML5 Canvas 动画赏析
HTML5确实是一项改革浏览器乃至整个软件行业的新技术,它可以帮助我们Web开发者很方便地在网页上实现动画特效,而无需臃肿的Flash作为支撑.本文分享7个顶级的HTML5 Canvas 动画,都有非 ...
- 8个经典炫酷的HTML5 Canvas动画欣赏
HTML5非常强大,尤其是Canvas技术的应用,让HTML5几乎可以完成所有Flash能完成的效果.本文精选了8个经典炫酷的HTML5 Canvas动画欣赏,每一个都提供全部的源代码,希望对你有所帮 ...
- 7个惊艳的HTML5 Canvas动画效果及源码
HTML5非常强大,尤其是现在大部分浏览器都支持HTML5和CSS3,用HTML5制作的动画也多了起来.另外,Canvas上绘制图形非常简单,本文就分享了一些强大的HTML5 Cnavas动画,一起来 ...
- 《FLASH CC 2015 CANVAS 中文教程》——1、导出canvas动画,文件结构浅析
注::如果你对 FLASH 这个软件操作不够熟悉,建议你可以先看看FLASH动画之类的书. :FLASH CC 在文中直接简称为CC. :以下所以文章中所说的快捷键 如果你按了不起作用,请检查是否有其 ...
- Html5 Canvas动画旋转的小方块;
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
随机推荐
- iOS多线程基本使用
大家都知道,在开发过程中应该尽可能减少用户等待时间,让程序尽可能快的完成运算.可是无论是哪种语言开发的程序最终往往转换成汇编语言进而解释成机器码来执行.但是机器码是按顺序执行的,一个复杂的多步操作只能 ...
- Android之View绘制流程源码分析
版权声明:本文出自汪磊的博客,转载请务必注明出处. 对于稍有自定义View经验的安卓开发者来说,onMeasure,onLayout,onDraw这三个方法都不会陌生,起码多少都有所接触吧. 在安卓中 ...
- Nginx配置文件中文详解
######Nginx配置文件nginx.conf中文详解##### #定义Nginx运行的用户和用户组 user www www; #nginx进程数,建议设置为等于CPU总核心数. worker_ ...
- 【解决方案】M2Crypto不支持python3
问题现象:python3的环境下,导入M2Crypto模块报错 "ModuleNotFoundError: No module named 'M2Crypto",通过pip ins ...
- ArcGis for flex查询FeatureLayer数据
1. 首先实例化一个FeatureLayer对象 private var featureLayer:FeatureLayer=new FeatureLayer(); 2.指定FeatureLayer对 ...
- 程序员节应该写博客之.NET下使用HTTP请求的正确姿势
程序员节应该写博客之.NET下使用HTTP请求的正确姿势 一.前言 去年9月份的时候我看到过外国朋友关于.NET Framework下HttpClient缺陷的分析后对HttpClient有了一定的了 ...
- 【NOIP2015提高组】 Day2 T3 运输计划
题目描述 L 国有 n 个星球,还有 n-1 条双向航道,每条航道建立在两个星球之间,这 n-1 条航道连通了 L 国的所有星球. 小 P 掌管一家物流公司,该公司有很多个运输计划,每个运输计划形如: ...
- OOAD-设计模式(四)结构型模式之适配器、装饰器、代理模式
前言 前面我们学习了创建型设计模式,其中有5中,个人感觉比较重要的是工厂方法模式.单例模式.原型模式.接下来我将分享的是结构型模式! 一.适配器模式 1.1.适配器模式概述 适配器模式(Adapter ...
- ASP.NET Core 开源GitServer 实现自己的GitHub
ASP.NET Core 2.0 开源Git HTTP Server,实现类似 GitHub.GitLab. GitHub:https://github.com/linezero/GitServer ...
- java基础解析系列(十)---ArrayList和LinkedList源码及使用分析
java基础解析系列(十)---ArrayList和LinkedList源码及使用分析 目录 java基础解析系列(一)---String.StringBuffer.StringBuilder jav ...