gpt4 book ai didi

JavaScript 动画矩形

转载 作者:行者123 更新时间:2023-11-28 00:49:26 25 4
gpt4 key购买 nike

我尝试开发一款手机游戏。以下问题:您在代码中看到的矩形应该是动画的。这意味着它应该来自上方并运行到底部,依此类推。如果我运行此代码,所有内容都会被绘制,但矩形不会移动。不知道错在哪里。

window.onload = window.onresize = function() {
var C = 1; // canvas width to viewport width ratio
var W_TO_H = 2 / 1; // canvas width to canvas height ratio
var el = document.getElementById("myCanvas");


var viewportWidth = window.innerWidth;
var viewportHeight = window.innerHeight;

var canvasWidth = viewportWidth * C;
var canvasHeight = canvasWidth / W_TO_H;
el.style.position = "fixed";
el.setAttribute("width", canvasWidth);
el.setAttribute("height", canvasHeight);
el.style.top = (viewportHeight - canvasHeight) / 2;
el.style.left = (viewportWidth - canvasWidth) / 2;
var x = canvasWidth / 100;
var y = canvasHeight / 100;

window.ctx = el.getContext("2d");
ctx.clearRect(0, 0, canvasWidth, canvasHeight);
// draw triangles


function init() {
return setInterval(main_loop, 10);
}

function draw() {
recty = canvasHeight / 100 * 20;
rectheight = canvasHeight / 100 * 30;
ctx.clearRect(0, 0, canvasWidth, canvasHeight);
// draw rect
ctx.beginPath();
fillStyle = "#000000";
ctx.rect(x * 30, recty, x * 20, rectheight);
ctx.fill();
// draw triangles
ctx.beginPath();
ctx.moveTo(x * 90, y * 50);
ctx.lineTo(x * 99, y * 75);
ctx.lineTo(x * 99, y * 25);
ctx.closePath();
ctx.stroke();
ctx.beginPath();
ctx.moveTo(x * 10, y * 50);
ctx.lineTo(x * 1, y * 25);
ctx.lineTo(x * 1, y * 75);
ctx.closePath();
ctx.stroke();
// ballx
ballx = canvasWidth / 100;
// draw ball
ctx.beginPath();
ctx.fillStyle = "#FF4422";
ctx.arc(ballx * 50, y * 50, x * 5, 0, 2 * Math.PI);
ctx.fill();
}

function update() {
recty += 1;
if (recty > canvasHeight) {
recty = -rectheight;
}
if (recty > canvasHeight) {
recty -= 1;
}
}

function main_loop() {
draw();
update();
}

init();
}
<html>

<head>
<meta name="viewport" content="width=device-width, height=device-height, initial-scale=1, maximum-scale=1">
<style>
</style>
</head>

<body>
<div="gameArea">
<canvas id="myCanvas"></canvas>
</div>
</body>

</html>

最佳答案

您的update函数更改了recty,但随后它会在draw的第一行中被覆盖。

recty = canvasHeight / 100 * 20;

我刚刚将此行从 draw 移至 init

window.onload = window.onresize = function() {
var C = 1; // canvas width to viewport width ratio
var W_TO_H = 2 / 1; // canvas width to canvas height ratio
var el = document.getElementById("myCanvas");


var viewportWidth = window.innerWidth;
var viewportHeight = window.innerHeight;

var canvasWidth = viewportWidth * C;
var canvasHeight = canvasWidth / W_TO_H;
el.style.position = "fixed";
el.setAttribute("width", canvasWidth);
el.setAttribute("height", canvasHeight);
el.style.top = (viewportHeight - canvasHeight) / 2;
el.style.left = (viewportWidth - canvasWidth) / 2;
var x = canvasWidth / 100;
var y = canvasHeight / 100;

window.ctx = el.getContext("2d");
ctx.clearRect(0, 0, canvasWidth, canvasHeight);
// draw triangles


function init() {
recty = canvasHeight / 100 * 20;
return setInterval(main_loop, 10);
}

function draw() {
rectheight = canvasHeight / 100 * 30;
ctx.clearRect(0, 0, canvasWidth, canvasHeight);
// draw rect
ctx.beginPath();
fillStyle = "#000000";
ctx.rect(x * 30, recty, x * 20, rectheight);
ctx.fill();
// draw triangles
ctx.beginPath();
ctx.moveTo(x * 90, y * 50);
ctx.lineTo(x * 99, y * 75);
ctx.lineTo(x * 99, y * 25);
ctx.closePath();
ctx.stroke();
ctx.beginPath();
ctx.moveTo(x * 10, y * 50);
ctx.lineTo(x * 1, y * 25);
ctx.lineTo(x * 1, y * 75);
ctx.closePath();
ctx.stroke();
// ballx
ballx = canvasWidth / 100;
// draw ball
ctx.beginPath();
ctx.fillStyle = "#FF4422";
ctx.arc(ballx * 50, y * 50, x * 5, 0, 2 * Math.PI);
ctx.fill();
}

function update() {
recty += 1;
if (recty > canvasHeight) {
recty = -rectheight;
}
if (recty > canvasHeight) {
recty -= 1;
}
}

function main_loop() {
draw();
update();
}

init();
}
<html>

<head>
<meta name="viewport" content="width=device-width, height=device-height, initial-scale=1, maximum-scale=1">
<style>
</style>
</head>

<body>
<div="gameArea">
<canvas id="myCanvas"></canvas>
</div>
</body>

</html>

关于JavaScript 动画矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26955798/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com