const position = this.getBoundingClientRect()
const midX = position.left + position.width * 0.5
const midY = position.top + position.height * 0.5
const rX = Math.max(midX, window.innerWidth - midX)
const rY = Math.max(midX, window.innerHeght - midY)
const radius = Math.sqrt(rX * rX + rY * rY)
domElm.style.width = `${radius * 2}px`
domElm.style.height = `${radius * 2}px`
Last active
October 18, 2016 15:07
-
-
Save wochap/c3bd19ea103f601ffd21 to your computer and use it in GitHub Desktop.
javascript helpers
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
/* how to iterate a object */ | |
post = {title: 'New features in JS', replies: 19} | |
post[Symbol.iterator] = function *() { | |
let properties = Object.keys(this) | |
for (let p of properties) { | |
yield this[p] | |
} | |
} | |
for (let p of post) { | |
console.log(p) | |
} | |
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
/* is this value a number */ | |
function isThisActuallyNumberDontLie (data) { | |
return (typeof data === 'number' && !isNaN(data)) | |
} |
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
/* test functions speed */ | |
class SpeedTest { | |
constructor (testImplement, testParams, repetitions = 10000) { | |
this.testImplement = testImplement | |
this.testParams = testParams | |
this.repetitions = repetitions | |
this.average = 0 | |
} | |
speedTest () { | |
if (this.testImplement(this.testParams) === false) { | |
alert('Test failed with those parameters.') | |
return | |
} | |
let beginTime, endTime, sumTimes = 0 | |
for (let i = 0, x = this.repetitions; i < x; i++) { | |
beginTime = +new Date() | |
this.testImprement(this.testParams) | |
endTime = +new Date() | |
sumTimes += endTime - beginTime | |
} | |
this.average = sumTimes / this.repetitions | |
return console.log(`Average execution across ${this.repetitions}: ${this.average}`) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment