简体   繁体   English

按比例调整图像大小以适合HTML5画布

[英]Resize image proportionately to fit into HTML5 canvas

I am trying to write a jQuery plugin that will have similar functionality to the Flash based product editor on Zazzle.com. 我正在尝试编写一个jQuery插件,该插件将具有与Zazzle.com上基于Flash的产品编辑器类似的功能。 What I need to know is how, using the context.drawImage() canvas function, I can insert an image and resize it to fit in the canvas without distorting it. 我需要知道的是,使用context.drawImage() canvas函数,我可以插入一个图像并调整大小以适应画布而不会扭曲它。

The image is 500x500px and so is the canvas, but for some reason when I set 500x500 to the image dimentions it is way to big. 图像是500x500px,画布也是如此,但出于某种原因,当我将500x500设置为图像尺寸时,它是大的。

Here is my full code so far: 到目前为止,这是我的完整代码:

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

Any other constructive criticism also welcomed. 任何其他建设性的批评也受到欢迎。

This is the problem: 这就是问题:

$(canvas).attr('id', options.id).css({ width: options.width, height: options.height }); $(canvas).attr('id',options.id).css({width:options.width,height:options.height});

You are setting the CSS width/height of the canvas when you need to be setting the width and height attributes directly. 您需要直接设置宽度和高度属性时设置画布的CSS宽度/高度。 You are not distorting the image drawn, you are distorting the canvas itself. 你没有扭曲绘制的图像,你扭曲了画布本身。 The canvas is still 300x150 (the default) and has merely been CSS-stretched to be 500x500. 画布仍然是300x150(默认值),并且仅仅是CSS拉伸为500x500。 Thus right now you are drawing a 500x500 image on a 300x150 stretched canvas! 因此,现在您正在300x150拉伸画布上绘制500x500图像!

You need to do: 你需要这样做:

    var defaults = {
        'id'        :   'productEditor',
        'width'     :   '500',  // NOT 500px, just 500
        'height'    :   '500',  // NOT 500px, just 500
        'bgImage'   :   'http://www.wattzup.com/projects/jQuery-product-editor/sampleProduct.jpg'
    };

...

// Create canvas
var canvas = document.createElement('canvas');
canvas.width = options.width;
canvas.height= options.height;

...

$(canvas).attr('id', options.id); // DON'T change CSS width/height

Note that changing the width or height of a canvas clears it so this MUST be done before using drawImage. 请注意,更改画布的宽度或高度会将其清除,因此必须在使用drawImage之前完成此操作。

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

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