Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WPF News Ticker - timing issues

Status
Not open for further replies.

Borvik

Programmer
Jan 2, 2002
1,392
0
0
US
I'm working on a news ticker application as part of a company dashboard.

I'm basing the ticker on RumorMill ticker found
Instead of scrolling horizontally, mine is scrolling vertically, and because there may be a small number of items they get removed as soon as they clear the display area - which then can get them put back into the queue again (assuming it wasn't removed from the database).

My issue is with the timing/speed of the news items. Some items seem to be scrolling faster than others - with the chance that some could overlap.

I've been operating with the following equations:
s = d/t (general speed formula)
w = window height
h = item height
t = specified time on screen
t[sub]1[/sub] = actual time to target

The animation moving the item from window height (item clipped below the panel) to the item's height above the window.
w/t = s = (w+h)/t[sub]1[/sub]
(w+h)/s = t[sub]1[/sub]

Here is the code for the animation:
Code:
    Private Sub AnimateMove(ByVal e As FrameworkElement)
        ' Max Width/Height set in calling routine based on orientation
        '  of the scroll.
        e.Measure(New Size(e.MaxWidth, e.MaxHeight))
        'e.Measure(New Size(Double.PositiveInfinity, Double.PositiveInfinity))
        Dim r As New Rect(e.DesiredSize)
        e.Arrange(r)

        Dim [from] As Double = If(_orientation = Controls.Orientation.Horizontal, Container.ActualWidth, Container.ActualHeight)
        Dim elSize As Double = If(_orientation = Controls.Orientation.Horizontal, e.ActualWidth, e.ActualHeight)
        Dim [to] As Double = 0 - elSize

        Dim windowSpeed As Double = ([from] / Speed.TotalSeconds)
        Dim newTime As Double = (([from] + elSize) / windowSpeed)
        Dim actualDuration As New TimeSpan(0, 0, Convert.ToInt32(newTime))
        Dim nextFire As Integer = Convert.ToInt32((elSize + SeperatorSize) / windowSpeed)

        'This is what triggers the next item to be added to the container - and then animated with this subroutine
        displayTimer.Stop()
        displayTimer.Interval = New TimeSpan(0, 0, nextFire)
        displayTimer.Start()

        Dim ani As New TaggedDoubleAnimation
        ani.From = [from]
        ani.To = [to]
        ani.Duration = New Duration(actualDuration)
        ani.TargetElement = e
        Dim depProp As DependencyProperty = If(_orientation = Controls.Orientation.Horizontal, TranslateTransform.XProperty, TranslateTransform.YProperty)
        AddHandler ani.Completed, AddressOf ani_Completed

        Dim trans As New TranslateTransform()
        e.RenderTransform = trans
        trans.BeginAnimation(depProp, ani, HandoffBehavior.Compose)
    End Sub

Any ideas on what might be causing the timing to be off?
 
The height of your strings is different and therefore they are moving at different speeds.
 
According to my calculations I am compensating for that.

I first calculate the speed based on the given duration for the height of the container:
Code:
Dim windowSpeed As Double = ([from] / Speed.TotalSeconds)
I then use that to calculate the new duration for the specific item from the speed (which stays the same from each run to the next - verified) and the container height + the item height:
Code:
Dim newTime As Double = (([from] + elSize) / windowSpeed)
That time is what is used as the actual duration of the animation - keeping the speed constant between the strings.
 
I figured it out. It was a rounding issue. All the calculations use doubles for increased precision, but then stick that double into the seconds parameter of a timespan (an Integer). By multiplying it by 1000, and placing it in the milliseconds parameter it increased the precision of the timespan, and solved the issue.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top