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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:03:21+00:00 2026-06-01T11:03:21+00:00

I have some complicated, problem to be solved. Now I need to write such

  • 0

I have some complicated, problem to be solved. Now I need to write such a function in javascript that supports object canvas in html5, which will cut out the portion of the picture and show it in the preview. I need to do than with diffrent resolution of images. I use the jQuery library for this, especially jquery.Jcrop.js .. The visible part of the picture in the preview I’m gona use later for further purposes, it must be writable in the cut form to file – so I use the opportunities of canvas. This script is working well, but the problem is when I try to scale a photo from the larger to the smaller one ( I want that each image will be fixed in the browser window to height 500 px). I present below the current figure of a script:

 function updatePreview(c)
 {
 if (parseInt(c.w) > 0)
 {
 var rx = 200 / c.w;
 var ry = 300 / c.h;

 // Show canvas image preview2
 var imageObj = $("#target")[0];
 var canvas = $("#preview2")[0];
 var context = canvas.getContext("2d");
 canvas.setAttribute('width', '200');
 canvas.setAttribute('height', '300');
 //context.scale(2, 2);
 context.drawImage(imageObj, c.x, c.y, c.w, c.h, 0, 0, 
 canvas.width, canvas.height);

 $('#preview').css({
 width: Math.round(rx * boundx) + 'px',
 height: Math.round(ry * boundy) + 'px',
 marginLeft: '-' + Math.round(rx * c.x) + 'px',
 marginTop: '-' + Math.round(ry * c.y) + 'px'
 });
 }
 };

And here’s a simplified html code that is for the script to be processing

 <table>
 <tr>
 <td>
 <img src="http://imgon.net/di-M7Z9.jpg" id="target" 
 alt="obrazek" height="450"/>
 </td>
 <td>
 <div style="width:200px;height:300px;overflow:hidden;">
 <img src="http://imgon.net/di-M7Z9.jpg" id="preview" alt="Preview" />
 </div>
 <br />
 <div>
 <canvas id="preview2" style="width:200px;height:300px;"></canvas>
 </div>
 </td>
 </tr>
 </table>

The first preview is based on css and it works correctly but css covers only part of the picture and not actually cut it. Contrast, canvas sees a picture in high resolution and preview does not reflect the segment of the scaled images (I have enlarged section only). This happens in Firefox. For Internet Explorer is a special library, and there result for the canvas is the same as for CSS. My final question is. How I can say to the function to create a preview for the scaled image and not the preview for the original resolution? I tried to put substitute for the variable c.y like Math.round (rx * boundx) and for cx Math.round (ry * boundy) but it is not right.
I give here a page where you can see the script live: agd-brita.pl/mobile2/tutorial.html
I ask you for help. Thanks for advance.

  • 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-01T11:03:23+00:00Added an answer on June 1, 2026 at 11:03 am

    I had to understand a little bit how jCrop works to answer you.

    I made a fiddle to show how my solution works: http://jsfiddle.net/maitrekaio/Wre8w/
    The main point is that working with CSS and working with canvas are really different and that you have to make different computations.
    Nevertheless, the starting is the same: you have an image element, target and a crop within. The details of the crop are given by jCrop, and the dimensions of the image element are stored in my displayedImg variable.

    The CSS preview

    The preview-container is like a window that let’s us see a portion of the contained image. The contained image element has not been resized, so its dimensions are those of the picture realImg. When the crop is resized and moved, we have to give the feeling that the window does the same. In fact, it’s the contained image that is resized and moved. How to compute that ?
    It’s easy to see that the ratio (crop / displayedImg) must be equal to the ratio (preview / realImg). That gives us the formula:
    realImg = (displayedImg * preview) / crop
    With this, it’s easy to calculate width, height, x (marginLeft) and y (marginTop) which are applied to the CSS preview.

    The canvas preview

    For the canvas, we know that we’ll use the most complex version of context.drawImage(), the one that allows us to crop and resize. The first parameter of this method is a JS image object, we’ll use the target image. This time we want to compute the crop in the real image, knowing the crop in the displayed image. (crop / displayedImg) must be equal to (realImgCrop / realImg). It gives us the formula:
    realImgCrop = (crop * realImg) / displayedImg
    Done !

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

Sidebar

Related Questions

I have some really complicated legacy code I've been working on that crashes when
Logging can get complicated, quickly. Considering that you have some code, how do you
I have a complicated nested form (Ryan Bates' version) with .live() attached to some
I have some UI in VB 2005 that looks great in XP Style, but
We have some input data that sometimes appears with &nbsp characters on the end.
So, I have this public API that my application exposes, allowing customers to write
I'm using Rails with the JQuery plugin, and have some really complicated validations for
I have some unicode text that I want to clean up using regular expressions.
I have made a script that run various loops and does some SQL inserts.
I have some problem with matrix multiplication: I want to multiplicate for example a

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.