Last active
July 8, 2017 05:37
-
-
Save crakaC/f73ec0974d50a89f35ca39514c671c95 to your computer and use it in GitHub Desktop.
friends.nicoのニコるくんを回します。ブラウザでF12を押して、コンソールにコピペしてみてください。計測してないですが、前回のやつより軽いと思います。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ニコるくんを探すためのクエリ | |
const QUERY = '.status img[alt*="nicoru"]' | |
const ROLL_PER_SEC = 1.75 | |
// 角度指定用のstringを予め確保 | |
const LUT = Array.from(Array(360).keys()).map(e => "rotate(" + e + "deg)") | |
var nicorus = [] | |
class Nicorukun { | |
constructor(orig, rps){ | |
this.element = orig | |
this.rps = rps | |
this.deg = this.getRotation() | |
} | |
getRotation(){ | |
var cssText = this.element.style.cssText | |
var pattern = /transform: rotate\((\d+)deg\)/ | |
return parseInt(cssText.match(pattern)[1]) | |
} | |
update(dt){ | |
this.deg = (this.deg + this.rps * 360 * dt * 0.001) % 360 | |
this.element.style.transform = LUT[Math.floor(this.deg)] | |
} | |
} | |
function nicoRolling(){ | |
var lastUpdated = Date.now() | |
function rollNicorus(){ | |
var now = Date.now() | |
var dt = now - lastUpdated | |
nicorus.forEach(e => e.update(dt)) | |
lastUpdated = now; | |
setTimeout(rollNicorus, 33) | |
} | |
setTimeout(rollNicorus, 33) | |
} | |
// TLにいるニコるくんを見つけて回す | |
document.querySelectorAll(QUERY).forEach((e)=>{ | |
nicorus.push(new Nicorukun(e, ROLL_PER_SEC)) | |
}) | |
// 新しく流れてきたニコるくんを回す | |
var observer = new MutationObserver((mutations) => { | |
mutations.forEach((m) => { | |
m.addedNodes.forEach((node) => { | |
node.querySelectorAll(QUERY).forEach((e)=>{ | |
nicorus.push(new Nicorukun(e, ROLL_PER_SEC)) | |
}) | |
}); | |
}) | |
}); | |
document.querySelectorAll(".scrollable>div").forEach( | |
function(target){ | |
observer.observe(target, {childList: true}); | |
} | |
) | |
nicoRolling(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
気が向いたら拡張機能にする