Is this correct to use image fill


#1
async function imageFill() {
    const fs = storage.localFileSystem;
    const pluginFolders = await fs.getPluginFolder();
    const entries = await pluginFolders.getEntries();
    var imagefolder;
    entries.forEach(e => {
        if (e.name == "images") {
            imagefolder = e.nativePath;
        }
    });
    let imageFile = "\\baselinetheme.png"

    return new ImageFill(imagefolder + imageFile);
}

now How to use this fill to use in rectangle element explain… anyone


#2

Hi @PramUkesh,

The API reference has a good example documented: https://adobexdplatform.com/plugin-docs/reference/ImageFill.html?h=imagefill.

If you are looking for a solution for using images received from network requests, we have a guide for it:
Have you seen this guide? https://adobexdplatform.com/plugin-docs/tutorials/how-to-make-network-requests/?h=imagefill

Hope this helps!


#3

Using the code you’ve supplied, there’s an easier way – you can use getEntry:

async function imageFill() {
    const fs = storage.localFileSystem;
    const pluginFolders = await fs.getPluginFolder();
    const imageFile = await pluginFolders.getEntry("images/baselinetheme.png");
    return new ImageFill(imageFile);
}

The above should do the trick for you.


#4
function createThemePreview(selection) {
    const rect = new Rectangle();
    rect.name = "themePreview";
    rect.width = 866;
    rect.height = 537;
    rect.cornerRadii = {
        topLeft: 0,
        topRight: 0,
        bottomLeft: 0,
        bottomRight: 0,
    };
    rect.fill = new Color("#000000");
    rect.stroke = new Color("#707070");
    rect.strokeWidth = 1;
    rect.moveInParentCoordinates(0, 156);
    var imagefill;
    imageFill().then(
        (success => {
            console.log("image fill success");
            console.log(success);
            imagefill = success;
        }),
        (failure => {
            console.log("image fill failed");
        })
    );
    rect.fill = imagefill;
    selection.insertionParent.addChild(rect);
    return rect;
}

Throws Error

Plugin Error: Plugin 07b2dfeb is not permitted to make changes from the background. Return a Promise to continue execution asynchronously.
        at convertPluginErrorToString (plugins/PluginErrorUtil.js:1:198)
        at internalFormatPluginError (plugins/PluginErrorUtil.js:1:503)
        at internalReportPluginError (plugins/PluginErrorUtil.js:1:610)
        at Object.reportPluginError (plugins/PluginErrorUtil.js:1:1015)
        at Object.checkAllowedToEdit (plugins/ScenegraphGuard.js:1:1094)
        at Rectangle.<anonymous> (plugins/ScenegraphWrappers.js:1:2286)
        at createThemePreview (C:\Users\PramUkesh\AppData\Local\Packages\Adobe.CC.XD_adky2gkssdxte\LocalState\develop\07b2dfeb\main.js:334:15)

What is causing this error and how to fix it
#5

You’re adding something to the scenegraph in an async operation – you must make sure that there’s a complete promise chain returned to the menu handler in order to make changes to the scenegraph. If not, XD locks it down immediately after invoking your plugin.

module.exports = {
    commands: {
        someCommand: function(selection) {
            return new Promise((resolve, reject) => {
                // you can make changes as long as this promises is neither resolved nor rejected
                // once resolved, the scenegraph becomes locked to changes from code
                // if rejected, changes to the scenegraph will be undone completely
            });
        }
    }
};

#6

do you suggest me a better way to fill rect obj with an asset file without a promise chain
@kerrishotts


#7

due to lack of timeout api some js operations are hard implement due to js is single threaded


#8

There’s no other way – once you start doing anything that involves an asynchronous operation (file IO, network IO, renditions), you’ll need to be in the land of promises, and one unbroken chain all the way back to the menu handler.

If async/await is easier for you to use, that works too:

module.exports = {
    commands: {
        someCommand: async function(selection) {
            const doSomethingAsync = await someOtherAsyncFunction();
            // can still do things to the scenegraph
            // throw if you want them to be undone
        }
    }
}

(Async/await is built on top of promises, so XD can work with async functions too.)


#9

I tried promise function, still not able to fix “xxxx is not permitted to make changes from the background. Return a Promise to continue execution asynchronously.”

Can you please give an example to handle this problem


#10

@realvjy make sure to return inside your Promise block


#11

Hey steve,
Thank you for quick response. Still not luck to solve this problem. I’m not too aware of promise/await/async fx.
Can you please check this code?

// Get Rectangle and oval Shapes
async function fillImages(selection, allImages) {
  var imageCount = allImages.length;
  if (imageCount != 0 && imageCount >= selection.items.length) {
    for (var i = 0; i < selection.items.length; i++) {
      if (selection.items[i] instanceof scenegraph.Rectangle || selection.items[i] instanceof scenegraph.Ellipse) {
        try {
          var imgObj = await getImage(allImages[i]);
          fillSelectionWithImage(selection.items[i], imgObj);
        } catch (e) {
          console.log(e);
        }
      } else {
        console.log('please select elipse or rectangle');
      }
    }
  } else {
    console.log('too many selection');
  }
}

// Check image
function getImage(selectedImage) {
  return new Promise((resolve, reject) => {
    if (selectedImage) {
      try {
        const image = selectedImage;
        resolve(image);
      } catch (e) {
        reject('could not load image')
      }
    } else {
      reject('had an error')
    }
  });
}

// Fill selected shape with image
function fillSelectionWithImage(selectedPath, image) {
  const imageFill = new ImageFill(image);
  selectedPath.fill = imageFill;
}

#12

Higher func i.e func provided in exports to a menu item of your plugin to be a promise to make changes for a scenegraph

See this reply


#13

Hey @realvjy Looks like you are working on the XD port of your sketch plugin uiLogos.


#14

@bantya yeah, I’m working on it and struggling with new syntax await/promise/async. :grinning:


#15

@PramUkesh thanks! It works like charm :grinning:


#16

Looking forward to it. Then complete it and release it fast. Best wishes for that. :smile:


#17

Here it is - https://github.com/realvjy/uilogos-XD/releases (Download .xdx file and double click to install)
Can you please test it and share your feedback?


#18

For perfect squares, Logomarks, work flawlessly:

But for Logotypes it just stumbles:

According to me, the logo should just fit in the container, independent of the size and shape of the container (be it square or vertical/horizontal rectangles)


#19

@bantya This was intended to change the width of container to respect ratio of original logo.

Idea behind logotype here -
Logotype have different dimension (height and width can’t fixed for all logo).
Possible solution:

  1. Center fill or fit logo inside the original container/rectangle (Problem: Logo should be visible completely, it’s not general image)
  2. Just picked height of rectangle/container and change the width of container respect to the ratio of original logotype. (Problem: If have group of container/rectangle, will stumbles :grinning:)

Solution 2 is best for logo related stuff. So, I used this approach. We can re-arrange logos with the help of align distribute.

PS: Added border for comparison


#20

Completely agree with you bhai. Lage raho…