Paper.js: can't set fillColor of a symbol instance

天涯浪子 提交于 2019-12-10 18:16:24

问题


I'm new to paper.js.

For this project, I need a shape that will be used in many instances (with different fill colors), so it's apparently better to use a symbol (instead of using the Path.clone() method). However, once I instantiate the symbol into a placedSymbol, it seems that changing the fillColor property has no effect on the rendered shape: it remains the initial color of the symbol.

Other properties, such as position or opacity, are successfully set.

My question: how can I change the fill color of each instance of a symbol?

jsFiddle here: http://jsfiddle.net/GlauberRocha/uTskY/ (note that I've put all the code in the HTML pane. Doesn't seem to work otherwise, probably because paperscript isn't plain javascript).

Paperscript code:

var
  path = new Path(),
  symbol = {},
  inst = [],
  colors = ["#1f8f81", "#c7c5a8", "#1b4a9f", "#d6a493", "#1a8879", "#599ce3", "#1a459c", "#b9a87a", "#365db2", "#2479d4", "#a46430", "#1b449a", "#a4632e", "#1a4297", "#3359ad", "#b1852b", "#1a8077", "#1b3849", "#ae832a", "#186cc9", "#1b8178"]

path.add(new Point(0, 56), new Point(56, 0), new Point(56, 40), new Point(0, 96));
path.fillColor = "red";
path.closed = true;
symbol = new Symbol(path);
path.remove();

for (var i = 0; i < 20; ++i) {
  inst[i] = symbol.place();
  inst[i].fillColor = colors[i]; // Change fill color : NO
  console.log(inst[i].fillColor); // But... the correct color value appears here
  inst[i].opacity = (i / 30) + .4; // Change opacity: OK
  inst[i].position.x = 100; // Change position: OK
  inst[i].position.y = 42 * i + 50;
}

回答1:


Answered by Jonathan Puckey, of the paper.js team:

This is by design. You cannot change the visual properties of an instance of a Symbol. To change the color of the item, you can create multiple copies of the path by using path.clone().




回答2:


$("#symbol_id").find("g").attr('fill', "#colorOfChoice");

Alt, set ID of the g:s you want to change and

$("#symbol_id").find("#g_id").attr('fill', "#colorOfChoice");


来源:https://stackoverflow.com/questions/9754193/paper-js-cant-set-fillcolor-of-a-symbol-instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!