Skip to content

Commit

Permalink
fix positions
Browse files Browse the repository at this point in the history
  • Loading branch information
realvjy committed May 29, 2022
1 parent 6a03bfb commit 1ba7557
Show file tree
Hide file tree
Showing 5 changed files with 614 additions and 656 deletions.
44 changes: 10 additions & 34 deletions src/code.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// This plugin will add 3dicons from 3dicons.co to your artboard


// Show the plugin UI
figma.showUI(__html__, {
width: 300,
height: 562,
height: 600,
themeColors: true
}
);
Expand All @@ -13,32 +12,26 @@ function clone(val) {
return JSON.parse(JSON.stringify(val))
}

console.log('3dicons running...');

// Message received
figma.ui.onmessage = (msg) => {
console.log("got this from the UI", msg)
console.log("checking message type ", msg.type);

if (msg.type === 'window-resize') {
figma.ui.resize(msg.data.width, msg.data.height);
return;
}
// if (msg.type === 'notify') {
if (msg.type === 'notify') {
figma.notify(msg.data.message);
return;
}

if (msg.type === 'set-bg') {
const newBytes: Uint8Array = msg.data.newBytes;
const dropPosition = msg.data.dropPosition;
const windowSize = msg.data.windowSize;
let node = figma.currentPage.selection[0];
if (!node || dropPosition) {
if (!node) {
node = figma.createRectangle();
node.resize(800, 800)
node.x = Math.round(figma.viewport.center.x - node.width / 2);
node.y = Math.round(figma.viewport.center.y - node.height / 2);
}
figma.notify('Added to canvas');


const newFills = []
//@ts-ignore
for (const paint of node.fills) {
Expand Down Expand Up @@ -68,29 +61,12 @@ figma.ui.onmessage = (msg) => {
}
//@ts-ignore
node.fills = newFills

if (dropPosition) {
console.log(dropPosition, windowSize)
const bounds = figma.viewport.bounds;
const zoom = figma.viewport.zoom;

// Math.round is used here because sometimes it may return a floating point number very close but not exactly the window width.
const hasUI = Math.round(bounds.width * zoom) !== windowSize.width;

const leftPaneWidth = windowSize.width - bounds.width * zoom - 240;
const xFromCanvas = hasUI ? dropPosition.clientX - leftPaneWidth : dropPosition.clientX;
const yFromCanvas = hasUI ? dropPosition.clientY - 40 : dropPosition.clientY;


node.x = bounds.x + xFromCanvas / zoom - node.width / 2;
node.y = bounds.y + yFromCanvas / zoom - node.height / 2;
} else {
node.x = figma.viewport.center.x - node.width / 2;
node.y = figma.viewport.center.y - -(node.height / 2);
}
console.log(figma.viewport.center.x, figma.viewport.center.y );

figma.currentPage.selection = [node];

figma.notify('3dicons/'+msg.icoName + ' added' );

return;
}
}

0 comments on commit 1ba7557

Please sign in to comment.