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 8791265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:49:08+00:00 2026-06-13T22:49:08+00:00

There are hundreds of tutorials, how one can crop an image by drawImage() on

  • 0

There are hundreds of tutorials, how one can crop an image by drawImage() on a canvas.

context.drawImage(imageObj, sourceX, sourceY, sourceWidth, sourceHeight, destX, destY, destWidth, destHeight);

However, I have a canvas that fills the user’s browser. By exporting the canvas as an image I would like to export only an area of 640px*480px from (0|0).

Problem: How can I tell javascript to use only 640*480 of the canvas for the toDataURL()?

Here is what I have so far:

$("#submitGraphic").click( function(){
    var canvas = document.getElementsByTagName("canvas");
    // canvas context
    var context = canvas[0].getContext("2d");
    // get the current ImageData for the canvas
    var data = context.getImageData(0, 0, canvas[0].width, canvas[0].height);
    // store the current globalCompositeOperation
    var compositeOperation = context.globalCompositeOperation;
    // set to draw behind current content
    context.globalCompositeOperation = "destination-over";
    //set background color
    context.fillStyle = "#FFFFFF";
    // draw background/rectangle on entire canvas
    context.fillRect(0,0,canvas[0].width,canvas[0].height);

    // not working, seems to clear the canvas? browser hangs?
    // seems that I can click a white image in the background
    /*canvas[0].width = 640;
    canvas[0].height = 480;*/

    // not working either
    /*canvas[0].style.width  = '640px';
    canvas[0].style.height = '480px';*/

    // not working at all
    /*context.canvas.width = 640;
    context.canvas.height = 480;*/

    // write on screen
    var img = canvas[0].toDataURL("image/png");
    document.write('<a href="'+img+'"><img src="'+img+'"/></a>');
})

PS: I do not want to resize or scale, just clipping/cropping to the fixed window. Here I read that you only specifiy canvas.width and canvas.height – but this clears the canvas.

  • 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-06-13T22:49:09+00:00Added an answer on June 13, 2026 at 10:49 pm

    The best way is to just create a temporary canvas to draw onto from the current canvas. The user will never see this temp canvas. Then you just need use toDataUrl() on the temp canvas.

    Live Demo

    $("#submitGraphic").click( function(){
        var canvas = document.getElementsByTagName("canvas");
        // canvas context
        var context = canvas[0].getContext("2d");
        // get the current ImageData for the canvas
        var data = context.getImageData(0, 0, canvas[0].width, canvas[0].height);
        // store the current globalCompositeOperation
        var compositeOperation = context.globalCompositeOperation;
        // set to draw behind current content
        context.globalCompositeOperation = "destination-over";
        //set background color
        context.fillStyle = "#FFFFFF";
        // draw background/rectangle on entire canvas
        context.fillRect(0,0,canvas[0].width,canvas[0].height);
    
        var tempCanvas = document.createElement("canvas"),
            tCtx = tempCanvas.getContext("2d");
    
        tempCanvas.width = 640;
        tempCanvas.height = 480;
    
        tCtx.drawImage(canvas[0],0,0);
    
        // write on screen
        var img = tempCanvas.toDataURL("image/png");
        document.write('<a href="'+img+'"><img src="'+img+'"/></a>');
    })​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know there are hundreds of posts on here about htaccess but I can't
The one thing I don't like about javascript is that there are hundreds of
There are hundreds of tutorials of how to create your own simple MVC I
I know there's hundreds of jQuery plug-ins for dropdowns, but I can't seem to
I'm developing an app in which there's hundreds of different calculations involved and would
I have a small problem... There are hundreds of images in a folder. they
I have a mysql database in which there is about hundreds of category for
Setting up environment variables for hundreds of tests get old very quick. Is there
I know there are hundreds of questions about UTF-8 woes but I tried all
I have joining two tables,There are hundreds of records in table a and there

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.