-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenswap.coffee
48 lines (43 loc) · 1.41 KB
/
tokenswap.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
swapDuration = 500
swapDelay = 100
###
initCircles = ->
## Remember original location of all circles
for elt in document.querySelectorAll 'circle'
continue if elt.hasAttribute 'data-cx'
elt.setAttribute 'data-cx', elt.getAttribute 'cx'
elt.setAttribute 'data-cy', elt.getAttribute 'cy'
###
timeline = null
animateSwaps = (swaps, reverse) ->
#initCircles()
swaps = swaps.split /\s+/
swaps.reverse() if reverse
centers = {}
timeline?.finish()
timeline = new SVG.Timeline
for swap, count in swaps
continue unless swap
[i, j] = swap.split ','
i = parseInt i
j = parseInt j
if isNaN(i) or isNaN(j)
console.warn "Invalid swap #{swap}"
continue
ti = SVG "section.present circle.t#{i}"
tj = SVG "section.present circle.t#{j}"
continue unless ti? and tj?
ti.timeline timeline
tj.timeline timeline
centers[i] ?= [ti.cx(), ti.cy()]
centers[j] ?= [tj.cx(), tj.cy()]
## Swap
[centers[i], centers[j]] = [centers[j], centers[i]]
ti.animate(swapDuration, swapDelay, 'after').center centers[i]...
tj.animate(swapDuration, -swapDuration, 'after').center centers[j]...
Reveal.on 'fragmentshown', (e) ->
return unless e.fragment.classList.contains 'tokenswap'
animateSwaps e.fragment.dataset.swaps, false
Reveal.on 'fragmenthidden', (e) ->
return unless e.fragment.classList.contains 'tokenswap'
animateSwaps e.fragment.dataset.swaps, true