Пример #1
0
 .on('mouseup', function() {
     d3_select(window).on('mousemove.pannellum', null);
     // continue dispatching events for a few seconds, in case viewer has inertia.
     var t = d3_timer(function(elapsed) {
         dispatch.call('viewerChanged');
         if (elapsed > 2000) {
             t.stop();
         }
     });
 });
Пример #2
0
    function behavior(surface) {
        _done = false;
        _timer = d3_timer(function() {
            // wait for elements to actually become selected
            if (surface.selectAll(selector).empty()) {
                return false;
            }

            surface.call(run, 'from');
            _timer.stop();
            return true;
        }, 20);
    }
Пример #3
0
	function run() {

		timer = d3Timer((elapsed, time) => {

			// tell user how much time is left
			displayTimeLeft(duration - elapsed);

			// are we done?
			if (elapsed > duration) {
				timer.stop();

				// call user-provided callback, and pass along run,
				// so they can resume the timer, if so desired
				callback(run);
			}

		});

	}
Пример #4
0
    return new Promise((ok, ko) => {
        let results = new Array(runs + 1);
        results[0] = ['# run', 'ms', 'rows cfg=' + (sz * sz)];
        let i = 0;
        let t = d3timer(() => {
            let test = makeRandom(sz, sz, rng);
            let _start = performance.now();
            // timed function
            el.datum(test.a).call(tbl);
            let _end = performance.now() - _start;
            i++;
            results[i] = [i, _end, test.c];

            let done = (i > runs);
            if (done) {
                ok(results);
                t.stop();
            }
        });
    });
Пример #5
0
    return new Promise((resolve, reject) => {
      var begin = this._getStateValue(prop),
      i = interpolate(begin, end),
      easeFun = ease(easing)

      /* The timer stops when the callback retuns a truthy value */
      timer( (elapsed,d) => {

        if (this._setStopped) { return true; }

        var progress = easeFun( elapsed / duration ),

        value = i(progress)

        this._updateStateValue(prop, value, resolve)

        if (elapsed > duration) {
          this._updateStateValue(prop, end, resolve)
          resolve()
          return true;
        }

      })
    })
Пример #6
0
function create(node, id, self) {
  var schedules = node.__transition,
      tween;

  // Initialize the self timer when the transition is created.
  // Note the actual delay is not known until the first callback!
  schedules[id] = self;
  self.timer = timer(schedule, 0, self.time);

  function schedule(elapsed) {
    self.state = SCHEDULED;
    self.timer.restart(start, self.delay, self.time);

    // If the elapsed delay is less than our first sleep, start immediately.
    if (self.delay <= elapsed) start(elapsed - self.delay);
  }

  function start(elapsed) {
    var i, j, n, o;

    // If the state is not SCHEDULED, then we previously errored on start.
    if (self.state !== SCHEDULED) return stop();

    for (i in schedules) {
      o = schedules[i];
      if (o.name !== self.name) continue;

      // While this element already has a starting transition during this frame,
      // defer starting an interrupting transition until that transition has a
      // chance to tick (and possibly end); see d3/d3-transition#54!
      if (o.state === STARTED) return timeout(start);

      // Interrupt the active transition, if any.
      // Dispatch the interrupt event.
      if (o.state === RUNNING) {
        o.state = ENDED;
        o.timer.stop();
        o.on.call("interrupt", node, node.__data__, o.index, o.group);
        delete schedules[i];
      }

      // Cancel any pre-empted transitions. No interrupt event is dispatched
      // because the cancelled transitions never started. Note that this also
      // removes this transition from the pending list!
      else if (+i < id) {
        o.state = ENDED;
        o.timer.stop();
        delete schedules[i];
      }
    }

    // Defer the first tick to end of the current frame; see d3/d3#1576.
    // Note the transition may be canceled after start and before the first tick!
    // Note this must be scheduled before the start event; see d3/d3-transition#16!
    // Assuming this is successful, subsequent callbacks go straight to tick.
    timeout(function() {
      if (self.state === STARTED) {
        self.state = RUNNING;
        self.timer.restart(tick, self.delay, self.time);
        tick(elapsed);
      }
    });

    // Dispatch the start event.
    // Note this must be done before the tween are initialized.
    self.state = STARTING;
    self.on.call("start", node, node.__data__, self.index, self.group);
    if (self.state !== STARTING) return; // interrupted
    self.state = STARTED;

    // Initialize the tween, deleting null tween.
    tween = new Array(n = self.tween.length);
    for (i = 0, j = -1; i < n; ++i) {
      if (o = self.tween[i].value.call(node, node.__data__, self.index, self.group)) {
        tween[++j] = o;
      }
    }
    tween.length = j + 1;
  }

  function tick(elapsed) {
    var t = elapsed < self.duration ? self.ease.call(null, elapsed / self.duration) : (self.timer.restart(stop), self.state = ENDING, 1),
        i = -1,
        n = tween.length;

    while (++i < n) {
      tween[i].call(null, t);
    }

    // Dispatch the end event.
    if (self.state === ENDING) {
      self.on.call("end", node, node.__data__, self.index, self.group);
      stop();
    }
  }

  function stop() {
    self.state = ENDED;
    self.timer.stop();
    delete schedules[id];
    for (var i in schedules) return; // eslint-disable-line no-unused-vars
    delete node.__transition;
  }
}
Пример #7
0
export default function(nodes) {
  var simulation,
      iteration = 0,
      alphaMin = 0.001,
      alphaDecay = -0.02,
      drag = 0.6,
      forces = map(),
      stepper = timer(step),
      event = dispatch("tick", "end");

  if (nodes == null) nodes = [];

  function restart() {
    iteration = 0;
    stepper.restart(step);
    return simulation;
  }

  function stop() {
    stepper.stop();
    return simulation;
  }

  function step() {
    var stop = tick();
    event.call("tick", simulation);
    if (stop) {
      stepper.stop();
      event.call("end", simulation);
    }
  }

  function tick() {
    var alpha = Math.exp(++iteration * alphaDecay);

    forces.each(function(force) {
      force(alpha);
    });

    for (var i = 0, n = nodes.length, node; i < n; ++i) {
      node = nodes[i];
      node.x += node.vx *= drag;
      node.y += node.vy *= drag;
    }

    return alpha < alphaMin;
  }

  function initializeNodes() {
    for (var i = 0, n = nodes.length, node; i < n; ++i) {
      node = nodes[i], node.index = i;
      if (isNaN(node.x) || isNaN(node.y)) {
        var radius = initialRadius * Math.sqrt(i), angle = i * initialAngle;
        node.x = radius * Math.cos(angle);
        node.y = radius * Math.sin(angle);
      }
      if (isNaN(node.vx) || isNaN(node.vy)) {
        node.vx = node.vy = 0;
      }
    }
  }

  function initializeForce(force) {
    if (force.initialize) force.initialize(nodes);
    return force;
  }

  initializeNodes();

  return simulation = {
    restart: restart,
    stop: stop,
    tick: tick,

    nodes: function(_) {
      return arguments.length ? (nodes = _, initializeNodes(), forces.each(initializeForce), simulation) : nodes;
    },

    alphaMin: function(_) {
      return arguments.length ? (alphaMin = _, simulation) : alphaMin;
    },

    alphaDecay: function(_) {
      return arguments.length ? (iteration = +_ ? Math.round(iteration * alphaDecay / -_) : 0, alphaDecay = -_, simulation) : -alphaDecay;
    },

    drag: function(_) {
      return arguments.length ? (drag = 1 - _, simulation) : 1 - drag;
    },

    force: function(name, _) {
      return arguments.length > 1 ? ((_ == null ? forces.remove(name) : forces.set(name, initializeForce(_))), simulation) : forces.get(name);
    },

    on: function(name, _) {
      return arguments.length > 1 ? (event.on(name, _), simulation) : event.on(name);
    }
  };
}
Пример #8
0
 constructor() {
   this.shouldAnimate = true;
   this.subscribers = [];
   this.loop = this.loop.bind(this);
   this.timer = timer(this.loop);
 }
        );

        var {duration, ease, onStart, onEnd, logFPS} = this.props;
        ease = _.isString(ease) ?
            d3.ease(ease) :
            (_.isFunction(ease) ? ease : d3.ease('linear'));

        var i = 0;
        this._timer && this._timer.stop();
        onStart && onStart();
        var _timer = d3_timer.timer(p => {
            this.setState(interpolate(ease(p / duration)));
            i++;
            if (p >= duration) {
                onEnd && onEnd();
                if (logFPS) {
                    console.warn(i * (1000 / duration) + 'fps; ' + i + ' frames in ' + duration + 'ms');
                }
                _timer.stop();
            }
        });
        this._timer = _timer;
    },

    componentWillUnmount() {
        this._timer && this._timer.stop();
    },

    // render

    render: function () {