zoom out and change blend level of image at the same time

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
keram
Posts: 340
Joined: Fri Nov 08, 2013 4:22 am

zoom out and change blend level of image at the same time

Post by keram » Fri Apr 18, 2014 1:05 pm

Hi,

I'd like to change the blend level of an image gradually and at the same time zoom out the image maintaining its location (loc)

For gradual change in blend level I use this code which works OK on its own:

Code: Select all

on mouseUp
   repeat with x = 98 down to 0 step -1
      set blendLevel of img "globe" to x
      wait 2 ticks
   end repeat
end mouseUp
For zooming out this code which also works OK on its own:

Code: Select all

on mouseUp
   put 40 into dim
   repeat until dim=130 
      put (dim + 1) into dim
      set the loc of img "globe" to 287,405
      set the width of img "globe" to dim
      set the height of img "globe" to dim
      wait 2 ticks
   end repeat
end mouseUp
When I combine them together into the code below the image does not stop expanding and I have to use Ctrl+Shift+. to stop it

Code: Select all

on mouseUp
   put 40 into dim
   repeat until dim=130 
      repeat with x = 98 down to 0 step -1
         set blendLevel of img "globe" to x
         put (dim + 1) into dim
         set the loc of img "globe" to 287,405
         set the width of img "globe" to dim
         set the height of img "globe" to dim
         wait 2 ticks
      end repeat
   end repeat
end mouseUp
Where am I missing the logic?

keram
Using the latest stable version of LC Community 6.7.x on Win 7 Home Premium, 64bit

magice
Posts: 457
Joined: Wed Mar 18, 2009 12:57 am

Re: zoom out and change blend level of image at the same tim

Post by magice » Fri Apr 18, 2014 1:30 pm

your inner repeat loop must complete all iterations before the outer repeat loop can move to the second iteration, so your loop of 98 actions is being performed 90 times.
Last edited by magice on Fri Apr 18, 2014 7:33 pm, edited 1 time in total.

SparkOut
Posts: 2943
Joined: Sun Sep 23, 2007 4:58 pm

Re: zoom out and change blend level of image at the same tim

Post by SparkOut » Fri Apr 18, 2014 2:55 pm

In other words you have been blending from 98 to 0 within each iteration of resizing. There are probably many ways of dealing with this, here's one:

Code: Select all

on mouseUp
   --start off with a fixed blend level that we change within the (single) resizing loop
   put 98 into tBlend
   repeat with dim = 40 to 130
      set the width of image "globe" to dim
      set the height of image "globe" to dim
      set the loc of image "globe" to 287,405
      set the blendLevel of image "globe" to tBlend
      subtract 1 from tBlend
      wait 2 ticks
   end repeat
   --the resizing loop has finished, so we just need another loop
   --to keep gradually blending in for the last few iterations down to 0
   repeat until tBlend is 0
      set the blendLevel of image "globe" to tBlend
      subtract 1 from tBlend
      wait 2 ticks
   end repeat
end mouseUp

magice
Posts: 457
Joined: Wed Mar 18, 2009 12:57 am

Re: zoom out and change blend level of image at the same tim

Post by magice » Fri Apr 18, 2014 3:51 pm

I am not in a place where i can test it at the moment, but I think this will work. Because of the difference between the 98 steps of the blendlevel process and the 90 steps of the dim process, it won't be perfect. you will essentially have 16 ticks between the two processes being completed.

Code: Select all

on mouseUp
 put 40 into dim
   repeat with x = 98 down to 0 step -1
      set blendLevel of img "globe" to x
   if dim < 130 
      then 
       add 1 to dim
       set the width of img "globe" to dim
       set the height of img "globe" to dim
       set the loc of img "globe" to 287,405
   end if
      wait 2 ticks
   end repeat
end mouseUp
However, I have never been a fan of using the wait command in this way, especially if it is within a repeat loop. Your homework assignment is to find a way to use the send in time command to do the same thing.

keram
Posts: 340
Joined: Fri Nov 08, 2013 4:22 am

Re: zoom out and change blend level of image at the same tim

Post by keram » Sat Apr 19, 2014 3:06 am

Hi magice and SparkOut,

Thank you both for your help! Both versions work equally well.

Now, my homework from magice :
Instead of waiting I have to 'send in 2 ticks' for the next repeat loop, right?

so would something like this... maybe?:

Code: Select all

on mouseUp
   put 40 into dim
   repeat with x = 98 down to 0 step -1
      set blendLevel of img "globe" to x
      if dim < 130
      then
         add 1 to dim
         set the width of img "globe" to dim
         set the height of img "globe" to dim
         set the loc of img "globe" to 287,405
      end if
      again
   end repeat
end mouseUp

on again
   send mouseUp to me in 2 ticks
end again
but that makes it zoom again and again, indefinitely and locks the LC

keram
Using the latest stable version of LC Community 6.7.x on Win 7 Home Premium, 64bit

magice
Posts: 457
Joined: Wed Mar 18, 2009 12:57 am

Re: zoom out and change blend level of image at the same tim

Post by magice » Sat Apr 19, 2014 10:23 am

Kerem,

I'll give you a hint. You won't use the repeat loop at all. Think about calling a custom handler that in turn can call itself if the a certain criteria is not yet met.

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: zoom out and change blend level of image at the same tim

Post by jmburnod » Sat Apr 19, 2014 10:30 am

Hi Keram,
but that makes it zoom again and again, indefinitely and locks the LC
Yes, because you created a loop in a loop
Here is way to use the "send in time" command with a quick and dirty solution to stop the pendingmessage
Is someone can explane why this script doesn't work ?
--if the width of img "globe" = 130 and the blendlevel of img "globe" = 0 then --doesn't work, never got 0 ??

Code: Select all

on mouseUp
   set the loc of img "globe" to 287,405
     set the blendlevel of img "globe" to 98
      set the width of img "globe" to 40
    set the height of img "globe" to 40
   setDimAndBlendLevel
end mouseUp

on setDimAndBlendLevel
   if the blendlevel of img "globe" > 1 then
      set the blendlevel of img "globe" to (the blendlevel of img "globe" -1)
   end if
   if the width of img "globe" < 130 then
      set the width of img "globe" to the width of img "globe" +1
      set the height of img "globe" to the height of img "globe" +1
      set the loc of img "globe" to 287,405
   end if
   put the width of img "globe"  && the blendlevel of img "globe" into fld "fDev"
   --if the width of img "globe" = 130 and the blendlevel of img "globe" = 0 then --doesn't work, never got 0 ??
      if the width of img "globe" = 130 and the blendlevel of img "globe" = 1 then
         set the blendlevel of img "globe" to 0 --quick and dirty solution
         doStopPending "setDimAndBlendLevel"
         exit to top
      end if
      send "setDimAndBlendLevel" to me in 10 milliseconds
end setDimAndBlendLevel

on doStopPending pMessage
   repeat for each line aLine in the pendingmessages
      if pMessage is in item 1 of aLine then
         cancel item 1 of aLine
      end if
   end repeat
end doStopPending
Kind regards
Jean-Marc
https://alternatic.ch

magice
Posts: 457
Joined: Wed Mar 18, 2009 12:57 am

Re: zoom out and change blend level of image at the same tim

Post by magice » Sat Apr 19, 2014 10:52 am

How about this:

Code: Select all

local x
local dim

on mouseUp
   put 40 into dim
   put 98 into x
   send zoomInPic to me in 2 ticks
end mouseUp

on zoomInPic
   if dim < 130 then add 1 to dim
    if x > 1
   then
      subtract 1 from x
      set the width of img "globe" to dim
      set the height of img "globe" to dim
      set the loc of img "globe" to 287,405
      send zoomInPic to me in 2 ticks
    end if
 set blendLevel of img "globe" to x
 end zoomInPic
The advantage to doing it this way, is that you are not tying up the processor the way you do with the repeat loop and wait. during the 2 ticks it can still execute other commands.Try pushing another button while the repeat script executes. Now try it with the send in time.

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: zoom out and change blend level of image at the same tim

Post by jmburnod » Sat Apr 19, 2014 11:20 am

I simplified my first script but magice was faster than me and his script works fine
https://alternatic.ch

keram
Posts: 340
Joined: Fri Nov 08, 2013 4:22 am

Re: zoom out and change blend level of image at the same tim

Post by keram » Sat Apr 19, 2014 12:17 pm

Hello Friends,

Thanks a lot for your comments and code. It's interesting that there are many solutions to the same task. And your code, magice, works like a magic(e) :) . It's simplest, most compact and it make sense, even I can understand when I go through it.

The only question that I have now is: when will I be able to write this kind of code on my own, from scratch? What does it take to develop such a skill? Is it time? More experience? Going through many samples of different stacks? Or some special neurons in the brain?
In any case it's great that experts like you are around to help :)

keram
Using the latest stable version of LC Community 6.7.x on Win 7 Home Premium, 64bit

magice
Posts: 457
Joined: Wed Mar 18, 2009 12:57 am

Re: zoom out and change blend level of image at the same tim

Post by magice » Sat Apr 19, 2014 7:52 pm

keram wrote:Hello Friends,

Thanks a lot for your comments and code. It's interesting that there are many solutions to the same task. And your code, magice, works like a magic(e) :) . It's simplest, most compact and it make sense, even I can understand when I go through it.

The only question that I have now is: when will I be able to write this kind of code on my own, from scratch? What does it take to develop such a skill? Is it time? More experience? Going through many samples of different stacks? Or some special neurons in the brain?
In any case it's great that experts like you are around to help :)

keram
Ok, let me clarify something. I am not an expert. That title is reserved for Klaus, Jacque, Fourthworld, Craig Newmen, and I guess even Simon. Not long ago I was asking the same questions you are, and these are the people who have always come to my rescue on this forum when I got stuck. For me the best way to learn is to follow this forum. When someone asks a question on here, sit down and experiment with different ways to solve their problem. Download every sample stack and study them. Create a file on your computer for miscellaneous stacks and save them. With every project I do I almost always refer back to at least one of those stacks to copy a piece of code. Even more important come up with an idea you want to create, and start working on it. The best way to learn to swim, is to be thrown into the lake.

Post Reply