gpt4 book ai didi

javascript - Canvas html 标签

转载 作者:行者123 更新时间:2023-11-28 02:06:43 25 4
gpt4 key购买 nike

我有一个 JavaScript 项目(使用 jquery),我在其中通过代码绘制 Canvas ,然后在其上插入图像数组。表示正常,没有任何错误。

问题: 稍后,我尝试从 Canvas 中获取这些图像以比较它们是否具有相同的 src 或相同的 id,以免用新图像替换它

我的职能是:

var inter;
var screen;

function onClick(id){
getScreen(id, function(data){
screen=window.open('',id,'width=' + data.maxX + ',height=' + data.maxY , true);
if (screen.document.getElementById("screen") != null ){
screen.document.getElementById("screen").innerHTML = "";
}
screen.document.write('<div id="screen"><canvas id="screenCanvas" width=' +
data.maxX + ' height=' + data.maxY +
' style="border:2px solid #000000;color:#000000;" ></canvas></div>');
draw(data);
inter = setInterval(function(){screen(id); }, 5000);
});
}

function screen(id){
getScreen(id, function(data){
if (screen.closed == true){
clearInterval(inter);
return;
}
if((screen.document.getElementById('screenCanvas').width != data.maxX) ||
(data.maxY != screen.document.getElementById('screenCanvas').height)){
screen.close();
screen=window.open('',id,'width=' + data.maxX + ',height=' + data.maxY , true);
screen=window.open('',id,'width=' + data.maxX + ',height=' + data.maxY , true);
if (screen.document.getElementById("screen") != null ){
screen.document.getElementById("screen").innerHTML = "";
}
screen.document.write('<div id="screen"><canvas id="screenCanvas" width=' +
data.maxX + ' height=' + data.maxY +
' style="border:2px solid #000000;color:#000000;" ></canvas></div>');
draw(data);
});
}

function draw(data) {
var canvas = screen.document.getElementById('screenCanvas');
var context = canvas.getContext('2d');
var tileY = 0;
var tileX = 0;
var counter = 0;
var tileWidth = data.tileWidth;
var tileHeight = data.tileHeight;
for (var i=0;i<(data.maxX/data.tileWidth);i++){
for (var j=0;j<(data.maxY/data.tileHeight);j++){
var img = new Image();
img.onload = (function(img, tileX, tileY, tileWidth, tileHeight){
return function() {
context.drawImage(img,tileX, tileY, tileWidth, tileHeight);
}
})(img, tileX, tileY, tileWidth, tileHeight);
img.src = "http://myserver:8080/images/screen/tile/" +
data.tiles[counter].imageId;
tileX = tileX + parseInt(data.tileWidth);
counter ++;
}
tileY = tileY + parseInt(data.tileHeight);
tileX = 0;
}
}

</script>

此代码获取一个数组(data),其中包含 (id, maxX, maxY, tileWidth, tileHeight, tiles[x, y, imageId]) 并打开新窗口,然后写入这个窗口的 Canvas 代码和绘制图像然后调用一个计时器每 5 秒工作一次。每 5 秒后,screen 方法调用重新获取数据并检查屏幕是否打开,然后删除所有内部 html 以重写新 Canvas ,并测试尺寸是否发生变化。

此代码工作正常,没有任何错误,但我需要编辑这些代码以在 Canvas 中获取图像并测试它们是否具有相同的 imageId,不要再次下载它。(我不保存图像 ID,以获取图片,我们可以将其存储在 img.id 中,或者通过 src 获取图片,因为 imageId 是图片路径的一部分,使其具有唯一性)。

注意:

id[unique](is the id of the user), 
maxX(maximum width of the screen),
maxY(maximum height of the screen),
tileWidth(width of each image),
tileHeight(height of each image),
tiles(list of images)
x(left position of the image)
y(top postion of the image)
imageId[unique](id of each image)
example: data[id:1,maxX:1920,maxY:1080,tileWidth:480,tileHeight:270,tiles:(x:2,y:1,imageId:1a)]

有什么帮助吗?

最佳答案

正如 Ravi Jain 所说,您无法将图像绘制到 canvas 对象,而正如 robertc 所说,您可以使用 img 元素。

尝试这些指定 img 元素而非 Canvas 用法的函数:

function onClick(id){
getScreen(id, function(data){
var inter;
var screen;
var width = parseInt(data.maxX) + parseInt(data.tileWidth);
var height = parseInt(data.maxY) + parseInt(data.tileHeight);
screen=window.open('',id,'width=' + width + ',height=' + height , true);
draw(screen, data);
inter = setInterval(function(){screen(screen, inter, id); }, 5000);
});
}

function screen(screen, inter, id){
getScreen(id, function(data){
if (screen.closed == true){
clearInterval(inter);
return;
}
if((screen.document.getElementById("screen").style.width != data.maxX + "px") ||
(screen.document.getElementById("screen").style.height != data.maxY + "px")){
screen.close();
var width = parseInt(data.maxX) + parseInt(data.tileWidth);
var height = parseInt(data.maxY) + parseInt(data.tileHeight);
screen=window.open('',id,'width=' + width + ',height=' + height , true);
}
draw(screen, data);
});
}

function draw(screen, data) {
var screenDiv = screen.document.getElementById("screen");
if (screenDiv == null) screen.document.write('<div id="screen" style="width:' +
data.maxX + '; height:' + data.maxY + '; " style="margin: 0 auto;" >');
var tileY = 0;
var tileX = 0;
var counter = 0;
for (var i=0;i<(data.maxX/data.tileWidth);i++){
for (var j=0;j<(data.maxY/data.tileHeight);j++){
var imageSource = screen.document.getElementById("id_" + counter);
var path = "http://myserver:8080/images/screen/tile/" +
data.tiles[counter].imageId;
if (imageSource == null){
screen.document.write('<img id="id_' + counter + '" src="' + path +
'" height="' + data.tileHeight + '" width="' + data.tileWidth + '" />');
}else if(imageSource.src != path){
imageSource.src = path;
}
tileX = tileX + parseInt(data.tileWidth);
counter ++;
}
tileY = tileY + parseInt(data.tileHeight);
tileX = 0;
}
if (screenDiv == null) screen.document.write('</div>');
}

</script>

你的代码工作正常,这是真的,但仅举一个例子,你在脚本顶部而不是在函数中声明了 var screen 和 var inter,这意味着如果你点击第一个用户来获取它的屏幕(据我了解您的代码,您的项目做了什么),第一个用户的计时器停止,第二个用户的计时器启动。我写的这段代码解决了这个问题和你的问题,如果图像发生变化则替换图像源。

希望这对你有帮助,

祝你好运;

关于javascript - Canvas html 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11032138/

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