animation - Javascript physics easing/attraction maths -



animation - Javascript physics easing/attraction maths -

this best understood looking @ fiddle, general thought want accomplish smooth scroll, various 'snapping' attraction points.

the user scroll mouse/trackpad creating delta value, these added give target value, here i'm easing toward value (this smooth scroll people clicking mouse wheels). 1 time scroll has started want snap various points. i've calculated attraction forcefulness toward these points, when incorporated easing above doesn't work desired.

class="snippet-code-js lang-js prettyprint-override">var ael = document.getelementbyid('a'); var bel = document.getelementbyid('b'); sketch.create({ setup: function() { this.range = { min: 0, max: 10000 }; this.easing = 0.1; this.current = { p: 0, t: 0, f: 0 }; this.attractors = []; this.attractionradius = (this.range.max / 5); (var = 0, x = 0; x = * (this.range.max / 4), < 5; i++) { this.attractors.push(x); } window.addeventlistener('mousewheel', this.onmouse.bind(this)); }, draw: function() { var w = (this.current.p / this.range.max) * this.width; this.fillstyle = 'black'; this.fillrect(0, 0, w, this.height); this.attractors.foreach(function(attractor) { var x = (attractor / this.range.max) * this.width; this.beginpath(); this.arc(x, this.height, 10, 0, math.pi * 2, false); this.fillstyle = 'red'; this.fill(); }.bind(this)); }, update: function() { var _this = this; this.attractors.foreach(function(attractor, index) { var delta = attractor - _this.current.p; var absdelta = math.abs(delta); if (absdelta < _this.attractionradius) { // calculate forcefulness toward each attraction point. _this.current.f += delta * (1 - absdelta / _this.attractionradius); } }); console.log(this.current, (this.current.t - this.current.p) * this.easing); // add together easing our forces, think error lies? this.current.f += (this.current.t - this.current.p) * this.easing; this.current.p += this.current.f; //reset forces. this.current.f = 0; }, // set target users scroll position, limited range set above. onmouse: function(event) { var delta = event.wheeldeltay; this.current.t += delta; this.current.t = this.current.t < this.range.min ? this.range.min : this.current.t > this.range.max ? this.range.max : this.current.t; event.preventdefault(); } }); class="snippet-code-css lang-css prettyprint-override">* { margin: 0; padding: 0; } class="snippet-code-html lang-html prettyprint-override"><script src="https://rawgit.com/soulwire/sketch.js/master/js/sketch.min.js"></script>

javascript animation physics easing

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -