/    Sign up×
Community /Pin to ProfileBookmark

JS & Canvas – Draw Image to scale

Hello:

I am working with JQuery, and drawing images using Canvas (drawImage, translate, rotate) I am trying to draw an image and maintain its scale or aspect ratio so the image does not become distorted.

I have been working on a problem for many days now, I am writing a smartphone app and am having a problem displaying an image to scale, but also using the most area of the screen as possible.

For example:
-If the image is taller than it is wide and the phone is in portrait “tall” mode, then the image only has to be scaled, not rotated.

-If the image is taller than it is wide and the phone is in landscape “wide” mode, the image must be rotated and then scaled in order to maximize the displayed size

-If the image is wider than it is tall and the phone is in portrait mode, then the image has to be rotated and scaled.

-If the image is wider than it is tall and the phone is in landscape mode, then the image has to be only scaled, not rotated.

Seems simple enough, but I can’t get it to work out at all. Does anyone have an example code which would do this?

Thank you.

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @nomaam spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...