-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.js
53 lines (43 loc) · 1.35 KB
/
code.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
// This file will communicate with Figma's API and your UI
// Main function to count pixels
function countPixels() {
let totalPixels = 0;
let frameCount = 0;
const topLevelNodes = figma.currentPage.children;
topLevelNodes.forEach(node => {
if (node.type === "FRAME" || node.type === "SECTION") {
let nodeArea = node.width * node.height;
totalPixels += nodeArea;
frameCount++;
}
});
console.log(`Total nodes (frames/sections) counted: ${frameCount}`);
console.log(`Total area covered by frames/sections: ${totalPixels}`);
updateUI(frameCount, totalPixels);
}
function updateUI(frameCount, totalPixels) {
if (figma.ui) {
figma.ui.postMessage({
type: 'update-counts',
frameCount: frameCount,
totalPixels: totalPixels
});
} else {
console.error("UI not created yet. Unable to send message.");
}
}
figma.showUI(__html__, { width: 340, height: 558 });
// Set up an event listener for changes in the document
figma.on('documentchange', () => {
countPixels();
});
// Initial call to count pixels and sections
countPixels();
// Listen for when the UI is ready
figma.ui.onmessage = msg => {
if (msg.type === 'calculate-pixels') {
countPixels();
}
};
// Initial call or setup instructions go here
countPixels();