Zazzle.com の Flash ベースの製品エディターと同様の機能を持つ jQuery プラグインを作成しようとしています。私が知る必要があるのは、context.drawImage()
キャンバス関数を使用して、画像を挿入し、画像を歪ませずにキャンバスに収まるようにサイズを変更する方法です。
画像は 500x500px で、キャンバスもそうですが、何らかの理由で画像のサイズを 500x500 に設定すると大きくなりすぎます。
これまでの私の完全なコードは次のとおりです。
(function( $ ) {
jQuery.fn.productEditor = function( options ) {
var defaults = {
'id' : 'productEditor',
'width' : '500px',
'height' : '500px',
'bgImage' : 'http://www.wattzup.com/projects/jQuery-product-editor/sampleProduct.jpg'
};
return this.each(function() {
var $this = $(this)
var options = $.extend( defaults, options );
// Create canvas
var canvas = document.createElement('canvas');
// Check if their browser supports the canvas element
if(canvas.getContext) {
// Canvas defaults
var context = canvas.getContext('2d');
var bgImage = new Image();
bgImage.src = options.bgImage;
bgImage.onload = function () {
// Draw the image on the canvas
context.drawImage(bgImage, 0, 0, options.width, options.height);
}
// Add the canvas to our element
$this.append(canvas);
// Set ID of canvas
$(canvas).attr('id', options.id).css({ width: options.width, height: options.height });
}
// If canvas is not supported show an image that says so
else {
alert('Canvas not supported!');
}
});
};
})( jQuery );
その他の建設的な批判も歓迎します。