首先用一个例子来演示这个效果:

鼠标可以拖曳和投掷小球

 

// > 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学习笔记——拖曳与投掷物体的更多相关文章

  1. canvas学习笔记、小函数整理

    http://bbs.csdn.net/topics/391493648 canvas实例分享 2016-3-16 http://bbs.csdn.net/topics/390582151 html5 ...

  2. canvas学习笔记,实用知识点总结(上)

    本博客是本人日常学习笔记,作为重要知识点的总结记录,随笔风格可能更倾向于个人的学习习惯和方式,若对您有帮助十分荣幸,若存在问题欢迎互相学习探讨,前端小白一枚在此恭候. 一.基本使用规则 1.创建画布 ...

  3. canvas学习笔记(下篇) -- canvas入门教程--保存状态/变形/旋转/缩放/矩阵变换/综合案例(星空/时钟/小球)

    [下篇] -- 建议学习时间4小时  课程共(上中下)三篇 此笔记是我初次接触canvas的时候的学习笔记,这次特意整理为博客供大家入门学习,几乎涵盖了canvas所有的基础知识,并且有众多练习案例, ...

  4. canvas学习笔记(中篇) -- canvas入门教程-- 颜色/透明度/渐变色/线宽/线条样式/虚线/文本/阴影/图片/像素处理

    [中篇] -- 建议学习时间4小时  课程共(上中下)三篇 此笔记是我初次接触canvas的时候的学习笔记,这次特意整理为博客供大家入门学习,几乎涵盖了canvas所有的基础知识,并且有众多练习案例, ...

  5. canvas学习笔记(上篇)-- canvas入门教程 -- canvas标签/方块/描边/路径/圆形/曲线

    [上篇] -- 建议学习时间4小时  课程共(上中下)三篇 此笔记是我初次接触canvas的时候的学习笔记,这次特意整理为博客供大家入门学习,几乎涵盖了canvas所有的基础知识,并且有众多练习案例, ...

  6. canvas学习笔记一

    为了研究pixi库,就顺带从头到位学习下canvas吧 判断支持力度 var webgl = (function() { try { var canvas = document.createEleme ...

  7. canvas学习笔记:canvas对图片的像素级处理--ImageData的应用

    学习了canvas的基本绘图功能后,惊喜的发现canvas对图片数据也有相当强大的处理功能,能够从像素级别操作位图,当然[lte ie8]不支持. 主要的函数有三个: ctx.createImageD ...

  8. canvas学习笔记(一)-认识canvas

    canvas是html5新增的一个标签,主要用于图形的绘制.我们可以把它理解为是浏览器给我们提供了一个画板,至于要绘制怎样的画卷,就看神笔马良你的主意了.而在canvas上绘制图形使用的笔,就是js了 ...

  9. 【canvas学习笔记一】基本认识

    <canvas>标签定义了一块画布,画布可以在网页中绘制2D和3D图象,现在先学习如何绘制2D图象,绘制3D图象属于WebGL的内容(也就是网页版的OpenGL,3D图形接口). 属性 & ...

随机推荐

  1. php单一入口和多入口模式详细讲解

    php单一入口模式可谓是现在一种比较流行的大型web应用开发模式,比如当前比较流行的一些php开发框架,zend,thinkphp,qeephp,还有cakephp 等他们都是采用的单一入口模式的.本 ...

  2. bzoj4717 改装 模拟+二分

    Description [题目背景] 小Q最近喜欢上了一款游戏,名为<舰队connection>,在游戏中,小Q指挥强大的舰队南征北战,从而成为了一名dalao.在游戏中,不仅船只能力很重 ...

  3. 洛谷 P 1514 引水入城==Codevs 1066

    题目描述 在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠.该国的行政区划十分特殊,刚好构成一个N 行M 列的矩形,如上图所示,其中每个格子都代表一座城市,每座城市都有一个海拔高度. ...

  4. LeetCode OJ--Rotate List

    http://oj.leetcode.com/problems/rotate-list/ 取得后面k个节点,然后截断插到前面.如果k比list长,则按照求余算. 去后面的k个节点:使用两个指针,第一个 ...

  5. 洛谷——P2296 寻找道路

    P2296 寻找道路 题目描述 在有向图G 中,每条边的长度均为1 ,现给定起点和终点,请你在图中找一条从起点到终点的路径,该路径满足以下条件: 1 .路径上的所有点的出边所指向的点都直接或间接与终点 ...

  6. ubuntu下某些文件目录

    1.#include <stdio.h> 2.#include <stdlib.h> stdio.h和stdlib.h的路径:/usr/include

  7. django使用logging记录日志

    django使用logging记录日志,我没有用这方式去记录日志,主要还是项目小的原因吧, 有机会遇见大项目的话可以回头研究. 配置setting.py配置文件 import logging impo ...

  8. 项目心得——按照指定的日期/时间创建Date对象

    项目心得——按照指定的日期/时间创建Date对象 有时,在做项目中,需要获得指定日期的Date对象,这个指定的日期或者时间可能不是当前的时间.下面讲解两种获取指定日期/时间的Date对象的方法: pa ...

  9. 浅析 JavaScript 中的闭包(-------------------------------------------)

    一.前言 对于 JavaScript 来说,闭包是一个非常强大的特征.但对于刚开始接触的初学者来说它又似乎是特别高深的.今天我们一起来揭开闭包的神秘面纱.闭包这一块也有很多的文章介绍过了,今天我就浅谈 ...

  10. tomcat知识点

    (1)使用线程池   Servlet引擎为每一个请求创建一个隔离的线程,分配这个线程给service()方法,在它执行完后移除这个线程.默认情况下,servlet引擎 为每一个请求创建新的线程.因为创 ...