Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6706665
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:32:50+00:00 2026-05-26T07:32:50+00:00

I am trying to write a jQuery plugin that will have similar functionality to

  • 0

I am trying to write a jQuery plugin that will have similar functionality to the Flash based product editor on Zazzle.com. 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.

The image is 500x500px and so is the canvas, but for some reason when I set 500×500 to the image dimentions it is way to big.

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T07:32:50+00:00Added an answer on May 26, 2026 at 7:32 am

    This is the problem:

    $(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. You are not distorting the image drawn, you are distorting the canvas itself. The canvas is still 300×150 (the default) and has merely been CSS-stretched to be 500×500. Thus right now you are drawing a 500×500 image on a 300×150 stretched canvas!

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a jQuery plugin that will provide additional functions/methods to the
Trying to figure out how to write a jquery formula that will sum all
I am trying to write a jQuery plugin, that can be applied to a
I'm trying to do write a jQuery plugin based on a prototype pattern. My
i'm trying to write a jquery plugin that when i click on a image
I'm trying to write username validation using jquery, I'm using jmsajax plugin.I have tested
I'm trying to write a function in jQuery that will split lists - or
Im trying to write a quick plugin that will load some AJAX content into
For a JQuery plugin that I am trying to write I would like to
I am trying to write a jQuery script that will add a class to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.