gpt4 book ai didi

javascript - jQuery/Javascript - 在(文档)之前运行函数。准备好加载图像

转载 作者:行者123 更新时间:2023-11-28 03:20:29 24 4
gpt4 key购买 nike

我正在尝试获取图像的尺寸,以便将 Canvas 的长度调整为两倍宽度(或大小)。 (因此,对于 400x800 的图像,我需要 800x800 的 Canvas )。我现在要做的是加载图像两次,一次确定它的大小,另一次显示它。我的带有 Javascript/JQuery 的 HTML 看起来像

<!DOCTYPE html>
<html>
<head>
<title>Canvas from scratch</title>
<meta charset="utf-8">

<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="jcanvas.min.js"></script>
</head>

<body>
<canvas id="myCanvas" width="1100" height="500">
</canvas>
<script>

$("<img />").attr("src", "test1image.jpg").load(function() {
/* This is where I'd like to load the image to get its dimensions */
});

$(document).ready(
function() {


var imgWidth, imgHeight;
var imageData;

var image = new Image();
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");

$(image).load(function() {
/* This is where the image is loaded and
inserted into the canvas */
ctx.drawImage(image, 0, 0);
});
image.src = "test1image.jpg";

imageData = ctx.getImageData(0, 0,
image.width,
);

/* This is just part of the image manipulation,
bland right now */
var newImgData = ctx.createImageData(imageData.width,
imageData.height);
for ( var i = 0; i < newImgData.data.length; i += 4) {
newImgData.data[i + 0] = 255;
newImgData.data[i + 1] = 0;
newImgData.data[i + 2] = 0;
newImgData.data[i + 3] = 255;
}
ctx.putImageData(newImgData, imageData.width, 0);

});
</script>
</body>
</html>

有一件事我一直在尝试但注意到它不起作用,那就是尝试将函数放在 (document).ready 之前,但如果 (document).ready 调用总是先出现。如果没有创建 Canvas ,我似乎无法加载图像(因为它在 (document).ready 调用中)。任何人都可以解释发生了什么事吗?

谢谢。

Edit_1:我刚刚尝试添加

jQuery(window).load(function() {
$("<img />").attr("src", "test1image.jpg").load(function() {
imgWidth = this.width;
});
});

到脚本的顶部,但 imgWidth 在 (document).ready 中被声明为未定义。

Edit_2:所以,这实际上非常有效。这是新代码,它 100% 有效。新的 Canvas 大小正是我想要的,基于图像尺寸并且一切正常......我唯一的想法是可能有一个太多的嵌套函数但我会尝试自己解决这个问题.

<!DOCTYPE html>
<html>
<head>
<title>Canvas from scratch</title>
<meta charset="utf-8">

<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="jcanvas.min.js"></script>
</head>

<body>
<canvas id="myCanvas" width="1100" height="500">
</canvas>
<script>
$(document).ready(
function() {
$("<img>").attr("src", "test1image.jpg").on('load', function() {
var imgWidth, imgHeight;
var imageData;
var image = new Image();
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
ctx.canvas.width = this.width * 2;
ctx.canvas.height = this.height;
$(image).on("load", function() {
/* This is where the image is loaded and
inserted into the canvas */
ctx.drawImage(image, 0, 0);

});
image.src = "test1image.jpg";

imageData = ctx.getImageData(0, 0,
this.width,
this.height);

/* This is just part of the image manipulation,
bland right now */
var newImgData = ctx.createImageData(imageData.width,
imageData.height);
for ( var i = 0; i < newImgData.data.length; i += 4) {
newImgData.data[i + 0] = 255;
newImgData.data[i + 1] = 0;
newImgData.data[i + 2] = 0;
newImgData.data[i + 3] = 255;
}
ctx.putImageData(newImgData, imageData.width, 0);
});
});
</script>
</body>
</html>

最佳答案

This syntax: .load()已弃用,请改用 .on('load'...。此外,这不会延迟 .ready 中函数的执行。是否有理由可以难道要做简单、愚蠢的事情,只是在 .on('load', handler) 处理程序中放入对该函数的回调吗?

$(document).ready(function() {
$("<img>").attr("src", "test1image.jpg").on('load', function() {
// do stuff with dimensions

var imgWidth, imgHeight;
var imageData;
// ... etc.

});
});

关于javascript - jQuery/Javascript - 在(文档)之前运行函数。准备好加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24753940/

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