Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Opacity number crunching loop?

Reply
Thread Tools

Opacity number crunching loop?

 
 
Tuxedo
Guest
Posts: n/a
 
      02-10-2007
I'd like to display an animation whereby images blend into each other, and
so have a question regarding the for-loop construct.

To start simply by a 2-image animation:

There is a 'div' containing the first and visible image.

<div id="container" style="width:300;height:250">
<img src="photo1.jpg" id="photo" width="300" height="250">
</div>

The background image of the div should become the second image by gradually
increasing the opacity of the foreground image.

Setting a background in the div while the first image is non-transparent,
can of course be done in CSS/JS as follows:

document.getElementById("container").style.backgro undImage="url(photo2.jpg)";

Therafter, I would like to gradually increase the opacity of the visible
photo from 0.1 (no opacity) to 0.0 (full opacity) thereby giving the
impression of the next image blending into view over the previous.

Setting opacity in JS/CSS is simply done, in the latest browsers, as
follows:

document.getElementById("photo").style.opacity=1.0 ; // no opacity

document.getElementById("photo").style.opacity=0.0 ; // full opacity

My question is, how can the loop be done, starting from no opacity to full
opacity, in for example 100 increments over a period of one second, by
setting/resetting the opacity and decimal values until the loop is complete?

Thanks in advance for any ideas.
 
Reply With Quote
 
 
 
 
Emmanuel
Guest
Posts: n/a
 
      02-11-2007
On Feb 10, 9:47 pm, Tuxedo <tux...@mailinator.net> wrote:
> My question is, how can the loop be done, starting from no opacity to full
> opacity, in for example 100 increments over a period of one second, by
> setting/resetting the opacity and decimal values until the loop is complete?


Here is the code which is at work at <http://beta.quomodo.com> (when
you edit a note all others become half-transparent). I edited it a bit
(for your purpose), so I may have introduced some bugs. On the other
hand, I provide the additional stuff for IE6.

Instructions for use: call qsn_dim with div = the element concerned,
dimtarget = the opacity value you want to reach

var nsteps = 100
var duration = 1000 // thousandths of a seconds, that is 1 second

function qsn_dim ( div , dimtarget ) {
div.dimtarget = dimtarget ;
do_qsn_dim( div ) ;
}

function do_qsn_dim( div ) {
if ( ! div.style.opacity ) div.style.opacity = "1" ;
var opa = parseFloat ( div.style.opacity ) ;
if ( div.dimtarget > opa ) {
opa = opa + 1.0/nsteps ;
if ( opa > div.dimtarget ) opa = div.dimtarget ;
} else {
opa = opa - 1.0/nsteps ;
if ( opa < div.dimtarget ) opa = div.dimtarget ;
}
div.style.opacity = "" + opa ;
div.style.filter = "alpha(opacity=" + Math.round ( 100 * opa ) +
")" ;
if ( opa != div.dimtarget ) setTimeout ( "do_qsn_dim ( '" + x +
"' )" , duration/nsteps ) ;
} // opacity: "0.5" ; filter: alpha(opacity=50)

 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      02-11-2007
On Feb 11, 6:47 am, Tuxedo <tux...@mailinator.net> wrote:
> I'd like to display an animation whereby images blend into each other, and
> so have a question regarding the for-loop construct.


Opacity has some issues in a cross-browser environment, notably
performance and different support in different browsers. Below is a
link to a discussion that you might find useful:

<URL: http://groups.google.com.au/group/fo...pt/browse_frm/
thread/e20278372e54f5b0?hl=en >

[...]
> Setting opacity in JS/CSS is simply done, in the latest browsers, as
> follows:
>
> document.getElementById("photo").style.opacity=1.0 ; // no opacity
>
> document.getElementById("photo").style.opacity=0.0 ; // full opacity


Safari has an issue with setting opacity to 1.0:

<URL: http://dev.rubyonrails.org/ticket/7063 >


--
Rob

 
Reply With Quote
 
Tuxedo
Guest
Posts: n/a
 
      02-11-2007
[..]

> hand, I provide the additional stuff for IE6.


Thanks for the customized example code, I will test it.

[..]

 
Reply With Quote
 
Tuxedo
Guest
Posts: n/a
 
      02-11-2007
> Opacity has some issues in a cross-browser environment, notably
> performance and different support in different browsers. Below is a
> link to a discussion that you might find useful:


Yes, that highlights especially the performance issues.

I find that for small dimension images, eg. 300x250px, its less of a drag
for slower systems, while full screen image blending for example will be
off the limits on nearly all cases, including systems with very fast CPU's
and graphics.

Once I've managed to figure the decreasing opacity incrementing loop
construct I'll test with setInterval and compare with setTimeout, which
sound like a good tip from that discussion, too.

 
Reply With Quote
 
Tuxedo
Guest
Posts: n/a
 
      02-11-2007
> Opacity has some issues in a cross-browser environment, notably
> performance and different support in different browsers. Below is a
> link to a discussion that you might find useful:


Yes, that highlights especially the performance issues.

I find that for small dimension images, eg. 300x250px, its less of a drag
for slower systems, while full screen image blending for example will be
off the limits on nearly all cases, including systems with very fast CPU's
and graphics.

Once I've managed to figure the increasing opacity incrementing loop
construct I'll test with setInterval and compare with setTimeout, which
sounds like a good tip from that discussion, too.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java ready for number crunching? jhc0033@gmail.com Java 40 06-23-2008 01:38 AM
java versus C or C++ for number crunching johnmortal.forums@gmail.com Java 21 03-26-2008 10:44 PM
Using c++ template metaprogramming for high performance number crunching? Ted C++ 5 09-24-2007 05:34 PM
EJB + number crunching Martin Ankerl Java 8 08-23-2005 12:08 AM
big number crunching in C Shuo Xiang C Programming 3 09-26-2003 06:04 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57