Raphael.js - 带有原始宽度和高度尺寸的图像?

时间:2012-05-29 16:01:51

标签: javascript raphael

Hi RaphaelJS用户=),

我可能有一个愚蠢的问题,但我似乎无法找到答案,事情是,我正在尝试用Raphael加载/创建一个图像,这很好,就像这样:

var image_1 = paper.image('/img/img1.jpg', 0, 0, 100, 100);

但正如你所看到的,似乎你必须给出“宽度”和“高度”属性,我已经检查了文档,但它总是很短,而不是那么详细,我尝试给出空参数或空参数,但它不起作用......

所以我想知道在raphael中是否有直接的方法来做这件事,或者...... 我必须总是在这之前得到这些值吗?

我的意思是,这样的事情?:

var imgURL = "../img/img1.jpg"

var myImg = new Image();
myImg.src = imgURL;

var width = myImg.width;
var height = myImg.height;

   //create the image with the obtained width and height:
    var image_1 = paper.image('/img/img1.jpg', 0, 0, width, height);

所以这样我用图像的原始大小加载,这有点解决了我的问题,但......拉斐尔里面没有办法做到这一点???

4 个答案:

答案 0 :(得分:15)

现在......我想我必须用我自己的答案回答我自己的问题,因为似乎没有其他方式我找到或有人告诉我= P

var imgURL = "../img/img1.jpg"

var myImg = new Image();
myImg.src = imgURL;

var width = myImg.width;
var height = myImg.height;

   //create the image with the obtained width and height:
    var image_1 = paper.image('/img/img1.jpg', 0, 0, width, height);

答案 1 :(得分:8)

基于@ drzaus的精彩答案,我遇到了一些问题,如果我加载到Raphael中的图像还没有在缓存中,则高度和宽度将设置为0.要解决此问题:

(function(Raphael){
/// Plugin - replaces original RaphaelJS .image constructor
/// with one that respects original dimensions.
/// Optional overrides for each dimension.
/// @drzaus @zaus
/// based on http://stackoverflow.com/questions/10802702/raphael-js-image-with-its-original-width-and-height-size
/// modified 13/08/2013 by @Huniku to support asynchronous loads

var originalRaphaelImageFn = Raphael.fn.image;

Raphael.fn.imageAsync = function(url, x, y, w, h) {
    var dfd = new jQuery.Deferred();
    var done = false;
    var paper = this;
    // fix the image dimensions to match original scale unless otherwise provided
    if( !w || !h ) {
        //Create the new image and set the onload event to call
        //the original paper.image function with the desired dimensions
        var img = new Image();
        img.onload = function() {
            if(done)
                return;
            if( !w ) w = img.width;
            if( !h ) h = img.height;
            dfd.resolve(originalRaphaelImageFn.call(paper, url, x, y, w, h));
        };
        //Begin loading of the image
        img.src = url;

        //If the images is already loaded (i.e. it was in the local cache)
        //img.onload will not fire, so call paper.image here.
        //Set done to ensure img.onload does not fire.
        if(img.width != 0) {
            if( !w ) w = img.width;
            if( !h ) h = img.height;
            done = true;
            dfd.resolve(originalRaphaelImageFn.call(paper, url, x, y, w, h));
        }
    }
    else
        dfd.resolve(originalRaphaelImageFn.call(paper, url, x, y, w, h));
    return dfd.promise();
};
})(Raphael);

这允许图像在查询宽度/高度之前加载,并且还支持图像已经在缓存中并且不会触发onload事件的实例。要使用它,只需:

var dfd = paper.imageAsync("images/hello.png",0,0,false,false);
dfd.done(function( result ) {  //result is a Raphael element
    console.log('done');
    //do something with result
});

答案 2 :(得分:3)

有同样的问题,感谢提示 - 这是一个用现有的图像功能替换尺寸的插件:http://jsfiddle.net/drzaus/dhkh7/

为了更安全,可能不应该覆盖原始图像功能,以防它发生变化,但你明白了。

;(function(Raphael){
/// Plugin - replaces original RaphaelJS .image constructor
/// with one that respects original dimensions.
/// Optional overrides for each dimension.
/// @drzaus @zaus
/// based on http://stackoverflow.com/questions/10802702/raphael-js-image-with-its-original-width-and-height-size

var originalRaphaelImageFn = Raphael.fn.image;
Raphael.fn.image = function(url, x, y, w, h) {
    // fix the image dimensions to match original scale unless otherwise provided
    if( !w || !h ) {
        var img = new Image();
        img.src = url;
        if( !w ) w = img.width;
        if( !h ) h = img.height;
    }
    return originalRaphaelImageFn.call(this, url, x, y, w, h);
};
})(Raphael);

用法:

window.onload = function() {
  var paper = new Raphael('canvas', '100%', '100%');

  // specify dimensions as before
  paper.image('http://www.stockfreeimages.com/Swiss-panorama-thumb6499792.jpg', 10, 10, 100, 50).attr('stroke', '#000');

  // original ratio
  paper.image('http://www.stockfreeimages.com/Swiss-panorama-thumb6499792.jpg', 10, 100).attr('stroke', '#f00');

  // specify width, height taken from original
  paper.image('http://www.stockfreeimages.com/Swiss-panorama-thumb6499792.jpg', 10, 200, 100).attr('stroke', '#0f0');

  // specify height, width taken from original
  paper.image('http://www.stockfreeimages.com/Swiss-panorama-thumb6499792.jpg', 10, 300, false, 100).attr('stroke', '#00f');
};

答案 3 :(得分:0)

爱德华的评论对我有用,但我不得不把它放在间隔函数中,等到加入图像之前定义了宽度和高度(否则我得到了一个0x0图像)。这是我使用的代码:

var imgURL = "/img/img1.jpg"
var myImg = new Image();
myImg.src = imgURL;

function loadImagAfterWidthAndHeightAreDefined(){
  width = myImg.width;
  height = myImg.height;

  if(myImg.width > 0 && myImg.height > 0){
    image_1 = paper.image(imgURL, 0, 0, width, height);
  }
  else{
    setTimeout(function(){
      loadImagAfterWidthAndHeightAreDefined();
    },250);
  }
}
loadImagAfterWidthAndHeightAreDefined()
相关问题