Canvas学习笔记——拖曳与投掷物体
首先用一个例子来演示这个效果:
鼠标可以拖曳和投掷小球
// > 16 & 0xff,
g = color >> 8 & 0xff,
b = color >> 0xff,
a = (alpha 1) ? 1 : alpha);
if(a === 1) {
return 'rgb('+r+','+g+','+b+')';
} else {
return 'rgba('+r+','+g+','+b+','+a+')';
}
};
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;
}
};
window.utils.containsPoint = function (rect, x, y) {
return !(x rect.x + rect.width ||
y rect.y + rect.height);
};
window.utils.intersects = function (rectA, rectB) {
return !(rectA.x + rectA.width 0) {
ctx.stroke();
}
ctx.restore();
};
Ball.prototype.getBounds = function () {
return {
x: this.x - this.radius,
y: this.y - this.radius,
width: this.radius * 2,
height: this.radius * 2
};
};
function Ship () {
this.x = 0;
this.y = 0;
this.width = 25;
this.height = 20;
this.rotation = 0;
this.showFlame = false;
}
Ship.prototype.draw = function (ctx) {
ctx.save();
ctx.translate(this.x, this.y);
ctx.rotate(this.rotation);
ctx.lineWidth = 1;
ctx.strokeStyle = "#ffffff";
ctx.beginPath();
ctx.moveTo(10, 0);
ctx.lineTo(-10, 10);
ctx.lineTo(-5, 0);
ctx.lineTo(-10, -10);
//ctx.lineTo(10, 0);
ctx.closePath();
ctx.stroke();
if (this.showFlame) {
ctx.beginPath();
ctx.moveTo(-7.5, -5);
ctx.lineTo(-15, 0);
ctx.lineTo(-7.5, 5);
ctx.stroke();
}
ctx.restore();
};
(function() {
var canvas = document.createElement('canvas'),
a = document.getElementById('a');
canvas.id = 'c1';
canvas.width = 500;
canvas.height = 500;
a.appendChild(canvas);
var canvas = document.getElementById('c1'),
ctx = canvas.getContext('2d');
var ball = new Ball(30,Math.random() * 0xffffff),
mouse = utils.captureMouse(canvas),
vx = Math.random() * 5 - 2,
vy = -10,
g = 0.2,
isMouseDown = false,
oldX,oldY,
bounce = -0.8,
left = 0,
top = 0,
dx,dy,
right = canvas.width,
bottom = canvas.height;
ball.x = canvas.width / 2;
ball.y = canvas.height / 2;
canvas.addEventListener('mousedown',function() {
if(utils.containsPoint(ball.getBounds(),mouse.x,mouse.y)) {
isMouseDown = true;
vx = 0;
vy = 0;
dx = mouse.x - ball.x;
dy = mouse.y - ball.y;
oldX = ball.x;
oldY = ball.y;
canvas.addEventListener('mousemove',onMouseMove,false);
canvas.addEventListener('mouseup',onMouseUp,false);
}
},false);
function onMouseMove() {
ball.x = mouse.x - dx;
ball.y = mouse.y - dy;
}
function trackVelocity() {
vx = ball.x - oldX;
vy = ball.y - oldY;
oldX = ball.x;
oldY = ball.y;
}
function checkBound() {
vy += g;
ball.x += vx;
ball.y += vy;
if(ball.x + ball.radius > right) {
ball.x = right - ball.radius;
vx *= bounce;
} else if(ball.x - ball.radius bottom) {
ball.y = bottom - ball.radius;
vy *= bounce;
} else if(ball.y - ball.radius
拖曳功能比较简单,主要难点在如何计算投掷时的速度。
用一张图来说明:
物体在动画行进一帧的间隔内从a点被鼠标拖动到了b点,很显然在这个过程中物体的运动速度为:
vx = b.x - a.x;
vy = b.y - a.y;
只要在鼠标按下时记录小球的位置,然后在拖动时不断计算小球当前位置与旧位置的距离,就能得到小球的速度:
canvas.addEventListener('mousedown',function() {
oldX = ball.x;
oldY = ball.y; canvas.addEventListener('mousemove',onMouseMove,false);
canvas.addEventListener('mouseup',onMouseUp,false);
},false); function onMouseMove() {
ball.x = mouse.x;
ball.y = mouse.y;
} function onMouseUp() {
canvas.removeEventListener('mousemove',onMouseMove,false);
canvas.removeEventListener('mouseup',onMouseUp,false);
} function trackVelocity() {
vx = ball.x - oldX;
vy = ball.y - oldY;
oldX = ball.x;
oldY = ball.y;
} (function() {
window.requestAnimFrame(arguments.callee,canvas);
ctx.clearRect(0,0,canvas.width,canvas.height);
trackVelocity();
ball.draw(ctx);
})();
Canvas学习笔记——拖曳与投掷物体的更多相关文章
- canvas学习笔记、小函数整理
http://bbs.csdn.net/topics/391493648 canvas实例分享 2016-3-16 http://bbs.csdn.net/topics/390582151 html5 ...
- canvas学习笔记,实用知识点总结(上)
本博客是本人日常学习笔记,作为重要知识点的总结记录,随笔风格可能更倾向于个人的学习习惯和方式,若对您有帮助十分荣幸,若存在问题欢迎互相学习探讨,前端小白一枚在此恭候. 一.基本使用规则 1.创建画布 ...
- canvas学习笔记(下篇) -- canvas入门教程--保存状态/变形/旋转/缩放/矩阵变换/综合案例(星空/时钟/小球)
[下篇] -- 建议学习时间4小时 课程共(上中下)三篇 此笔记是我初次接触canvas的时候的学习笔记,这次特意整理为博客供大家入门学习,几乎涵盖了canvas所有的基础知识,并且有众多练习案例, ...
- canvas学习笔记(中篇) -- canvas入门教程-- 颜色/透明度/渐变色/线宽/线条样式/虚线/文本/阴影/图片/像素处理
[中篇] -- 建议学习时间4小时 课程共(上中下)三篇 此笔记是我初次接触canvas的时候的学习笔记,这次特意整理为博客供大家入门学习,几乎涵盖了canvas所有的基础知识,并且有众多练习案例, ...
- canvas学习笔记(上篇)-- canvas入门教程 -- canvas标签/方块/描边/路径/圆形/曲线
[上篇] -- 建议学习时间4小时 课程共(上中下)三篇 此笔记是我初次接触canvas的时候的学习笔记,这次特意整理为博客供大家入门学习,几乎涵盖了canvas所有的基础知识,并且有众多练习案例, ...
- canvas学习笔记一
为了研究pixi库,就顺带从头到位学习下canvas吧 判断支持力度 var webgl = (function() { try { var canvas = document.createEleme ...
- canvas学习笔记:canvas对图片的像素级处理--ImageData的应用
学习了canvas的基本绘图功能后,惊喜的发现canvas对图片数据也有相当强大的处理功能,能够从像素级别操作位图,当然[lte ie8]不支持. 主要的函数有三个: ctx.createImageD ...
- canvas学习笔记(一)-认识canvas
canvas是html5新增的一个标签,主要用于图形的绘制.我们可以把它理解为是浏览器给我们提供了一个画板,至于要绘制怎样的画卷,就看神笔马良你的主意了.而在canvas上绘制图形使用的笔,就是js了 ...
- 【canvas学习笔记一】基本认识
<canvas>标签定义了一块画布,画布可以在网页中绘制2D和3D图象,现在先学习如何绘制2D图象,绘制3D图象属于WebGL的内容(也就是网页版的OpenGL,3D图形接口). 属性 & ...
随机推荐
- 【Luogu】P3355骑士共存问题(最小割)
题目链接 像题面那样把棋盘染成红黄点.发现骑士迈一步能到达的点的颜色一定是跟他所在的格子的颜色不同的.于是(woc哪来的于是?这个性质有这么明显吗?)从源点向所有红点连边,从所有黄点向汇点连边,红点向 ...
- iOS-文件断点续传
* 移动客户端在和服务器交互的时候,上传和下载使用十分广泛. * 在我们下载文件的时候,我们在点击暂停的时候可以暂停下载,点击下载的时候可以继续下载,这个功能如何实现? * 下载进度条如何显示? 先大 ...
- bzoj 3544 [ONTAK2010]Creative Accounting 贪心
Description 给定一个长度为N的数组a和M,求一个区间[l,r],使得(\sum_{i=l}^{r}{a_i}) mod M的值最大,求出这个值,注意这里的mod是数学上的mod Input ...
- 文本生成器(bzoj 1030)
Description JSOI交给队员ZYX一个任务,编制一个称之为“文本生成器”的电脑软件:该软件的使用者是一些低幼人群,他们现在使用的是GW文本生成器v6版.该软件可以随机生成一些文章―――总是 ...
- 【NOIP2016游记】
day-5:上午看了火箭打马刺 火箭差点翻盘但老大爷们还是稳 下午一场五校 T1T2原题做过 T3分块 day-4:上午五校1小时写3道暴力 2个半小时优化 然而还不知道拿了多少 %%%CC T2树链 ...
- postgresql 10 ltree 使用说明
官方文档 https://www.postgresql.org/docs/10/static/ltree.html ltree是俄罗斯Teodor Sigaev和Oleg Bartunov共同开发的P ...
- LeetCode OJ——Binary Tree Inorder Traversal
http://oj.leetcode.com/problems/binary-tree-inorder-traversal/ 树的中序遍历,递归方法,和非递归方法. /** * Definition ...
- tableView刷新中的问题
在开始之前先上一张效果图 相信大家都看到了“店铺优惠”这一栏,在这里假设这一栏就是单独的一个cell,当无店铺优惠的时候不可点击在有店铺优惠的时候会弹出优惠列表,选中并返回时会刷新数据,所以弹出视图采 ...
- Codeforces 551E GukiZ and GukiZiana(分块思想)
题目链接 GukiZ and GukiZiana 题目大意:一个数列,支持两个操作.一种是对区间$[l, r]$中的数全部加上$k$,另一种是查询数列中值为$x$的下标的最大值减最小值. $n < ...
- Widows下利用OpenSSL生成证书
1.下载OpenSSL的windows版本 32位:openssl-1.0.2a-i386-win32.zip 64位:openssl-1.0.2a-x64_86-win64.zip 下载之后解压即可 ...