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

2019-07-02 01:34发布

问题:

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");