简体   繁体   English

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

[英]Raphael.js - image with it's original width and height size?

Hi RaphaelJS users =) , 嗨RaphaelJS用户=),

I have maybe a dumb-ish question, but I can't seem to find the answer, the thing is, I'm trying to load/create an image with Raphael, that's fine, like this: 我可能有一个愚蠢的问题,但我似乎无法找到答案,事实是,我正在尝试用Raphael加载/创建一个图像,这很好,就像这样:

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

But as you can see, it always seems like you have to give the "width" and "height" properties, I already checked the documentation, but it's always short and not that's detailed, and I tried giving null parameters or empty parameters, but it doesn't work... 但正如你所看到的,似乎你必须给出“宽度”和“高度”属性,我已经检查了文档,但它总是很短而且不是那么详细,我尝试给出空参数或空参数,但是它不起作用......

So I'm wondering if there is actually a direct way in raphael to do this, or.... do I have to always get those values before??? 所以我想知道raphael实际上是否有直接的方式来做这个,或者...... 我必须总是在之前获得这些值吗?

I mean, something like this?: 我的意思是,这样的事情?:

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);

And so that way I load with the original size of the image and that kinda solves my problem, but....isn't there way inside Raphael to do this??? 因此,我加载图像的原始大小,这有点解决了我的问题,但....拉斐尔内部没有办法做到这一点???

For now...I guess I'll have to answer my own question with my own answer, cause there seems to be no other way that I've found or that someone has told me =P 现在......我想我必须用我自己的答案回答我自己的问题,因为似乎没有其他方式我找到或有人告诉我= 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);

Building off of @drzaus's awesome answer, I had some issues where if the image I was loading into Raphael was not already in the cache, the height and width would be set to 0. To fix this: 基于@ 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);

This allows the image to load before before querying it for width/height, and also supports the instance where the image is already in the cache and the onload event is not fired. 这允许图像在查询宽度/高度之前加载,并且还支持图像已经在缓存中并且不触发onload事件的实例。 To use this, simply: 要使用它,只需:

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
});

Had same problem, thanks for the tip -- here's a plugin to replace the existing image function with one that respects dimensions: http://jsfiddle.net/drzaus/dhkh7/ 有同样的问题,感谢提示 - 这是一个插件来替换现有的图像功能与尊重尺寸的插件: http//jsfiddle.net/drzaus/dhkh7/

To be safer, should probably not overwrite the original image function just in case it ever changes, but you get the idea. 为了更安全,应该不要覆盖原始图像功能,以防万一它发生变化,但你明白了。

;(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);

And usage: 用法:

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');
};

Edward's comment worked for me but I had to place it in an interval function and wait until the width and height are defined before adding the image (otherwise I got a 0x0 image). 爱德华的评论对我有用,但我不得不把它放在一个区间函数中,等到加入图像之前定义宽度和高度(否则我得到一个0x0图像)。 Here is the code I used: 这是我使用的代码:

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM