Javascript数组覆盖先前的数据

时间:2018-08-14 14:38:51

标签: javascript arrays

我正在为Adobe Illustrator编码一些东西,并且在下面的这段代码中遇到了问题。如果我将对象的网格设置为<110,不透明度为80,则每个数组的对象的不透明度为54。然后,数组变为54,并将刚更改的值更改为80至45,将45更改为39,依此类推。

总而言之,将99-28中的每个对象都设置为28,并将27-1中的每个对象都设置为27。

我尝试重新排列数组,使其从27-0到28-100,希望能避免拆分,但仍然存在相同的问题。

认为我可能已经找到了答案,但不确定如何解释此代码:STACK LINK HERE

function test() {
    var docRef = app.activeDocument;
    var layers = docRef.layers;
    var paths = docRef.pathItems;
    var items = docRef.selection;
    var meshtitle = "Mesh?"
    var mesh = Number(prompt("meshcount", 180, meshtitle));
    if (mesh <= 110) {
        var opacityRelationships = {
            "100": 100, "99": 99, "98": 98, "97": 97, "96": 68, "95": 67, "94": 66, "93": 65, "92": 64, "91": 63, "90": 63, "89": 62, "88": 61, "87": 61, "86": 60, "85": 59, "84": 58, "83": 57, "82": 56, "81": 55, "80": 54, "79": 53, "78": 53, "77": 52, "76": 52, "75": 52, "74": 51, "73": 51, "72": 51, "71": 50, "70": 50, "69": 50, "68": 49, "67": 49, "66": 49, "65": 48, "64": 48, "63": 48, "62": 47, "61": 47, "60": 47, "59": 46, "58": 46, "57": 46, "56": 46, "55": 45, "54": 45, "53": 45, "52": 44, "51": 44, "50": 44, "49": 43, "48": 42, "47": 41, "46": 40, "45": 39, "44": 39, "43": 38, "42": 37, "41": 37, "40": 36, "39": 35, "38": 34, "37": 34, "36": 33, "35": 33, "34": 32, "33": 32, "32": 31, "31": 30, "30": 29, "29": 28, "28": 28, "27": 27, "26": 27, "25": 26, "24": 26, "23": 25, "22": 25, "21": 24, "20": 24, "19": 24, "18": 23, "17": 23, "16": 22, "15": 22, "14": 21, "13": 21, "12": 20, "11": 20, "10": 20, "9": 19, "8": 18, "7": 17, "6": 16, "5": 15, "4": 14, "3": 13, "2": 12, "1": 11, "0": 0
        }
    }
    if (mesh > 110 && mesh <= 180) {
        var opacityRelationships = {
            "100": 40, "99": 40, "98": 40, "97": 40, "96": 40, "95": 40, "94": 40, "93": 10, "92": 10, "91": 10, "90": 10, "89": 10, "88": 10, "87": 10, "86": 86, "85": 85, "84": 84, "83": 83, "82": 82, "81": 81, "80": 80, "79": 79, "78": 78, "77": 77, "76": 76, "75": 75, "74": 74, "73": 73, "72": 72, "71": 71, "70": 70, "69": 69, "68": 68, "67": 67, "66": 66, "65": 65, "64": 64, "63": 63, "62": 62, "61": 61, "60": 10, "59": 59, "58": 58, "57": 57, "56": 56, "55": 55, "54": 54, "53": 53, "52": 52, "51": 51, "50": 50, "49": 49, "48": 48, "47": 47, "46": 46, "45": 45, "44": 44, "43": 43, "42": 42, "41": 41, "40": 40, "39": 39, "38": 38, "37": 37, "36": 36, "35": 35, "34": 34, "33": 33, "32": 32, "31": 31, "30": 30, "29": 29, "28": 28, "27": 27, "26": 26, "25": 25, "24": 24, "23": 23, "22": 22, "21": 21, "20": 20, "19": 19, "18": 18, "17": 17, "16": 16, "15": 15, "14": 14, "13": 13, "12": 12, "11": 11, "10": 10, "9": 9, "8": 8, "7": 7, "6": 6, "5": 5, "4": 4, "3": 3, "2": 2, "1": 1, "0": 0
        }
    }
    if (mesh > 180 && mesh <= 255) {
        var opacityRelationships = {
            "100": 55, "99": 55, "98": 55, "97": 55, "96": 55, "95": 55, "94": 55, "93": 55, "92": 10, "91": 10, "90": 10, "89": 10, "88": 10, "87": 10, "86": 86, "85": 85, "84": 84, "83": 83, "82": 82, "81": 81, "80": 80, "79": 79, "78": 78, "77": 77, "76": 76, "75": 75, "74": 74, "73": 73, "72": 72, "71": 71, "70": 70, "69": 69, "68": 68, "67": 67, "66": 66, "65": 65, "64": 64, "63": 63, "62": 62, "61": 61, "60": 10, "59": 59, "58": 58, "57": 57, "56": 56, "55": 55, "54": 54, "53": 53, "52": 52, "51": 51, "50": 50, "49": 49, "48": 48, "47": 47, "46": 46, "45": 45, "44": 44, "43": 43, "42": 42, "41": 41, "40": 40, "39": 39, "38": 38, "37": 37, "36": 36, "35": 35, "34": 34, "33": 33, "32": 32, "31": 31, "30": 30, "29": 29, "28": 28, "27": 27, "26": 26, "25": 25, "24": 24, "23": 23, "22": 22, "21": 21, "20": 20, "19": 19, "18": 18, "17": 17, "16": 16, "15": 15, "14": 14, "13": 13, "12": 12, "11": 11, "10": 10, "9": 9, "8": 8, "7": 7, "6": 6, "5": 5, "4": 4, "3": 3, "2": 2, "1": 1, "0": 0
        }
    }
    if (mesh > 255) {
        var opacityRelationships = {
            "100": 80, "99": 80, "98": 80, "97": 80, "96": 80, "95": 80, "94": 80, "93": 80, "92": 10, "91": 10, "90": 10, "89": 10, "88": 10, "87": 10, "86": 86, "85": 85, "84": 84, "83": 83, "82": 82, "81": 81, "80": 80, "79": 79, "78": 78, "77": 77, "76": 76, "75": 75, "74": 74, "73": 73, "72": 72, "71": 71, "70": 70, "69": 69, "68": 68, "67": 67, "66": 66, "65": 65, "64": 64, "63": 63, "62": 62, "61": 61, "60": 10, "59": 59, "58": 58, "57": 57, "56": 56, "55": 55, "54": 54, "53": 53, "52": 52, "51": 51, "50": 50, "49": 49, "48": 48, "47": 47, "46": 46, "45": 45, "44": 44, "43": 43, "42": 42, "41": 41, "40": 40, "39": 39, "38": 38, "37": 37, "36": 36, "35": 35, "34": 34, "33": 33, "32": 32, "31": 31, "30": 30, "29": 29, "28": 28, "27": 27, "26": 26, "25": 25, "24": 24, "23": 23, "22": 22, "21": 21, "20": 20, "19": 19, "18": 18, "17": 17, "16": 16, "15": 15, "14": 14, "13": 13, "12": 12, "11": 11, "10": 10, "9": 9, "8": 8, "7": 7, "6": 6, "5": 5, "4": 4, "3": 3, "2": 2, "1": 1, "0": 0
        }
    }

    function updateOpacity(item) {
        var curOpacity = Math.floor(item.opacity).toString();
        item.opacity = opacityRelationships[curOpacity];
    }
    for (var i = 0; i < paths.length; i++) {
        if (paths[i].fillColor == '[GradientColor]') {
            for (var x = 0, len = paths.length; x < len; x++) {
                for (var y = 0; y < paths[i].fillColor.gradient.gradientStops.length; y++) {
                    updateOpacity(paths[i].fillColor.gradient.gradientStops[y]);
                }
            }
        } else if (paths[i].fillColor == '[SpotColor]') {
            for (var x = 0, len = layers.length; x < len; x++) {
                for (var y = 0, yLen = layers[x].pageItems.length; y < yLen; y++) {
                    updateOpacity(layers[x].pageItems[y]);
                }
            }
        }
    }
}
test();

1 个答案:

答案 0 :(得分:0)

找到了解决方案。函数在彼此之间循环,我需要它们一次运行一次(我想我说的没错)。

function test() {
        var docRef = app.activeDocument;
        var layers = docRef.layers;
        var paths = docRef.pathItems;
        var items = docRef.selection;
        var meshtitle = "Mesh?"
        var mesh = Number(prompt("meshcount", 180, meshtitle));
        if (mesh <= 110) {
            var opacityRelationships = {
                "100": 100, "99": 99, "98": 98, "97": 97, "96": 68, "95": 67, "94": 66, "93": 65, "92": 64, "91": 63, "90": 63, "89": 62, "88": 61, "87": 61, "86": 60, "85": 59, "84": 58, "83": 57, "82": 56, "81": 55, "80": 54, "79": 53, "78": 53, "77": 52, "76": 52, "75": 52, "74": 51, "73": 51, "72": 51, "71": 50, "70": 50, "69": 50, "68": 49, "67": 49, "66": 49, "65": 48, "64": 48, "63": 48, "62": 47, "61": 47, "60": 47, "59": 46, "58": 46, "57": 46, "56": 46, "55": 45, "54": 45, "53": 45, "52": 44, "51": 44, "50": 44, "49": 43, "48": 42, "47": 41, "46": 40, "45": 39, "44": 39, "43": 38, "42": 37, "41": 37, "40": 36, "39": 35, "38": 34, "37": 34, "36": 33, "35": 33, "34": 32, "33": 32, "32": 31, "31": 30, "30": 29, "29": 28, "28": 28, "27": 27, "26": 27, "25": 26, "24": 26, "23": 25, "22": 25, "21": 24, "20": 24, "19": 24, "18": 23, "17": 23, "16": 22, "15": 22, "14": 21, "13": 21, "12": 20, "11": 20, "10": 20, "9": 19, "8": 18, "7": 17, "6": 16, "5": 15, "4": 14, "3": 13, "2": 12, "1": 11, "0": 0
            }
        }
        if (mesh > 110 && mesh <= 180) {
            var opacityRelationships = {
                "100": 40, "99": 40, "98": 40, "97": 40, "96": 40, "95": 40, "94": 40, "93": 10, "92": 10, "91": 10, "90": 10, "89": 10, "88": 10, "87": 10, "86": 86, "85": 85, "84": 84, "83": 83, "82": 82, "81": 81, "80": 80, "79": 79, "78": 78, "77": 77, "76": 76, "75": 75, "74": 74, "73": 73, "72": 72, "71": 71, "70": 70, "69": 69, "68": 68, "67": 67, "66": 66, "65": 65, "64": 64, "63": 63, "62": 62, "61": 61, "60": 10, "59": 59, "58": 58, "57": 57, "56": 56, "55": 55, "54": 54, "53": 53, "52": 52, "51": 51, "50": 50, "49": 49, "48": 48, "47": 47, "46": 46, "45": 45, "44": 44, "43": 43, "42": 42, "41": 41, "40": 40, "39": 39, "38": 38, "37": 37, "36": 36, "35": 35, "34": 34, "33": 33, "32": 32, "31": 31, "30": 30, "29": 29, "28": 28, "27": 27, "26": 26, "25": 25, "24": 24, "23": 23, "22": 22, "21": 21, "20": 20, "19": 19, "18": 18, "17": 17, "16": 16, "15": 15, "14": 14, "13": 13, "12": 12, "11": 11, "10": 10, "9": 9, "8": 8, "7": 7, "6": 6, "5": 5, "4": 4, "3": 3, "2": 2, "1": 1, "0": 0
            }
        }
        if (mesh > 180 && mesh <= 255) {
            var opacityRelationships = {
                "100": 55, "99": 55, "98": 55, "97": 55, "96": 55, "95": 55, "94": 55, "93": 55, "92": 10, "91": 10, "90": 10, "89": 10, "88": 10, "87": 10, "86": 86, "85": 85, "84": 84, "83": 83, "82": 82, "81": 81, "80": 80, "79": 79, "78": 78, "77": 77, "76": 76, "75": 75, "74": 74, "73": 73, "72": 72, "71": 71, "70": 70, "69": 69, "68": 68, "67": 67, "66": 66, "65": 65, "64": 64, "63": 63, "62": 62, "61": 61, "60": 10, "59": 59, "58": 58, "57": 57, "56": 56, "55": 55, "54": 54, "53": 53, "52": 52, "51": 51, "50": 50, "49": 49, "48": 48, "47": 47, "46": 46, "45": 45, "44": 44, "43": 43, "42": 42, "41": 41, "40": 40, "39": 39, "38": 38, "37": 37, "36": 36, "35": 35, "34": 34, "33": 33, "32": 32, "31": 31, "30": 30, "29": 29, "28": 28, "27": 27, "26": 26, "25": 25, "24": 24, "23": 23, "22": 22, "21": 21, "20": 20, "19": 19, "18": 18, "17": 17, "16": 16, "15": 15, "14": 14, "13": 13, "12": 12, "11": 11, "10": 10, "9": 9, "8": 8, "7": 7, "6": 6, "5": 5, "4": 4, "3": 3, "2": 2, "1": 1, "0": 0
            }
        }
        if (mesh > 255) {
            var opacityRelationships = {
                "100": 80, "99": 80, "98": 80, "97": 80, "96": 80, "95": 80, "94": 80, "93": 80, "92": 10, "91": 10, "90": 10, "89": 10, "88": 10, "87": 10, "86": 86, "85": 85, "84": 84, "83": 83, "82": 82, "81": 81, "80": 80, "79": 79, "78": 78, "77": 77, "76": 76, "75": 75, "74": 74, "73": 73, "72": 72, "71": 71, "70": 70, "69": 69, "68": 68, "67": 67, "66": 66, "65": 65, "64": 64, "63": 63, "62": 62, "61": 61, "60": 10, "59": 59, "58": 58, "57": 57, "56": 56, "55": 55, "54": 54, "53": 53, "52": 52, "51": 51, "50": 50, "49": 49, "48": 48, "47": 47, "46": 46, "45": 45, "44": 44, "43": 43, "42": 42, "41": 41, "40": 40, "39": 39, "38": 38, "37": 37, "36": 36, "35": 35, "34": 34, "33": 33, "32": 32, "31": 31, "30": 30, "29": 29, "28": 28, "27": 27, "26": 26, "25": 25, "24": 24, "23": 23, "22": 22, "21": 21, "20": 20, "19": 19, "18": 18, "17": 17, "16": 16, "15": 15, "14": 14, "13": 13, "12": 12, "11": 11, "10": 10, "9": 9, "8": 8, "7": 7, "6": 6, "5": 5, "4": 4, "3": 3, "2": 2, "1": 1, "0": 0
            }
        }

      for (var i=0,len = paths.length;i<len;i++) {
        if (paths[i].fillColor == '[GradientColor]') {
	  for (var z = 0; z < paths[i].fillColor.gradient.gradientStops.length; z++) {
            function updateOpacityGradient(item){
                var curOpacity = Math.floor(item.opacity);
        	item.opacity = opacityRelationships[curOpacity];
	    }
		updateOpacityGradient(paths[i].fillColor.gradient.gradientStops[z]);
	  }
	}
	
   }
   
    for (var j=0,len = paths.length;j<len;j++) {
    for (var r=0,len = layers.length;r<len;r++){
	if (paths[j].fillColor == '[SpotColor]') {
	   for (var y=0,yLen = layers[r].pageItems.length;y<yLen;y++) {
	    function updateOpacitySpot(item2){
                var curOpacity = Math.floor(item2.opacity).toString();
        	item2.opacity = opacityRelationships[curOpacity];	
	    }
		updateOpacitySpot(layers[r].pageItems[y]);
	   }
        }
   }
   }
    }test();