gpt4 book ai didi

javascript - Canvas 图像的定位问题

转载 作者:行者123 更新时间:2023-12-02 23:07:03 25 4
gpt4 key购买 nike

我正在开发一个 Canvas 项目,尝试使用一个背景图像和一个较小的图像来完成它。然后我会继续在上面做一些交互。
但我当前的问题是,当我在设置中绘制背景图像时,第二个图像的位置就在我放置的位置.
但是,当我尝试将第二个 image 作为 object 并在其 function 上构造它时,image 它是在原点 (0, 0) 上绘制,忽略参数。
这是两个代码。

在相同的设置下:

var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var maquina;

window.addEventListener("load", ()=>{

//Resizing
canvas.style.width = '100%';
canvas.style.height = '100vh';
canvas.width = canvas.offsetWidth;
canvas.height = canvas.offsetHeight;

if(canvas.getContext){
setup(canvas, ctx);
}

});

function setup(canvas, ctx){
//Pinto de verde el fondo del canvas
var background = new Image();
background.src = 'https://i.ibb.co/P9QTw6r/background.png';
background.onload = function(e){
ctx.drawImage(background, 0, 0);
}

//Obtengo las coordenadas del centro del canvas
centerX = canvas.width/2;
centerY = canvas.height/2;

var img = new Image();
img.src = 'https://i.ibb.co/tYwLtpB/maquina.png';
img.onload = function(e){
ctx.drawImage(img, centerX-10, canvas.height-100);
}
}
html, body {
width: 100%;
height: 100vh;
margin: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Monitor</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<canvas id="canvas">
</canvas>
</body>
</html>

关于其功能:

var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var maquina;

window.addEventListener("load", ()=>{

//Resizing
canvas.style.width = '100%';
canvas.style.height = '100vh';
canvas.width = canvas.offsetWidth;
canvas.height = canvas.offsetHeight;

if(canvas.getContext){
setup(canvas, ctx);
}

});

function setup(canvas, ctx){
//Pinto de verde el fondo del canvas
var background = new Image();
background.src = 'https://i.ibb.co/P9QTw6r/background.png';
background.onload = function(e){
ctx.drawImage(background, 0, 0);
}

//Obtengo las coordenadas del centro del canvas
centerX = canvas.width/2;
centerY = canvas.height/2;

maquina = new maquina(centerX-10, canvas.height-100);

}

function maquina(x, y){
this.x = x;
this.y = y;

var img = new Image();
img.src = 'https://i.ibb.co/tYwLtpB/maquina.png';
img.onload = function(e){
ctx.drawImage(img, this.x, this.y);
}

}
html, body {
width: 100%;
height: 100vh;
margin: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Monitor</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<canvas id="canvas">
</canvas>
</body>
</html>

我做错了什么?

最佳答案

我相信问题出在您的 maquina() 函数内。只需直接使用传递的参数 x 和 y 即可。我认为您不需要设置 this.x = x 和 this.y = y。

function maquina(x, y){
this.x = x;
this.y = y;

var img = new Image();
img.src = 'https://i.ibb.co/tYwLtpB/maquina.png';
img.onload = function(e){
ctx.drawImage(img, this.x, this.y);
}
}

尝试

function maquina(x, y){
//this.x = x;
//this.y = y;

var img = new Image();
img.src = 'https://i.ibb.co/tYwLtpB/maquina.png';
img.onload = function(e){
ctx.drawImage(img, x, y);
}

}

关于javascript - Canvas 图像的定位问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57540069/

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