Resizing an image with min and max limits

Getting into LiveCode for iOS? Ask your questions here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Glyphs
Posts: 7
Joined: Sat Feb 26, 2011 12:17 pm
Contact:

Resizing an image with min and max limits

Post by Glyphs » Fri Mar 04, 2011 11:53 pm

Hi all,

I have created an app with multi-touch resizing of images using the "How do I implement a multi-touch pinch motion?" lesson, but I can't seem to set limits to the resizing. What lines could I add to the code of the sample stack from this lesson to have a min size and and a max size for my image?

Klaus
Posts: 14182
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Resizing an image with min and max limits

Post by Klaus » Sat Mar 05, 2011 12:19 pm

Hi Glyphs,

could you please post the relevant part(s) of the code?


Best

Klaus

Glyphs
Posts: 7
Joined: Sat Feb 26, 2011 12:17 pm
Contact:

Re: Resizing an image with min and max limits

Post by Glyphs » Sat Mar 05, 2011 3:02 pm

Klaus wrote:Hi Glyphs,
could you please post the relevant part(s) of the code?
Here is the code of the Pinchlesson.rev stack. You can download the stack in the How do I implement a multi-touch pinch motion? lesson page in the RunRev website (I would have posted the link, but it seems I don't have permission yet with this account) I only replaced the "graphic "square"" part by the name of the object I wanted to resize. Problem is the resizing seems unlimited. I have a 320x480 image I want to resize dynamically, with a min size of 320x480 (no need to reduce the size) and a max size of 480x720. I have tried to tweak the "on resizeGraphic" part of the code, by trying anything I know to put a max size and a min size to at least the width part (I believe I have to work on only one size for this to work). So, for the max width, I replaced :
set the width of graphic "square" to round(sFRAMEWIDTH * (tPercentage / 100))
by
if the width of image "Small" < 480 then set the width of image "Small" to round(sFRAMEWIDTH * (tPercentage / 100))
if the width of image "Small" >= 480 then ----

After this latest "then", I have tried everything I know (but I am still a beginner). "set the width of image "Small" to 480" and "set the maxwidth of image "Small" to 480" both work, but block the resizing at 480, meaning no resizing is taking place anymore once the image width reaches 480.
"put 100 into tPercentage" blocks the resizing at 480 too.

At this stage, I think I would need to find a way to enter a fixed value for either the percentage or the maxwidth, then reset this value so that the resizing could continue, but there might be a better way to do all this. I am not sure about what to do next.

Here is the original code:
--
local sTouchArray, sFRAMEWIDTH, sFRAMEHEIGHT

on touchStart pId, pX, pY
put the width of graphic "square" into sFRAMEWIDTH
put the height of graphic "square" into sFRAMEHEIGHT
end touchStart

on touchEnd pId
delete variable sTouchArray[pId]
end touchEnd

on touchMove pId, pX, pY

if sTouchArray[pId]["startloc"] is empty then
put (pX & comma & pY) into sTouchArray[pId]["startloc"]
end if

put (pX & comma & pY) into sTouchArray[pId]["currentloc"]

if the number of lines of the keys of sTouchArray is 2 then
# First lets get the data out of the array
put line 1 of the keys of sTouchArray into tPointOne
put line 2 of the keys of sTouchArray into tPointTwo

# First lets calculate the size of the picture base on the distance
# between the two touch points
put sTouchArray[tPointOne]["startloc"] into tStartLoc1
put sTouchArray[tPointTwo]["startloc"] into tStartLoc2
if tStartLoc1 is not empty and tStartLoc2 is not empty then
put resizeDistance(tStartLoc1, tStartLoc2) into tStartDistance
put resizeDistance(sTouchArray[tPointOne]["currentloc"], sTouchArray[tPointTwo]["currentloc"]) into tCurrentDistance
resizeGraphic tStartDistance, tCurrentDistance
end if
end if
end touchMove

function resizeDistance pLoc1, pLoc2
local dy, dx, tDistance

put item 2 of pLoc1 - item 2 of pLoc2 into dy
put item 1 of pLoc1 - item 1 of pLoc2 into dx
put sqrt((dy*dy) + (dx*dx)) into tDistance

return tDistance
end resizeDistance

on resizeGraphic pStartDistance, pNewDistance
# Work out the percentage change between the old and new image
put round((pNewDistance / pStartDistance) * 100) into tPercentage

# Store the original location of the graphic
put the loc of graphic "square" into tLoc

# Calculate the new width and height
set the width of graphic "square" to round(sFRAMEWIDTH * (tPercentage / 100))
set the height of graphic "square" to round(sFRAMEHEIGHT * (tPercentage / 100))

set the loc of graphic "square" to tLoc
unlock screen
end resizeGraphic

--

Post Reply