2015-05-22 119 views
0

我正在上传图像,然后使用fabricjs将其显示在画布上。上传的图片很好。但是当我用帆布装入它时,它扭曲且模糊。 这是我用来在画布中加载图像的代码。图像使用fabricjs在画布中变形和模糊

$("#canvas_area").append('<canvas id="c"></canvas>'); 
var canvas = new fabric.Canvas('c'); 
canvas.setDimensions({ 
    width: '100%', 
    height: '100%' 
}, { 
    cssOnly: true 
}); 
var imgElement = response.uploaded_file; 
fabric.Image.fromURL(imgElement, function (img) { 
    img.set({ 
     borderColor: 'red', 
     cornerColor: 'green', 
     cornerSize: 6, 
     transparentCorners: false 
    }); 
    if (img.height > img.width) { 
     img.scaleToHeight(80); 
    } 
    if (img.height < img.width) { 
     img.scaleToWidth(120); 
    } 
    if (img.height == img.width) { 
     img.height = 100; 
     img.width = 200; 
    } 
    img.set('left', 10); 
    img.set('top', 10); 
    img.setCoords(); 
    canvas.add(img); 
}); 

此外,它调整大小的方形图像(图像宽度==图像高度)为矩形形状的图像。

任何帮助将不胜感激。感谢

+0

像这样http://jsfiddle.net/dhirajbodicherla/Vp6wa/183/? – Dhiraj

+0

为什么它会将方形图像(图像宽度==图像高度)调整为矩形图像。我也有这个问题。 – nomi416

回答

2

你的代码集在第三if

if (img.height == img.width) { 
    img.height = 100; 
    img.width = 200; 
} 

所以任何方(1:1)图像失真是一个2:1的图像。

我会写这样的调整大小:

if (img.height > img.width) { 
    img.scaleToHeight(80); 
} 
if (img.height <= img.width) { //include square images here 
    img.scaleToWidth(120); 
}