- 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/
此代码的目标是找出所需的删除内容使字符串之间没有相同的字符。 ababa = 0 abaaab = 2 and aaaa = 3 var input ="abababaaaabaaab"; var s
我需要一些帮助。我必须从文件中读取数据并将其存储到 Oracle 数据库中。当数据中出现“à”或“À”等字符时,我会遇到麻烦。例如,“à”被读取并变成“à”进入我的应用程序,因此,当我尝试将数据保存到
我用! @ # $ 在我的 MySQL 用户的密码中 但是当我在密码中使用这些字符时,PDO 无法连接到 MySQL 服务器 $servername = "localhost"; $username
我需要点击鼠标按钮才能在两个轴上旋转图形。当您释放鼠标按钮时,图形将旋转到原始位置。轻微旋转时,一切都会按预期进行,但大幅旋转 ( 0) { if (sumRotate -D
我有 this xml file 我正在尝试访问PHP中的标记permission中的yt:accessControl属性值 echo (string)$xmlyt->entry->children(
我正在尝试从我的 GitHub 在 Netlify 上部署我的网站。 但是我坚持以下错误: 11:37:19 AM: failed during stage 'deploying site': Inv
我是一名优秀的程序员,十分优秀!