- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是javascript画布的新手,正在尝试弄清楚一些事情。我能够很好地创建人物,但是它的实际动作对我来说是棘手的。我能够在此找到有关如何挥手的帮助,但现在我想知道如何做更多。例如,是否可以使人物摇动,向右走然后上下跳动?
var canvas = document.getElementById("canvas");
context = canvas.getContext("2d"); // get Canvas Context object
let timestamp = Date.now();
let wave = false;
draw();
function draw() {
if(Date.now() < (timestamp+500)) return requestAnimationFrame(draw);
context.clearRect(0, 0, window.innerWidth, window.innerHeight);
context.beginPath();
context.fillStyle = "black"; // #000000
context.arc(200, 50, 30, 0, Math.PI * 2, true);
context.fill(); //fill the circle
context.beginPath();
context.lineWidth = 6;
context.stroke();
//body
context.beginPath();
context.moveTo(200, 80);
context.lineTo(200, 180);
context.strokeStyle = "black";
context.stroke();
//arms
context.beginPath();
context.strokeStyle = "black";
context.moveTo(200, 100);
context.lineTo(150, 130);
if(wave) {
context.moveTo(200, 100);
context.lineTo(250, 130);
wave = false;
}
else {
context.moveTo(200, 100);
context.lineTo(250, 70);
wave = true;
}
context.stroke();
//legs
context.beginPath();
context.strokeStyle = "black";
context.moveTo(200, 180);
context.lineTo(150, 280);
context.moveTo(200, 180);
context.lineTo(250, 280);
context.stroke();
timestamp = Date.now();
requestAnimationFrame(draw);
}
<canvas id="canvas" width="400px" height="400px" >
Your browser does not support HTML5 Canvas element
</canvas>
最佳答案
装备和关键帧动画
最简单的动画是通过称为关键帧的方法。
动画的每个步骤都称为帧。对于电脑游戏,通常每秒60 -30帧(fps)
对于每一帧,您都需要在新位置绘制角色。以60fps播放5秒钟即300帧。
您可以创建绘制每个零件的x,y位置的300帧中的每一个的函数,但这是很多工作。
关键帧是通过仅在动画中创建一些特定关键点并让计算机在其间锻炼所有其他帧的方式来减轻工作量的一种方式。
动画循环
首先让我们设置动画循环。该函数每帧调用一次,我们用它来获取动画时间,清除画布并绘制动画。
requestAnimationFrame(mainLoop); // request the first frame
var startTime; // we need to have start time to match all the animations to.
var gTime; // We use this as a global time. All animation functions will reference this time
function mainLoop(time){ // time is passed automatically by requestAnimationFrame
if(startTime === undefined){ // this is the first frame so set as the start time
startTime = time;
}
gTime = time - startTime;
// clear the canvas
ctx.clearRect(0,0,canvas.width,canvas.height);
/* the render code will go here */
requestAnimationFrame(mainLoop); // request the next frame
}
function drawHead(x,y){
context.beginPath();
context.arc(x, y, 30, 0, Math.PI * 2, true);
context.fill();
}
drawHead(200,50);
x1,y1
和
x2,y2
,我们想在它们之间移动一段时间,例如
time = 2
秒。
gTime = 1
秒(半途)的位置,我们得到了两个点之间的差。
var dx = x2 - x1;
var dy = y2 - y1;
var fTime = gTime / time; // 1 / 2 = 0.5
dx *= fTime;
dy *= fTime;
var x = x1 + dx;
var y = y1 + dy;
var fTime = gTime / time;
var x = (x2 - x1) * fTime + x1;
var y = (y2 - y1) * fTime + y1;
// where v1, is start value, t1 is start time,
// v2, is end value, t2 is end time,
// and gTime is the global time
function tweenValue(v1, t1, v2, t2, gTime){
// to get the fraction of time
// first how far from the start time (gTime - t1)
// divided by the time between t1,t1
// gives (gTime - t1) / (t2 - t1); get the fraction of time between t1, t2
// which we plug directly into the formular
return (v2 - v1) * ((gTime - t1) / (t2 - t1)) + x1;
}
headKeys = [
{x : 200, y : 50, time : 0}, // x,y,time (time is in seconds
{x : 300, y : 50, time : 1},
{x : 200, y : 50, time : 2}, // last frame will loop back
]
function findKeys(keys, gTime){
// first get total time of keys
var start = keys[0].time;
var end = keys[keys.length - 1].time;
var totalTime = end - start;
var time = gTime - start; // get time relative to the start time
// loop the time in to the time between the start and end time (we need to make sure negative time works as well
time = ((time % totalTime) + totalTime) % totalTime;
// now time is at some value between and including start and less than end
// Now find the keys
var index = 0; // index of the keys
while(index < keys.length){ // could use while(true) but JS will not optimize un-terminated loops so we avoid that
if(keys[index].time <= time && keys[index+1].time > time){ // are these the two keys??
return index; // return the index of the first key
}
index ++;
}
return -1; // This will never happen unless you have some bad values in the keys array
}
const ctx = canvas.getContext("2d");
requestAnimationFrame(mainLoop);
var startTime;
var gTime;
//===============================================================================
// Animation code
function findKeys(keys, gTime){
var start = keys[0].time;
var end = keys[keys.length - 1].time;
var totalTime = end - start;
var time = gTime - start;
time = ((time % totalTime) + totalTime) % totalTime;
var index = 0;
while(index < keys.length){
if(keys[index].time <= time && keys[index+1].time > time){
return index;
}
index ++;
}
return -1;
}
function tweenValue(v1, t1, v2, t2, gTime){
return (v2 - v1) * ((gTime - t1) / (t2 - t1)) + x1;
}
function tweenCoords(key1, key2, gTime, result = {}){
var totalTime = key2.time - key1.time;
result.time = ((((gTime - key1.time) / totalTime) % 1) + 1) % 1;
result.x = (key2.x - key1.x) * result.time + key1.x;
result.y = (key2.y - key1.y) * result.time + key1.y;
return result;
}
//===============================================================================
// Character functions and animation data
const headKeys = [ // time in seconds, position in pixels
{x : 200, y : 50, time : 0},
{x : 300, y : 50, time : 1},
{x : 200, y : 50, time : 2},
];
const keyResult = {x : 0, y : 0, time : 0}; // this holds tween results and saves us creating objects each loop
function drawHead(x,y){
ctx.beginPath();
ctx.arc(x, y, 30, 0, Math.PI * 2, true);
ctx.fill();
}
function drawCharacter(gTime){
// draw the head
var keyIndex = findKeys(headKeys, gTime);
var headPos = tweenCoords(headKeys[keyIndex], headKeys[keyIndex +1], gTime, keyResult);
drawHead(headPos.x, headPos.y);
}
function mainLoop(time){
if(startTime === undefined){
startTime = time;
}
gTime = (time - startTime) / 1000; // convert time to seconds
ctx.clearRect(0,0,canvas.width,canvas.height);
drawCharacter(gTime)
requestAnimationFrame(mainLoop); // request the next frame
}
<canvas id="canvas" width="500" height="300"></canvas>
function drawLine(x,y,angle,length){
ctx.beginPath();
ctx.lineTo(x,y);
ctx.lineTo(x + Math.cos(angle) * length, y + Math.sin(angle) * length);
ctx.stroke();
}
const man = {
parts: {
body: {
len: 60,
ang: -Math.PI / 2,
parts: {
arm1: {
len: 60,
ang: Math.PI * (9 / 8), // 1/8th is 22.5 deg
},
arm2: {
len: 60,
ang: Math.PI * (7 / 8), // 1/8th is 22.5 deg
},
neck: {
len: 20,
ang: 0,
parts: {
head: {
size: 10,
}
}
}
}
},
leg1: {
len: 60,
ang: Math.PI * (5 / 8), // 1/8th is 22.5 deg
},
leg2: {
len: 60,
ang: Math.PI * (3 / 8), // 1/8th is 22.5 deg
}
}
}
man.parts.body.parts.neck.parts.head
角度是相对于上一个节点的。
const man = {
parts: {
body: {
len: 60,
ang: -Math.PI / 2,
parts: {
arm1: {
len: 60,
ang: Math.PI * (9 / 8), // 1/8th is 22.5 deg
},
arm2: {
len: 60,
ang: Math.PI * (7 / 8), // 1/8th is 22.5 deg
},
neck: {
len: 20,
ang: 0,
parts: {
head: {
size: 10,
}
}
}
}
},
leg1: {
len: 60,
ang: Math.PI * (5 / 8), // 1/8th is 22.5 deg
},
leg2: {
len: 60,
ang: Math.PI * (3 / 8), // 1/8th is 22.5 deg
}
}
}
const ctx = canvas.getContext("2d");
const workPos = {
x: 0,
y: 0
}; // to hold working posints and save having to create them every frame
// this function get the end pos of a line at angle and len starting at x,y
function angLine(x, y, ang, len, pos = {}) {
pos.x = x + Math.cos(ang) * len;
pos.y = y + Math.sin(ang) * len;
return pos;
}
// draws a line
function drawLine(x, y, x1, y1) {
ctx.beginPath();
ctx.lineTo(x, y);
ctx.lineTo(x1, y1);
ctx.stroke();
}
// draws a circle
function drawCircle(x, y, size) {
ctx.beginPath();
ctx.arc(x, y, size, 0, Math.PI * 2);
ctx.fill();
}
// Recursively draws a rig.
function drawRig(x, y, ang, rig) {
var x1, y1, ang1;
if (rig.ang !== undefined) { // is this an angled line?
var end = angLine(x, y, ang + rig.ang, rig.len, workPos);
drawLine(x, y, end.x, end.y);
x1 = end.x;
y1 = end.y;
ang1 = ang + rig.ang;
} else if (rig.size) { // is this the head
drawCircle(x, y, rig.size);
x1 = x;
y1 = y;
ang1 = ang;
} else {
// if rig has a position move to that position to draw parts
x1 = ang.x !== undefined ? ang.x + x : x;
y1 = ang.y !== undefined ? ang.y + y : y;
ang1 = ang;
}
// are there any parts attached
if (rig.parts) {
// For each part attached to this node
for (const part of Object.values(rig.parts)) {
drawRig(x1, y1, ang1, part);
}
}
}
drawRig(250, 100, 0, man);
<canvas id="canvas" width="500" height="300"></canvas>
drawRig
函数将使用关键帧(如果找到),否则仅使用正常位置
ang
和
len
const ctx = canvas.getContext("2d");
const workPos = {x: 0, y: 0}; // to hold working posints and save having to create them every frame
requestAnimationFrame(mainLoop);
var startTime;
var gTime;
//===============================================================================
// Animation code
function findKeys(keys, gTime){
var start = keys[0].time;
var end = keys[keys.length - 1].time;
var totalTime = end - start;
var time = gTime - start;
time = ((time % totalTime) + totalTime) % totalTime;
var index = 0;
while(index < keys.length){
if(keys[index].time <= time && keys[index+1].time > time){
return index;
}
index ++;
}
return -1;
}
function tweenKeys(key1, key2, gTime, result = {}){
var totalTime = key2.time - key1.time;
result.time = ((((gTime - key1.time) / totalTime) % 1) + 1) % 1;
if (key1.x !== undefined) { result.x = (key2.x - key1.x) * result.time + key1.x }
if (key1.y !== undefined) { result.y = (key2.y - key1.y) * result.time + key1.y }
if (key1.ang !== undefined) { result.ang = (key2.ang - key1.ang) * result.time + key1.ang }
if (key1.len !== undefined) { result.len = (key2.len - key1.len) * result.time + key1.len }
if (key1.size !== undefined) { result.size = (key2.size - key1.size) * result.time + key1.size }
return result;
}
const keyResult = {x : 0, y : 0, ang : 0, len : 0, size : 0,time : 0}; // this holds tween results and saves us creating objects each loop
// this function get the end pos of a line at angle and len starting at x,y
function angLine(x, y, ang, len, pos = {}) {
pos.x = x + Math.cos(ang) * len;
pos.y = y + Math.sin(ang) * len;
return pos;
}
// draws a line
function drawLine(x, y, x1, y1) {
ctx.beginPath();
ctx.lineTo(x, y);
ctx.lineTo(x1, y1);
ctx.stroke();
}
// draws a circle
function drawCircle(x, y, size) {
ctx.beginPath();
ctx.arc(x, y, size, 0, Math.PI * 2);
ctx.fill();
}
// Recursively draws a rig.
function drawRig(x, y, ang, time, rig) {
var x1, y1, ang1, end, index;
if (rig.ang !== undefined) { // is this an angled line?
if(rig.keys){ // are there key frames???
index = findKeys(rig.keys, time);
tweenKeys(rig.keys[index], rig.keys[index+1], time, keyResult);
end = angLine(x, y, ang + keyResult.ang, keyResult.len, workPos);
rig.ang = keyResult.ang;
}else{
end = angLine(x, y, ang + rig.ang, rig.len, workPos);
}
drawLine(x, y, end.x, end.y);
x1 = end.x;
y1 = end.y;
ang1 = ang + rig.ang;
} else if (rig.size) { // is this the head
if(rig.keys){ // are there key frames???
index = findKeys(rig.keys, time);
tweenKeys(rig.keys[index], rig.keys[index+1], time, keyResult);
drawCircle(x, y, keyResult.size);
}else{
drawCircle(x, y, rig.size);
}
x1 = x;
y1 = y;
ang1 = ang;
} else {
// if rig has a position move to that position to draw parts
x1 = ang.x !== undefined ? ang.x + x : x;
y1 = ang.y !== undefined ? ang.y + y : y;
ang1 = ang;
}
// are there any parts attached
if (rig.parts) {
// For each part attached to this node
for (const part of Object.values(rig.parts)) {
drawRig(x1, y1, ang1, time,part);
}
}
}
// The stick man rig with keyframes
const man = {
parts: {
body: {
len: 60,
ang: -Math.PI / 2,
keys : [
{len : 60, ang : -Math.PI * (5 / 8), time : 0},
{len : 60, ang : -Math.PI * (3 / 8), time : 1.5},
{len : 60, ang : -Math.PI * (5 / 8), time : 3},
],
parts: {
arm1: {
len: 60,
ang: Math.PI * (9 / 8), // 1/8th is 22.5 deg
keys : [
{len : 60, ang : Math.PI * (10 / 8), time : 0},
{len : 60, ang : Math.PI * (8 / 8), time : 2},
{len : 60, ang : Math.PI * (10 / 8), time : 4},
],
},
foreArm2: {
len: 30,
ang: Math.PI * (7 / 8), // 1/8th is 22.5 deg
keys : [
{len : 30, ang : Math.PI * (7 / 8), time : 0},
{len : 30, ang : Math.PI * (4 / 8), time : 1},
{len : 30, ang : Math.PI * (7 / 8), time : 2},
],
parts : {
arm : {
len: 30,
ang: Math.PI * (7 / 8), // 1/8th is 22.5 deg
keys : [
{len : 30, ang : Math.PI * (1 / 8), time : 0},
{len : 30, ang : -Math.PI * (2 / 8), time : 0.5},
{len : 30, ang : Math.PI * (1 / 8), time : 1},
],
}
}
},
neck: {
len: 20,
ang: 0,
parts: {
head: {
size: 10,
}
}
}
}
},
leg1: {
len: 60,
ang: Math.PI * (5 / 8), // 1/8th is 22.5 deg
},
leg2: {
len: 60,
ang: Math.PI * (3 / 8), // 1/8th is 22.5 deg
keys : [
{len : 60, ang : Math.PI * (3 / 8), time : 0},
{len : 60, ang : Math.PI * (3 / 8), time : 4},
{len : 60, ang : Math.PI * (1 / 8), time : 4.5},
{len : 60, ang : Math.PI * (3 / 8), time : 5},
{len : 60, ang : Math.PI * (3 / 8), time : 8},
],
}
}
}
function mainLoop(time){
if(startTime === undefined){
startTime = time;
}
gTime = (time - startTime) / 1000; // convert time to seconds
ctx.clearRect(0,0,canvas.width,canvas.height);
drawRig(250, 100, 0, gTime, man);
requestAnimationFrame(mainLoop); // request the next frame
}
<canvas id="canvas" width="500" height="300"></canvas>
easeInOut
曲线,从慢速开始加速,然后慢速向下减速。如下所示,将其添加到补间功能(从上面的片段中获取)。
const eCurve = (v, p = 2) => v < 0 ? 0 : v > 1 ? 1 : Math.pow(v, p) / (Math.pow(v, p) + Math.pow(1 - v, p));}
function tweenKeys(key1, key2, gTime, result = {}){
var totalTime = key2.time - key1.time;
result.time = ((((gTime - key1.time) / totalTime) % 1) + 1) % 1;
result.time = eCurve(result.time); // add the ease in out
... rest of function as normal
关于javascript - Javascript动画 Canvas (人物运动),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49135858/
这是否可以检测到手机何时像图片上那样移动?这个在android中叫什么名字? 是否有处理此类事情的事件?一年前,我看到一个带指南针的应用程序,它可以实时运行。 谢谢! 最佳答案 我希望这段代码能有所帮
我正在为这个而撕扯我的头发。出于某种奇怪的原因,我找不到/想不出如何在 SFML 和/或 SDL 中移动 Sprite 。我看过的这两个库的教程对此一无所知;所以我认为它更像是 C++ 的东西而不是库
所以我最近一直在研究 DirectX11,但我对它还是很陌生。我现在正在尝试通过翻译来移动一些东西,这就是我所拥有的。我一直在阅读 Frank D Luna 关于 DirectX11 的书,他提供了一
我一直在尝试为绘图元素制作动画,但没有成功。我可以对导入的图像进行动画处理,但是当我尝试对 pygame 生成的绘图进行动画处理时,它们仍然是静态的。 编辑:“动画”是指“移动”。就像使圆在 x 和
好吧,我已经尝试 Java 几个星期了,遵循类和在线教程。我做了一个简单的游戏,其中方 block 落向屏幕底部,而玩家控制一个小球,仅在 x 轴上移动并尝试避开它们。 我遇到的问题是方 block
我的 python 代码遇到一些问题,我正在制作蛇的一个版本,我的问题涉及蛇本身的运动。我已经得到了工作正常的方向,我只需要做到这一点,以便蛇继续沿着通过按键告诉它的方向移动,我还需要使它成为一个 b
那是我的代码。 -(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event { [[NSNotificationC
我正在构建这个用于慢跑的 Android 应用程序,它应该测量用户从某个时间点开始的距离。 我对使用 GPS 选项(经度、纬度)不感兴趣,所以如果没有 GPS 选项并且只使用 android 传感器,
什么是自律?网上有这样一个回答:“最健康的自律是早睡,最实用的自律是运动,最丰盈内心的自律是读书。”早睡养神,运动养身,读书养脑。坚持把这三件事做好,你就已经超越了很多人。 1 每天早点睡 你有没有过
考虑下面这行 Lisp 代码: (some-function 7 8 | 9) ;; some comment. note the extra indentation 该点位于“8”和
在 Vim 中,如何移动到 xml 文件中的父/表亲标签?我正在寻找类似的东西: vatat " create a selection for second parent tag with all c
用 Dart 做这样的 Canvas 运动的最佳方法是什么? http://jsfiddle.net/loktar/dMYvG/ 我正在尝试使 Canvas 运动平稳,并想看看Dart可以做什么。 还
我试图让一个物体在固定的时间段内沿着圆形路径移动。 这个应用程序是一个“平滑运动”时钟。 因此,我不想每次 .getSeconds() 更新时将位置移动到固定坐标,而是想使用 ( .getSecond
我正在尝试创建一个简单的动画,其中一系列气泡围绕中心点旋转。我有一个动画版本,其中气泡在开始旋转之前从中心点扩散,效果很好,但是一旦我单击其中一个图像(引发动画),屏幕就会卡住一会儿,然后气泡出现在他
不久前我开始学习java作为一种爱好,因为我想制作一个小游戏。我学习了 Java 基础知识,并决定尝试解决游戏开发问题。我的 JFrame 和一切都很好,从技术上讲我没有错误,但我的小矩形家伙不会在屏
我在制作台球游戏时遇到问题,当我模拟击球时,我需要球使用react,程序是这样工作的,您单击击球的方向和力量,然后单击开始, go按钮位于创建标签的GUI类中,该按钮调用我的主类中的一个方法来接收参数
我以前在 2d 项目中使用过类似的东西来移动并且它总是有效。我现在正在使用它,它为我提供了一些输出的正确角度和错误的角度。我认为我的触发器中有一些错误,我弄错了。不过,我已经检查了大约一百万次。 Pr
我的 OpenGL 应用程序有问题,您可以在 this gif 中清楚地看到.基本上我想朝光标指向的方向移动,但这并没有发生,而是“前进”方向保持不变。 例如,如果我转身 180° 并按“w”向前走,
因此,我再次开始使用 C++ 编程并尝试使用 OpenGL。目前我有一个基本的应用程序,我只想用键移动相机。我读了很多关于这个的文章,但我仍然对运动有问题,我猜是因为即使只有一点点,但它们与我的程序不
我在 Android OpenGL 中有一个 Sprite 。这个 Sprite (一只小甲虫)总是向前移动,我使用: sprite.setPosition(posX, posY); 现在我有一个旋转
我是一名优秀的程序员,十分优秀!