有同样的问题,谢谢你的提示——这是一个用尊重尺寸的插件替换现有图像功能的插件: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');
};