javascript 自定义对象和图像加载

发布于 2025-01-01 09:12:28 字数 906 浏览 1 评论 0原文

我在使用 Safari/mobile Safari 加载和触发图像时遇到问题。我有一个在画布上绘制的自定义对象,但需要等待对象图像属性加载,因为它在绘制时使用图像的尺寸。下面的代码在 ff 上工作正常,但在 safari 中失败:

function canvasSlider(){
    this.slideImg=new Image();
    this.slideImg.src="images/back_button.png";
    this.somevalue=55;
}
canvasSlider.prototype.init = function (){
    alert(this.slideImg.complete + ', ' + this.slideImg.height+', ' + this.somevalue);
}
var myObj = new canvasSlider();
myObj.init();

在 safari 中, this.slideImg.complete 总是返回 false

我如何使用 this.slideImg.onload=somefunction();并始终确保canvasSlider对象被传递?

function canvasSlider(){
this.slideImg=new Image();
this.slideImg.onload=somefunction(this);
this.slideImg.src="images/back_button.png";
this.somevalue=55;
}
function somefunction(obj){
alert(obj.slideImg.complete+', '+obj.slideImg.src);
}
var myObj = new canvasSlider()

这是行不通的。有什么想法吗? 谢谢

i am having trouble with image loading and triggering with safari/mobile safari. i have a custom object that draws on a canvas but needs to wait until an object image property has loaded because it uses the dimensions of the image when drawing. the below code works fine on ff but fails in safari:

function canvasSlider(){
    this.slideImg=new Image();
    this.slideImg.src="images/back_button.png";
    this.somevalue=55;
}
canvasSlider.prototype.init = function (){
    alert(this.slideImg.complete + ', ' + this.slideImg.height+', ' + this.somevalue);
}
var myObj = new canvasSlider();
myObj.init();

in safari this will always return false for this.slideImg.complete

How can i use this.slideImg.onload=somefunction(); and always ensure the canvasSlider object is passed?

function canvasSlider(){
this.slideImg=new Image();
this.slideImg.onload=somefunction(this);
this.slideImg.src="images/back_button.png";
this.somevalue=55;
}
function somefunction(obj){
alert(obj.slideImg.complete+', '+obj.slideImg.src);
}
var myObj = new canvasSlider()

this doesn't work. any ideas?
Thanks

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

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

发布评论

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

评论(1

静赏你的温柔 2025-01-08 09:12:28

图像加载是异步的,因此您必须在图像加载成功后触发绘制操作。您的 onload 走在正确的道路上。您可以这样做:

function canvasSlider(readyFunc){
    var self = this;
    this.slideImg=new Image();
    this.slideImg.onload = function() {
        self.init();
        if (readyFunc) {
            readyFunc.call(self);
        }
    };
    this.somevalue=55;
    // The last thing you should do in the constructor is set the .src value
    // That's because if the image is in the cache, some browsers will trigger
    // onload immediately when you set .src and we want the canvasSlider object 
    // to be fully formed when init() and readyFunc() are called.
    this.slideImg.src="images/back_button.png";
}
canvasSlider.prototype.init = function (){
    alert(this.slideImg.complete + ', ' + this.slideImg.height+', ' + this.somevalue);
}
var myObj = new canvasSlider(function() {
    // The slider is ready with the image loaded here 
    // and .init() has already been called.
    // "this" right here is the canvasSlider object
    // you can do anything else you wanted to do once the image was loaded
});
// The image is not necessarily loaded here.  
// You have to put anything that relies on the image being loaded in the 
// above callback to assure it is not executed until the image is loaded.

注意:在测试代码时,如果图像位于浏览器缓存中,某些浏览器将立即触发 onload。您应该测试两种方式(浏览器缓存中有图像和浏览器缓存中没有图像),以确保两种情况都能正常工作。开发人员通常认为一切正常(因为图像位于浏览器缓存中),但对于下一个尝试它的用户来说却不起作用。

Images load asynchronously so you have to trigger the drawing operation when the image is successfully loaded. You were on the right path with onload. You can do that like this:

function canvasSlider(readyFunc){
    var self = this;
    this.slideImg=new Image();
    this.slideImg.onload = function() {
        self.init();
        if (readyFunc) {
            readyFunc.call(self);
        }
    };
    this.somevalue=55;
    // The last thing you should do in the constructor is set the .src value
    // That's because if the image is in the cache, some browsers will trigger
    // onload immediately when you set .src and we want the canvasSlider object 
    // to be fully formed when init() and readyFunc() are called.
    this.slideImg.src="images/back_button.png";
}
canvasSlider.prototype.init = function (){
    alert(this.slideImg.complete + ', ' + this.slideImg.height+', ' + this.somevalue);
}
var myObj = new canvasSlider(function() {
    // The slider is ready with the image loaded here 
    // and .init() has already been called.
    // "this" right here is the canvasSlider object
    // you can do anything else you wanted to do once the image was loaded
});
// The image is not necessarily loaded here.  
// You have to put anything that relies on the image being loaded in the 
// above callback to assure it is not executed until the image is loaded.

Note: when testing your code, some browsers will fire onload immediately if the image is in the browser cache. You should test both ways (with the image in the browser cache and without the image in the browser cache) to make sure that both cases are working properly. It is common for developers to think everything works fine (because the image is in their browser cache) and then it doesn't work for the very next user who tries it.

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