-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
99 lines (77 loc) · 2.42 KB
/
index.js
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
// ScriptUI example with animated images in the interface
// By Tomas Šinkūnas, www.rendertom.com
(function(thisObj) {
var win = (thisObj instanceof Panel) ? thisObj : new Window('palette', 'ScriptUI mouseOver animation test', undefined, {
resizeable: true
});
win.margins = 0;
win.orientation = 'row';
win.spacing = 0;
var roll = addAnimatedImage(win, {
mouseoutImages: 'images/roll/mouseout/',
mouseoverImages: 'images/roll/mouseover/',
});
var hand = addAnimatedImage(win, {
mouseoutImages: 'images/hand/mouseout/',
mouseoverImages: 'images/hand/mouseover/',
});
var tractor = addAnimatedImage(win, {
mouseoutImages: 'images/tractor/mouseout/',
mouseoverImages: 'images/tractor/mouseover/',
});
win.onResizing = win.onResize = function() {
this.layout.resize();
};
if (win instanceof Window) {
win.center();
win.show();
} else {
win.layout.layout(true);
win.layout.resize();
}
function addAnimatedImage(parentGroup, options) {
var element = parentGroup.add('image');
element.mouseoutImages = getImages(options.mouseoutImages);
element.mouseoverImages = getImages(options.mouseoverImages);
element.imageIndex = 0;
element.numFrames = element.mouseoverImages.length;
element.addEventListener('mouseout', stopAnimation);
element.addEventListener('mouseover', startAnimation);
setMouseoutImage(element);
return element;
function getImages(folder) {
folder = (folder instanceof Folder) ? folder : new Folder(folder);
var files = folder.getFiles(function(file) {
return file.fsName.split('.').pop() === 'png';
});
return files.sort(function(a, b) {
return a.fsName.toLowerCase() > b.fsName.toLowerCase();
});
}
function incrementImageIndex(element) {
element.imageIndex = (element.imageIndex + 1) % element.numFrames;
}
function setMouseoutImage(element) {
var image = element.mouseoutImages[element.imageIndex];
element.icon = ScriptUI.newImage(image);
}
function setMouseoverImage(element) {
var image = element.mouseoverImages[element.imageIndex];
element.icon = ScriptUI.newImage(image);
}
function startAnimation() {
var self = this;
incrementImageIndex(self);
setMouseoverImage(self);
self.timer = app.setTimeout(function() {
startAnimation.call(self);
}, 1000 / 60);
}
function stopAnimation() {
if (this.timer) {
this.timer = app.cancelTimeout(this.timer);
setMouseoutImage(this);
}
}
}
})(this);