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.46k stars 1.22k forks source link

Segment Stringify #2086

Open NIkola458 opened 1 week ago

NIkola458 commented 1 week ago

Description/Steps to reproduce

Stringifying a segment leads to just an array, instead of an object with labeled properties like point, handleIn, and handleOut."

Link to reproduction test-case

https://codepen.io/nikola12334/pen/JjgopMK

Expected result

"segment":{"point":{"x":242,"y":259},"handleIn":{"x":0,"y":0},"handleOut":{"x":0,"y":0}}

Actual result

"['Path',{'applyMatrix':true,'segments':[[[100,50],[0,0],[80,100]],[[300,50],[-80,-100],[0,0]]],'strokeColor':[0,0,0]}]"

Additional information

mac os: 14.6.1 , Node: v16.13.0

OblivionSY commented 1 week ago

For clarity, I think you're comparing a segment with the whole path. I think your actual result should be showing just a segment. And therfore, from your codepen, this is your actual result

if you do console.log(JSON.stringify(secondSegment)) you get this:

"['Segment',[300,50],[-80,-100],[0,0]]"

and you would expect

"segment":{"point":{"x":300,"y":50},"handleIn":{"x":-80,"y":-100},"handleOut":{"x":0,"y":0}}

though it may be handleOut could be omitted since it is 0,0

The real issue, which you've not mentioned, is when you JSON.Parse(...) you do not get back to the Segment object. The question is why does stringify trash the object structure in this way, and what is the workaround.