-
-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathimages.js
113 lines (104 loc) · 5 KB
/
images.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
var scenegraph = require("scenegraph");
var fs = require("uxp").storage.localFileSystem;
var sm = require("./selectionManagement.js"),
fm = require("./fileManagement.js");
// Generate a random int from the interval [min, max]
function getRandomIntInARange(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
// Choose a random photo and set it as fill of a shape
function generateImageBitmapFill(photos, shape) {
var imageNumber = getRandomIntInARange(0, photos.length - 1),
bitmap = new scenegraph.BitmapFill.create();
bitmap.loadFromURL(photos[imageNumber].nativePath);
bitmap = bitmap.cloneWithOverrides({
scaleBehavior: scenegraph.BitmapFill.SCALE_COVER,
scale: (bitmap.scaleX + bitmap.scaleY) / 2
});
if (shape)
shape.fill = bitmap;
else {
var image = new scenegraph.Rectangle();
image.width = bitmap.width / 15;
image.height = bitmap.height / 15;
image.fill = bitmap;
return image;
}
}
function fillShapes(selection, landscapePhotos, portraitPhotos) {
for (var i = 0; i < selection.items.length; i++) {
if (selection.items[i] instanceof scenegraph.Rectangle || selection.items[i] instanceof scenegraph.Path) {
if (selection.items[i].height > selection.items[i].width) {
generateImageBitmapFill(portraitPhotos, selection.items[i]);
} else if (selection.items[i].height < selection.items[i].width) {
generateImageBitmapFill(landscapePhotos, selection.items[i]);
} else {
var photos = portraitPhotos.concat(landscapePhotos);
generateImageBitmapFill(photos, selection.items[i]);
}
} else if (selection.items[i] instanceof scenegraph.Ellipse) {
if (selection.items[i].radiusX < selection.items[i].radiusY) {
generateImageBitmapFill(portraitPhotos, selection.items[i]);
} else if (selection.items[i].radiusX > selection.items[i].radiusY) {
generateImageBitmapFill(landscapePhotos, selection.items[i]);
} else {
var photos = portraitPhotos.concat(landscapePhotos);
generateImageBitmapFill(photos, selection.items[i]);
}
} else {
var photos = portraitPhotos.concat(landscapePhotos);
var image = generateImageBitmapFill(photos);
selection.insertionParent.addChild(image);
image.placeInParentCoordinates(image.globalBounds, selection.items[i].globalBounds);
}
}
}
// Generate RANDOM IMAGES from Unsplash
async function unsplash(selection) {
var pluginFolder = await fs.getPluginFolder(),
pluginEntries = await pluginFolder.getEntries(),
unsplashEntries = await fm.getFilesFromSpecificFolder(pluginEntries, 'unsplash'),
landscapePhotos = sm.getImages(await fm.getFilesFromSpecificFolder(unsplashEntries, 'landscape')),
portraitPhotos = sm.getImages(await fm.getFilesFromSpecificFolder(unsplashEntries, 'portrait'));
fillShapes(selection, landscapePhotos, portraitPhotos);
}
// Generate PROFILE IMAGES (source is also Unsplash)
async function avatar(selection, gender) {
var pluginFolder = await fs.getPluginFolder(),
pluginEntries = await pluginFolder.getEntries(),
avatarEntries = await fm.getFilesFromSpecificFolder(pluginEntries, 'avatars'),
landscapePhotos, portraitPhotos;
if (gender == 'mixed') {
var ladiesEntries = await fm.getFilesFromSpecificFolder(avatarEntries, 'ladies'),
landscapePhotosLadies = sm.getImages(await fm.getFilesFromSpecificFolder(ladiesEntries, 'landscape')),
portraitPhotosLadies = sm.getImages(await fm.getFilesFromSpecificFolder(ladiesEntries, 'portrait')),
gentlemansEntries = await fm.getFilesFromSpecificFolder(avatarEntries, 'gentlemans'),
landscapePhotosGentlemans = sm.getImages(await fm.getFilesFromSpecificFolder(gentlemansEntries, 'landscape')),
portraitPhotosGentlemans = sm.getImages(await fm.getFilesFromSpecificFolder(ladiesEntries, 'portrait'));
landscapePhotos = landscapePhotosGentlemans.concat(landscapePhotosLadies);
portraitPhotos = portraitPhotosGentlemans.concat(portraitPhotosLadies);
} else {
var genderEntries = await fm.getFilesFromSpecificFolder(avatarEntries, gender);
landscapePhotos = sm.getImages(await fm.getFilesFromSpecificFolder(genderEntries, 'landscape'));
portraitPhotos = sm.getImages(await fm.getFilesFromSpecificFolder(genderEntries, 'portrait'));
}
fillShapes(selection, landscapePhotos, portraitPhotos);
}
// MALE PROFILE IMAGES
async function maleImages(selection) {
await avatar(selection, 'gentlemans');
}
// FEMALE PROFILE IMAGES
async function femaleImages(selection) {
await avatar(selection, 'ladies');
}
// MIXED PROFILE IMAGES
async function mixedImages(selection) {
await avatar(selection, 'mixed');
}
module.exports = {
unsplash,
maleImages,
femaleImages,
mixedImages
}