-
-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathinsertImage.js
46 lines (39 loc) · 1.32 KB
/
insertImage.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
const scenegraph = require("scenegraph");
const getUrlAsBase64 = require('./getUrlAsBase64.js');
const reportError = require('./reportError');
// file: {
// format,
// url,
// name
// }
const insertImage = async (selection, file) => {
// Fetch Image
try {
const base64 = await getUrlAsBase64(file.url);
// Create ImageFill for this image
const ImageFill = scenegraph.ImageFill;
var imageFill = new ImageFill(base64);
// Insert Image into Xd App
var newShape = new scenegraph.Rectangle();
newShape.fill = imageFill;
if (selection.insertionParent instanceof scenegraph.Artboard) {
// If Artboard is selected
const ratio = imageFill.naturalWidth / imageFill.naturalHeight;
newShape.width = selection.insertionParent.width
newShape.height = selection.insertionParent.width / ratio;
// Horizontal
if (newShape.height > selection.insertionParent.height) {
newShape.height = selection.insertionParent.height;
newShape.width = selection.insertionParent.height * ratio;
}
} else {
newShape.width = imageFill.naturalWidth;
newShape.height = imageFill.naturalHeight;
}
selection.insertionParent.addChild(newShape);
} catch (e) {
await reportError(file.url);
console.error(e.stack);
}
}
module.exports = insertImage