Skip to content

Instantly share code, notes, and snippets.

@hushin
Created November 23, 2016 01:05
Show Gist options
  • Save hushin/03a532172f16cbbf6ce2b36aa862efff to your computer and use it in GitHub Desktop.
Save hushin/03a532172f16cbbf6ce2b36aa862efff to your computer and use it in GitHub Desktop.
https://github.com/philc/vimium/blob/master/content_scripts/scroller.coffee を参考に _smooziee.js をもうちょっとスムーズにスクロールするようにした
//
// _smooziee.js
//
// LICENSE: {{{
// Copyright (c) 2009 snaka<snaka.gml@gmail.com>
//
// distributable under the terms of an MIT-style license.
// http://www.opensource.jp/licenses/mit-license.html
// }}}
//
// modified hushin nnnnot+github@gmail.com
//
// INFO: {{{
var INFO = xml`
<plugin name="smooziee" version="0.10.3"
href="https://github.com/vimpr/vimperator-plugins/raw/master/_smooziee.js"
summary="j,kキーでのスクロールをスムースに"
lang="en_US"
xmlns="http://vimperator.org/namespaces/liberator">
<author email="snaka.gml@gmail.com" homepage="http://vimperator.g.hatena.ne.jp/snaka72/">snaka</author>
<project name="Vimperator" minVersion="3.6"/>
<license>MIT style license</license>
<p>j,k key scrolling to be smoothly.</p>
<h3 tag="smooziee_global_variables">Global vriables</h3>
<p>You can configure following variable as you like.</p>
<dl>
<dt>smooziee_scroll_amount</dt><dd>Scrolling amount(unit:px). Default value is 400px.</dd>
</dl>
<h3 tag="smooziee_example">Example</h3>
<p>Set scroll amount is 300px.</p>
<code><ex><![CDATA[
let g:smooziee_scroll_amount="300"
]]></ex></code>
<h3 tag="smooziee_API">API</h3>
<code>smooziee.smoothScrollBy(amount);</code>
<p>Example</p>
<code><ex><![CDATA[
:js liberator.plugins.smooziee.smoothScrollBy(600)
:js liberator.plugins.smooziee.smoothScrollBy(-600)
]]></ex></code>
</plugin>
<plugin name="smooziee" version="0.10.2"
href="https://github.com/vimpr/vimperator-plugins/raw/master/_smooziee.js"
summary="j,kキーでのスクロールをスムースに"
lang="ja"
xmlns="http://vimperator.org/namespaces/liberator">
<author email="snaka.gml@gmail.com" homepage="http://vimperator.g.hatena.ne.jp/snaka72/">snaka</author>
<project name="Vimperator" minVersion="3.6"/>
<license>MIT style license</license>
<p>普段のj,kキーのスクロールをLDRizeライクにスムースにします。</p>
<h3 tag="smooziee_global_variables">グローバル変数</h3>
<p>以下の変数を.vimperatorrcなどで設定することで動作を調整することができます。</p>
<dl>
<dt>smooziee_scroll_amount</dt>
<dd>1回にスクロールする幅です(単位:ピクセル)。デフォルトは"400"です。</dd>
</dl>
<h3 tag="smooziee_example">設定例</h3>
<p>スクロール量を300pxに設定します。</p>
<code><ex><![CDATA[
let g:smooziee_scroll_amount="300"
]]></ex></code>
<h3 tag="smooziee_API">API</h3>
<p>他のキーにマップする場合やスクリプトから呼び出せるようAPIを用意してます。</p>
<code>smooziee.smoothScrollBy(amount);</code>
<p>Example</p>
<code><ex><![CDATA[
:js liberator.plugins.smooziee.smoothScrollBy(600)
:js liberator.plugins.smooziee.smoothScrollBy(-600)
]]></ex></code>
<h3 tag="soomziee_ToDo">ToDo</h3>
<ul>
<li>読み込みの順番によっては他のプラグインと競合する可能性があるのをなんとかしたい。</li>
</ul>
</plugin>`;
// }}}
liberator.plugins.smooziee = (function(){
// Mappings {{{
mappings.addUserMap(
[modes.NORMAL],
["j"],
"Smooth scroll down",
function(count){
liberator.plugins.smooziee.smoothScrollBy(getScrollAmount() * (count || 1));
},
{
count: true
}
);
mappings.addUserMap(
[modes.NORMAL],
["k"],
"Smooth scroll up",
function(count){
liberator.plugins.smooziee.smoothScrollBy(getScrollAmount() * -(count || 1));
},
{
count: true
}
);
// }}}
var requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame;
var cancelAnimationFrame = window.cancelAnimationFrame || window.mozCancelAnimationFrame;
// PUBLIC {{{
var PUBLICS = {
smoothScrollBy: function(amount) {
win = findScrollableWindow();
destY = win.scrollY + amount;
cancelAnimationFrame(next);
smoothScroll(amount);
}
}
// }}}
// PRIVATE {{{
var next;
var destY;
var win;
var minCalibration = 0.5;
var maxCalibration = 1.6;
var calibrationBoundary = 150;
function getScrollAmount() window.eval(liberator.globalVariables.smooziee_scroll_amount || '400');
function getSign(val) {
return val ? (val < 0 ? -1 : 1) : 0;
}
// https://github.com/philc/vimium/blob/master/content_scripts/scroller.coffee を参考に
function smoothScroll(amount) {
// liberator.echomsg("smoothScroll" + amount)
var sign = getSign(amount);
amount = Math.abs(amount);
var duration = Math.max(100, 20 * Math.log(amount));
var totalDelta = 0;
var totalElapsed = 0.0;
var calibration = 1.0;
var previousTimestamp = null;
function animate(timestamp) {
previousTimestamp = previousTimestamp || timestamp;
if (timestamp === previousTimestamp) {
requestAnimationFrame(animate)
}
var elapsed = timestamp - previousTimestamp;
totalElapsed += elapsed;
previousTimestamp = timestamp;
if (75 <= totalElapsed &&
minCalibration <= calibration &&
calibration <= maxCalibration) {
if (1.05 * calibration * amount < calibrationBoundary) calibration *= 1.05
if (calibrationBoundary < 0.95 * calibration * amount) calibration *= 0.95
}
var delta = Math.ceil(amount * (elapsed / duration) * calibration)
delta = Math.max(0, Math.min(delta, amount - totalDelta))
// liberator.echomsg(sign + ", " + delta + ", " + totalDelta + ","+ calibration)
if (delta) {
totalDelta += delta;
win.scrollBy(0, sign * delta);
next = requestAnimationFrame(animate)
}
}
requestAnimationFrame(animate)
}
function makeScrollTo(x, y) function() win.scrollTo(x, y);
function findScrollableWindow() {
var win = this.focusedWindow;
if (win && (win.scrollMaxX > 0 || win.scrollMaxY > 0)) return win;
win = config.browser.contentWindow;
if (win.scrollMaxX > 0 || win.scrollMaxY > 0) return win;
for (let frame in util.Array.itervalues(win.frames)) {
if (frame.scrollMaxX > 0 || frame.scrollMaxY > 0) return frame;
}
return win;
}
// }}}
return PUBLICS;
})();
// vim: sw=2 ts=2 et si fdm=marker:
@hushin
Copy link
Author

hushin commented Nov 23, 2016

https://github.com/hushin/dotfiles/blob/master/.vimperatorrc

こういう感じに半分スクロールもできる

" _smooziee を使いつつ dとuでスクロール
noremap <C-d> :js liberator.plugins.smooziee.smoothScrollBy(innerHeight / 2)<CR>
noremap <C-u> :js liberator.plugins.smooziee.smoothScrollBy(innerHeight / -2)<CR>
noremap d :js liberator.plugins.smooziee.smoothScrollBy(innerHeight / 2)<CR>
noremap u :js liberator.plugins.smooziee.smoothScrollBy(innerHeight / -2)<CR>

@hushin
Copy link
Author

hushin commented Nov 23, 2016

gitbookなどスクロールできないページがあるので、findScrollableWindowのあたりもなおしたい
https://developer.gitbook.com/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment