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

2019-07-02 01:48发布

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;
}

2条回答
兄弟一词,经得起流年.
2楼-- · 2019-07-02 02:06

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().

查看更多
倾城 Initia
3楼-- · 2019-07-02 02:17
$("#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");
查看更多
登录 后发表回答