画布图像问题

发布于 2024-11-10 18:33:23 字数 1329 浏览 1 评论 0原文

我在使用 JavaScript 和 Canvas 时遇到问题,尤其是在 Firefox 中。我正在尝试创建一个画布图像查看器,当在浏览器中启用画布时,它将运行,而不是标准 img 标签。单击箭头时,Javascript 会更改 src 路径数组中的图像。由于某种原因,Firefox 总是有一张图像不同步。当我在 Chrome 中测试它时,图像是同步的,但第一个图像没有显示。

这是图像对象

var image = new Image();
image.src = myImage.src[0];

,这是我的初始画布初始化

if(canvas()){ //canvas stuff
var myc = document.createElement('canvas');
myc.id='mycanvasx';
mycanvas = document.getElementById('mycanvasx');
myc.width = image.width;
myc.height = image.height;
//viewer.replaceChild(myc, scroller);
viewer.appendChild(myc);
var context = myc.getContext("2d");
dw = image.width;
dh = image.height;

context.clearRect(0, 0, myc.width, myc.height);
context.drawImage(image, 0, 0, dw, dh); 

}

,当按下箭头时,会调用这样的函数,

function update(){ 
if(canvas()){
context.clearRect(0,0,myc.width, myc.height) //maybe bloat
myc.width = image.width;
myc.height = image.height;
dw = image.width;
dh = image.height;

context.drawImage(image, 0, 0, dw, dh); 

} 
} 

function left(){ //these move the image through the source array
if(myImage.num > 0){myImage.num--;}else{
myImage.num = (myImage.src.length-1)}
image.src = myImage.src[myImage.num];
scroller.src = image.src;
update();

x=0;
}

我知道我一定错过了一些简单的东西。我使用的是 Firefox 4.0.1 和 chrome 11

I am having issues with JavaScript and Canvas particularly in Firefox. I am trying to create an canvas image viewer that will run when canvas is enabled in a browser in place of standard img tag. Javascript changes the image from an array of src paths when an arrow is clicked. For some reason Firefox is always one img out of sync. When I test it in chrome the imgs are in sync but the first one is not displayed.

here is the image object

var image = new Image();
image.src = myImage.src[0];

here is my initial canvas intialization

if(canvas()){ //canvas stuff
var myc = document.createElement('canvas');
myc.id='mycanvasx';
mycanvas = document.getElementById('mycanvasx');
myc.width = image.width;
myc.height = image.height;
//viewer.replaceChild(myc, scroller);
viewer.appendChild(myc);
var context = myc.getContext("2d");
dw = image.width;
dh = image.height;

context.clearRect(0, 0, myc.width, myc.height);
context.drawImage(image, 0, 0, dw, dh); 

}

and when the arrow is pressed a function like this is called

function update(){ 
if(canvas()){
context.clearRect(0,0,myc.width, myc.height) //maybe bloat
myc.width = image.width;
myc.height = image.height;
dw = image.width;
dh = image.height;

context.drawImage(image, 0, 0, dw, dh); 

} 
} 

function left(){ //these move the image through the source array
if(myImage.num > 0){myImage.num--;}else{
myImage.num = (myImage.src.length-1)}
image.src = myImage.src[myImage.num];
scroller.src = image.src;
update();

x=0;
}

I know I must be missing something simple here. I'm using firefox 4.0.1 and chrome 11

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷路的信 2024-11-17 18:33:25

您正在设置图像的 src,然后立即将其绘制到画布中。但图像加载是异步的。所以你要在你设置的新 src 加载之前绘制它;由于您不断重复使用同一张图像,因此此时它仍然显示上一张图像。

您想要在 image 上的 onload 侦听器上运行 update()

You're setting the src of the image and then immediately painting it into the canvas. But image loads are async. So you're painting it before the new src you set has loaded; since you keep reusing the same image it's still showing the previous image at that point.

You want to run update() off an onload listener on image.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文