-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathLottieLayer.coffee
132 lines (109 loc) · 3.28 KB
/
LottieLayer.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
###
LottieLayer
-
Implementation of Hernan Torrisi & AirBnb "Lottie-Web" for Framer.
by @72mena
###
# INCLUDE LIBRARY ———————————————————————————
insertScript = (localScript, webScript, name = 'JavaScript Library') ->
try
lib = Utils.domLoadDataSync localScript
console.log "%c#{name} Successfully Included Locally", "background: #DDFFE3; color: #007814"
catch e
try
lib = Utils.domLoadDataSync webScript
console.log "%c#{name} Successfully Included from Web", "background: #DDFFE3; color: #007814"
catch e
throw Error("Sorry, I couldn't load #{name}")
script = document.createElement "script"
script.type = "text/javascript"
script.innerHTML = lib
head = document.getElementsByTagName("head")[0]
head.appendChild script
script
insertScript("modules/lottie.min.js", "https://raw.githubusercontent.com/airbnb/lottie-web/master/build/player/lottie.min.js", "lottie-web")
# LOTTIE LAYER ———————————————————————————
class exports.LottieLayer extends Layer
@define "speed",
get: -> @_properties["speed"]
set: (value) ->
@_properties["speed"] = value
@setSpeed(value) if @built
@emit "change:speed"
@define "direction",
get: -> @_properties["direction"]
set: (value) ->
@_properties["direction"] = value
@setDirection(value) if @built
@emit "change:direction"
@define "path",
get: -> @_properties["path"]
set: (value) ->
@_properties["path"] = value
@setSettings() if @built
@emit "change:path"
constructor: (@options={}) ->
# Defaults
@options.backgroundColor ?= null
@options.path ?= null
@options.autoplay ?= true
@options.loop ?= true
@options.speed ?= 1
@options.direction ?= 1
@options.renderer ?= "svg"
super @options
if @options.path is null
print "From LottieLayer: Setting a path to your json file is required."
if @name is ""
print "From LottieLayer: The 'name' attribute is required."
#Shortcuts
@autoplay = @options.autoplay
@loop = @options.loop
@renderer = @options.renderer
#Vars
@built = false
@_animationLayer = null
#Run
@build()
build: () ->
@html = '<div id='+"#{@name}"+'></div>'
@setSettings()
@built = true
setSettings: () ->
_container = document.getElementById(@name)
_container.innerHTML = ""
lottieSettings =
container: _container,
path: @path,
renderer: @renderer,
autoplay: @autoplay,
loop: @loop
@_animationLayer = lottie.loadAnimation(lottieSettings);
@setSpeed()
@setDirection()
play: () ->
@_animationLayer.play()
stop: () ->
@_animationLayer.stop()
pause: () ->
@_animationLayer.pause()
goToAndPlay: (value, isFrame) ->
isFrame ?= true
@_animationLayer.goToAndPlay(value, isFrame)
goToAndStop: (value, isFrame) ->
isFrame ?= true
@_animationLayer.goToAndStop(value, isFrame)
playSegments: (segments, forceFlag) ->
forceFlag ?= true
@_animationLayer.playSegments(segments, forceFlag)
setSpeed: (speed) ->
speed ?= @speed
@_animationLayer.setSpeed(speed)
setDirection: (direction) ->
direction ?= @direction
@_animationLayer.setDirection(direction)
onComplete: (callback) ->
if @loop
@_animationLayer.addEventListener "loopComplete", callback
else
@_animationLayer.addEventListener "complete", callback