UNPKG

d3-inertia

Version:
3 lines (2 loc) 2.74 kB
// https://github.com/Fil/d3-inertia v0.4.0 Copyright 2021 Philippe Riviere !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("d3-selection"),require("d3-drag"),require("d3-timer"),require("versor")):"function"==typeof define&&define.amd?define(["exports","d3-selection","d3-drag","d3-timer","versor"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).d3=t.d3||{},t.d3,t.d3,t.d3,t.versor)}(this,(function(t,e,i,r,n){"use strict";function o(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var a=o(n);function s(t){var e,i,r,n,o,s,l=t.projection,f=d({start:function(){e=a.default.cartesian(l.invert(f.position)),i=l.rotate(),r=a.default(i),t.start&&t.start()},move:function(){var n=l.rotate(i).invert(f.position);if(!isNaN(n[0])){var o=a.default.cartesian(n),s=a.default.multiply(r,a.default.delta(e,o)),d=a.default.rotation(s);t.render(d),t.move&&t.move()}},end:function(){n=a.default.cartesian(l.invert(f.position.map((function(t,e){return t-f.velocity[e]/1e3})))),s=a.default(l.rotate()),o=a.default.cartesian(l.invert(f.position)),t.end&&t.end()},stop:t.stop,finish:t.finish,render:function(e){var i=a.default.rotation(a.default.multiply(s,a.default.delta(n,o,1e3*e)));t.render&&t.render(i)},time:t.time});return f}function d(t){var e=t.time||5e3,i=1.0001,n=-Math.log(1-1/i),o={start:function(e){var i=[e.x,e.y];o.position=i,o.velocity=[0,0],o.timer.stop(),this.classList.remove("inertia"),this.classList.add("dragging"),t.start&&t.start.call(this,i)},move:function(e){var i=[e.x,e.y],r=performance.now(),n=r-o.time,a=1-Math.exp(-n/1e3);o.velocity=o.velocity.map((function(t,e){var n=i[e]-o.position[e],s=r-o.time;return 1e3*(1-a)*n/s+t*a})),o.time=r,o.position=i,t.move&&t.move.call(this,i)},end:function(){this.classList.remove("dragging","inertia");var r=o.velocity;if(r[0]*r[0]+r[1]*r[1]<100)return o.timer.stop(),t.stop&&t.stop();var a=performance.now()-o.time;if(null==t.hold&&(t.hold=100),a>=t.hold)return o.timer.stop(),t.stop&&t.stop();this.classList.add("inertia"),t.end&&t.end();var s=this;o.timer.restart((function(r){o.t=i*(1-Math.exp(-n*r/e)),t.render&&t.render(o.t),o.t>1&&(o.timer.stop(),s.classList.remove("inertia"),o.velocity=[0,0],o.t=1,t.finish&&t.finish())}))},position:[0,0],velocity:[0,0],timer:r.timer((function(){})),time:0};return o.timer.stop(),o}t.geoInertiaDrag=function(t,r,n,o){o||(o={}),t.node&&(t=t.node()),t=e.select(t);var a=s({projection:n,render:function(t){n.rotate(t),r&&r()},start:o.start,move:o.move,end:o.end,stop:o.stop,finish:o.finish,time:o.time,hold:o.hold});return t.call(i.drag().on("start",a.start).on("drag",a.move).on("end",a.end)),a},t.geoInertiaDragHelper=s,t.inertiaHelper=d,Object.defineProperty(t,"__esModule",{value:!0})}));