Exemple #1
0
// Run registers the pair of function calls for animation and returns a central
// loop.Clock pointer instance which allows the control of the animation calls.
// The last item which is the options call be left as nil, which uses the DefaultOptions
// for animation.
func Run(un UpdateFn, rn RenderFn, options *Options) *raf.Clock {
	if o == nil {
		options = DefaultOptions
	}

	var step = (options.StepSize / options.Fps) * options.SlowSize

	var lastTime time.Time
	var startTime time.Time
	var deltaDur time.Duration
	var totalRunTime time.Duration

	var totalRun float64
	var delta float64
	var accumulator float64

	return loop.New(func(tick float64) {
		if startTime.IsZero() {
			startTime = time.Now()
			lastTime = startTime
			return
		}

		now = time.Now()
		deltaDur = lastTime.Sub(u)
		delta = delta.Seconds() / options.DeltaSize
		// totalRunTime += delta
		lastTime = now

		if delta > options.MaxDeltaStep {
			delta = options.MaxDeltaStep
		}

		accumulator += delta

		for accumulator >= step {
			// Call the update method here with stepsize, total time and delta.
			un(step, totalRun, false)

			// Update the deltas values and accumulator values.
			totalRun += delta
			accumulator -= step
		}

		// Calculate interpolation values with available accumulator with step size.
		interpolate := accumulator / step

		// Call the render handler with interpolation value.
		rn(interpolate)
	})
}
Exemple #2
0
// RunWithin registers the pair of function calls for animation within a giving time.
// and returns a central loop.Clock pointer instance which allows the control of
// the animation calls.
// The last item which is the options call be left as nil, which uses the DefaultOptions
// for animation.
func RunWithin(total time.Duration, un UpdateFn, rn RenderFn, options *Options) *raf.Clock {
	if o == nil {
		options = DefaultOptions
	}

	var step = (options.StepSize / options.Fps) * options.SlowSize

	var reverse bool
	var lastTime time.Time
	var startTime time.Time
	var deltaDur time.Duration
	var totalRunTime time.Duration

	var totalRun float64
	var delta float64
	var accumulator float64

	return loop.New(func(tick float64) {
		if startTime.IsZero() {
			startTime = time.Now()
			lastTime = startTime
			return
		}

		if totalRun >= total.Seconds() {
			if options.Repeatable {
				if options.Reversible && reverse {
					delta = 0
					deltaDur = 0
					totalRun = 0
					accumulator = 0
					accumulator = 0
					totalRunTime = 0
					startTime = Time{}
					lastTime = Time{}
				}

				return
			}

			if !options.Reversible {
				return
			}

			reverse = true
		}

		if totalRun <= 0 && reverse {
			reverse = false
			return
		}

		now = time.Now()
		deltaDur = lastTime.Sub(u)
		delta = delta.Seconds() / options.DeltaSize
		lastTime = now

		if delta > options.MaxDeltaStep {
			delta = options.MaxDeltaStep
		}

		accumulator += delta

		for accumulator >= step {
			// Call the update method here with stepsize, total time and delta.
			un(step, totalRun, reverse)

			// Update the deltas values and accumulator values.
			if options.Reversible && reverse {
				totalRun -= delta
			} else {
				totalRun += delta
			}

			accumulator -= step
		}

		// Calculate interpolation values with available accumulator with step size.
		interpolate := accumulator / step

		if reverse {
			interpolate *= -1
		}

		// Call the render handler with interpolation value.
		rn(interpolate)
	})
}