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

  • SEARCH
  • Home
  • 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 948569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:15:53+00:00 2026-05-15T23:15:53+00:00

I am building a firefox plugin which allows users to draw any arbitrary figure

  • 0

I am building a firefox plugin which allows users to draw any arbitrary figure on an object and save it as an image (png file).

var $ = getJQueryOb();
var canvas = '<canvas id="canvas" width="1024" height="1024" style="position:absolute; top:0px; left:0px;"></canvas>';
var currentWindow = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator).getMostRecentWindow("navigator:browser");
var mainDoc = currentWindow.getBrowser().contentDocument;
var cObj = $(canvas).appendTo(mainDoc.body);
$(cObj).bind('mousemove', handlePenDraw);

Using this I can draw on the canvas. However, when I save the image, I do not want the full canvas to be saved – rather just the ‘bounding rectangle’ around the image that was created to be saved.

Is there any way I can acheive this. What I am currently doing is going to save the canvas as it is:

var $ = getJQueryOb();
var canvas = $('#canvas')[0];
var dURL = canvas.toDataURL("image/png");
saveToFile(dURL, "image-file.png");
  • 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-15T23:15:54+00:00Added an answer on May 15, 2026 at 11:15 pm

    You could store the top left and bottom right coords that have been drawn on in your handlePenDraw method then retrieve the area that has been drawn on using the getImageData method.

    Then put the imageData you’ve retrieved onto a new canvas that is only as big as the area drawn on to and then save the new canvas.

    • EDIT: I’ve now created a demo that does the above, except it doesn’t save the new canvas, but just appends it to a div – http://jsfiddle.net/SMh3N/12/

    Here’s some rough untested code:

    // Set these with these in your handlePenDraw method.
    var topLeftPoint, bottomRightPoint;
    var width = bottomRightPoint.x - topLeftPoint.x;
    var height = bottomRightPoint.y - topLeftPoint.y;
    
    // Retrieve the area of canvas drawn on.
    var imageData = context.getImageData(topLeftPoint.x, topLeftPoint.y, width, height);
    
    // Create a new canvas and put the retrieve image data on it
    var newCanvas = document.createElement("canvas");
    newCanvas.width = width;
    newCanvas.height = height;
    
    newCanvas.getContext("2d").putImageData(imageData, 0, 0);
    
    // Now call save to file with your new canvas
    var dURL = newCanvas.toDataURL("image/png");
    saveToFile(dURL, "image-file.png");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am building a site which uses jQUery validation plugin and want things validated
I am new to firefox extension development. I am building an extension which needs
I am building an open search add-on for Firefox/IE and the image needs to
I got a question to ask on building firefox plugin, basically my aim is
I am building an application which is working fine in IE8 and Mozilla Firefox,but
I am building a Firefox extension and have come a long way. One of
I am building a firefox extension. In this extension, I want to see the
I am using the firefox sqlite manager to help me building an iphone app.
Building a new Mobile Web Platform for Mobile Users to purchase & download content
Building a website that has English & Japanese speaking users, with the Japanese users

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.