paperjs / paper.js

The Swiss Army Knife of Vector Graphics Scripting – Scriptographer ported to JavaScript and the browser, using HTML5 Canvas. Created by @lehni & @puckey
http://paperjs.org
Other
14.5k stars 1.23k forks source link

copyAttributes on symbol definitions does not update symbolItems #1683

Closed nikkwong closed 5 years ago

nikkwong commented 5 years ago

Sketch

const item = new Path.Rectangle({size: [30, 30]})
item.fillColor = 'red'
var symbol = new SymbolDefinition(item)

new SymbolItem(symbol, {x: 400, y: 100})

symbol.definition.copyAttributes(new Path.Rectangle({size: [10, 10], position: {x: 30, y: 30}}))

Symbol items should use updated rectangle, but still appear at {x: 400, y: 100}

nikkwong commented 5 years ago

Workaround is just replacing the definition:

symbol.definition = new Path.Rectangle({size: [10, 10], position: {x: 30, y: 30}})

Maybe add this to the docs if you don't mind; it's unclear which methods/properties specifically can be set on a SymbolDefinition to successfully update references.

sasensi commented 5 years ago

Hi, I think that there are multiple misunderstanding in your first code example:

With that said, here is a sketch demonstrating how we can replace the definition of a SymbolItem.

// We create 2 symbol definitions.
var symbol1 = new SymbolDefinition(
    new Path.Circle({
        center: [0, 0],
        radius: 50,
        fillColor: 'orange'
    })
);
var symbol2 = new SymbolDefinition(
    new Path.Circle({
        center: [0, 0],
        radius: 25,
        fillColor: 'red'
    })
);

// We place an instance of the first symbol.
var placedSymbol1 = symbol1.place(view.center);
// We place an instance of the second symbol.
var placedSymbol2 = symbol2.place(view.center + 100);
// We place an instance of the first symbol...
var placedSymbol3 = symbol1.place(view.center + 200);
// ...but we then make if point to the second symbol.
placedSymbol3.definition = symbol2;
nikkwong commented 5 years ago

Hey thanks for getting back to me.

Thanks for correcting my incorrect use of using size in Path.Rectangle. Keep getting those mixed up.

It seems as though I want to use SymbolDefinition in a way that is not currently supported by the library. In my case I am using the SymbolDefinition as a substitute for the inability to create patterns in paperjs. It works well actually, and to create attribute modifications on each instance, it is easiest to just modify the SymbolDefinition, including for matrix transformations (a translate operation for example on the definition translates each SymbolItem based on the definition's translate + the SymbolItem's translate, which is lovely). It already works in latest as is but has a few peculiarities.

Maybe you guys don't want to support this, but at least in my application it adds a lot of value and makes the use of symbols more robust.