简体   繁体   English

从我在 Sketch 中预定义的阴影在对象上实现阴影

[英]Implement shadow on the object from my predefine shadows in Sketch

I am not pro in javascript but I want to implement shadow on the object from my predefine shadows but the code is not working:我不是 javascript 专家,但我想从我的预定义阴影中在对象上实现阴影,但代码不起作用:

      var selection = context.selection;
      for (var i=0; i < selection.count(); i++){
        var layer = selection.objectAtIndex(i)

           if (layer.isEmpty) UI.message('No layers selected!');
           else {
                var sha_0 = [];
                var sha_1 = [
                color: '#00000024',
                 x: 0,
                 y: 1,
                 blur: 1,
                 spread: 0
                ]
                var sha_2 = [
                color: '#00036024',
                 x: 0,
                 y: 1,
                 blur: 1,
                 spread: 0
                ]
                var options = ['0dp', '1dp', '2dp'];
                var selectDialog = sketch.UI.getSelectionFromUser("Please select shadow depth:", options);
                if (selected == 0) {
                layer.forEach(function (e) {
                   e.style.shadows = sha_0;
                 });
               } else if (selected == 1) {
                 layer.forEach(function (e) {
                   e.style.shadows = sha_1;
                 });
               } else if (selected == 2) {
                 layer.forEach(function (e) {
                   e.style.shadows = sha_2;
                 });
               }
           }
      }

The methods you need to call belonging to the UI library are asynchronous.您需要调用的属于 UI 库的方法是异步的。 They rely on the user performing some action before the code can continue.它们依赖于用户在代码可以继续之前执行某些操作。 In Javascript this requires either a callback function or the use of async / await with Promises.在 Javascript 中,这需要回调函数或将async / await与 Promises 一起使用。 The specific method you're using: sketch.UI.getSelectionFromUser is depricated in favor of the new UI.getInputFromUser method as you can find here .您正在使用的特定方法: sketch.UI.getSelectionFromUsersketch.UI.getSelectionFromUser ,取而代之的是新的UI.getInputFromUser方法,您可以在此处找到

For example:例如:

UI.getInputFromUser(
  "What's your favorite design tool?",
  {
    type: UI.INPUT_TYPE.selection,
    possibleValues: ['Sketch', 'Paper'],
  },
  (err, value) => {
    if (err) {
      // most likely the user canceled the input
      return
    }
  }
)

Thus, your code will look something like this:因此,您的代码将如下所示:

// selected layers via document object per docs
var selection = document.selectedLayers;
// iterate directly over the selection layers
selection.forEach(layer => {
  // These shadows are objects, not arrays, so changed [] to {}
  var sha_0 = [];
  var sha_1 = [{
    color: '#00000024',
    x: 0,
    y: 1,
    blur: 1,
    spread: 0
  }]
  var sha_2 = [{
    color: '#00036024',
    x: 0,
    y: 1,
    blur: 1,
    spread: 0
  }]
  var options = ['0dp', '1dp', '2dp'];
  // Use the newer getInputFromUser method
  UI.getInputFromUser(
    "Please select shadow depth:",
    {
      type: UI.INPUT_TYPE.selection,
      possibleValues: options,
    },
    (err, value) => {
      if (err) {
        // most likely the user canceled the input
        return
      }
      switch (value) {
        case options[0]:
          layer.style.shadows = sha_0;
          break;
        case options[1]:
          layer.style.shadows = sha_1;
          break;
        case options[2]:
          layer.style.shadows = sha_2;
          break;
      }
    }
  )
})

Your code could possibly be simplified more, but that should get you moving again.您的代码可能会被进一步简化,但这应该会让您再次前进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM