# import Scroller from './scroller'
$id = (id) ->
document.getElementById id
$class = (className) ->
document.getElementsByClassName className
$tag = (tag) ->
document.getElementsByTagName tag
$query = (query) ->
document.querySelector query
$queryAll = (query) ->
document.querySelectorAll query
$create = (node) ->
document.createElement node
ajax = (method, url, data, callback, error=null, type=null) ->
xhttp = new XMLHttpRequest()
xhttp.onreadystatechange = (e) ->
if e.target.readyState is 4
if e.target.status is 200
callback(e.target.response)
else
if error
error(e.target)
# # Timeout
# setTimeout ->
# if e.target.readyState isnt 4
# do xhttp.abort
# # error(e.target)
# return
#
# , 10000
return
xhttp.open method, url, true
if type
xhttp.responseType = type
xhttp.send data
return
capitalize = (str) ->
str.charAt(0).toUpperCase() + str.slice(1)
timeAgo = (bump) ->
#- Get current time
now = (new Date).getTime()
#- Get difference in seconds
difference = (now - bump) / 1000
#- Time helper variables
year = 60 * 60 * 24 * 365
month = 60 * 60 * 24 * 30
week = 60 * 60 * 24 * 7
day = 60 * 60 * 24
hour = 60 * 60
minute = 60
#- In years
if difference >= year
total = Math.floor difference / year
unit = 'year'
#- In months
else if difference >= month
total = Math.floor difference / month
unit = 'month'
#- In weeks
else if difference >= week
total = Math.floor difference / week
unit = 'week'
#- In days
else if difference >= day
total = Math.floor difference / day
unit = 'day'
#- In hours
else if difference >= hour
total = Math.floor difference / hour
unit = 'hour'
#- In minutes
else if difference >= minute
total = Math.floor difference / minute
unit = 'minute'
#- In seconds
else
total = Math.floor difference
unit = 'second'
#- Make plural if more than one unit
if total > 1
unit += 's'
"#{total} #{unit}"
hexToRgb = (hex) ->
if hex.startsWith '#'
hex = hex.substring 1
bigint = parseInt hex, 16
r = (bigint >> 16) & 255
g = (bigint >> 8) & 255
b = bigint & 255
return {r, g, b}
analyzeAudio = (player, fft) ->
sum = 0
player.analyzer.getByteFrequencyData(player.dataArray);
for i in [0...player.bufferLength]
sum += player.dataArray[i]
# Average frequency
sum / fft
angle2rad = (angle) ->
angle*Math.PI/180
drawRoundedPolygon = (ctx, points, roundness) ->
# Points as vector
asVector = (p, pp, v) ->
v.x = pp.x - p.x
v.y = pp.y - p.y
v.len = Math.sqrt v.x * v.x + v.y * v.y
v.nx = v.x / v.len
v.ny = v.y / v.len
v.ang = Math.atan2 v.ny, v.nx
return
radius = roundness
v1 = {}
v2 = {}
len = points.length
p1 = points[len - 1]
i = 0
while i < len
p2 = points[i % len]
p3 = points[(i + 1) % len]
asVector p2, p1, v1
asVector p2, p3, v2
sinA = v1.nx * v2.ny - (v1.ny * v2.nx)
sinA90 = v1.nx * v2.nx - (v1.ny * -v2.ny)
angle = Math.asin(sinA)
radDirection = 1
drawDirection = false
if sinA90 < 0
if angle < 0
angle = Math.PI + angle
else
angle = Math.PI - angle
radDirection = -1
drawDirection = true
else
if angle > 0
radDirection = -1
drawDirection = true
if p2.radius != undefined
radius = p2.radius
else
radius = roundness
halfAngle = angle / 2
lenOut = Math.abs Math.cos(halfAngle) * radius / Math.sin(halfAngle)
if lenOut > Math.min v1.len / 2, v2.len / 2
lenOut = Math.min v1.len / 2, v2.len / 2
cRadius = Math.abs lenOut * Math.sin(halfAngle) / Math.cos(halfAngle)
else
cRadius = radius
x = p2.x + v2.nx * lenOut
y = p2.y + v2.ny * lenOut
x += -v2.ny * cRadius * radDirection
y += v2.nx * cRadius * radDirection
ctx.arc x, y, cRadius, v1.ang + Math.PI / 2 * radDirection, v2.ang - (Math.PI / 2 * radDirection), drawDirection
p1 = p2
p2 = p3
i++
ctx.closePath()
return
easeInOutQuad = (time, start, change, duration) ->
time /= duration / 2
if time < 1
return change / 2 * time * time + start
time--
-change / 2 * (time * (time - 2) - 1) + start
# Need to use module.exports because pug doesn't have es6 import
module.exports = {
$id
$class
$tag
$query
$queryAll
$create
ajax
capitalize
timeAgo
hexToRgb
analyzeAudio
angle2rad
drawRoundedPolygon
easeInOutQuad
}