Skip to content

Add deep clone support and picture manipulation in code#452

Closed
StefanKoell wants to merge 2 commits into
wieslawsoltes:masterfrom
StefanKoell:svg-cloning
Closed

Add deep clone support and picture manipulation in code#452
StefanKoell wants to merge 2 commits into
wieslawsoltes:masterfrom
StefanKoell:svg-cloning

Conversation

@StefanKoell
Copy link
Copy Markdown

Summary:

  • Add deep clone support for SKPicture and use it for SKSvg.Clone() so clones are independent of the original model state.
  • Make SKSvg.Picture lazy-initialized from Model and avoid setting it during clone operations.
  • Add SvgSource.Clone() that preserves original path/stream for ReLoad without eagerly copying the picture.

Limitations:

  • Reloading or re-applying CSS loads the model from the original source, so any in-memory modifications to the model are lost.

Result (sample app -> Cloning):

  • In the sample app, creating a clone from an SvgImage and applying a grayscale color transform updates the clone while the original stays unchanged, demonstrating independent render state and lazy picture creation.

Sample Code:

        var svgImage = svgCloningOriginal.Source as SvgImage;
        var clone = new SvgImage { Source = svgImage?.Source?.Clone() };

        foreach (var cmd in clone.Source?.Svg?.Model?.Commands?.OfType<DrawPathCanvasCommand>() ?? [])
        {
            var paint = cmd.Paint;
            if (paint?.Color is not null)
            {
                paint.Color = ToGrayscale(paint.Color.Value);
            }

            if (paint?.Shader is ColorShader shader)
            {
                paint.Shader = SKShader.CreateColor(ToGrayscale(paint.Color.Value), shader.ColorSpace);
            }
        }

        clone.Source?.RebuildFromModel();
        svgCloningClone.Source = clone;

@wieslawsoltes
Copy link
Copy Markdown
Owner

Alternative implementation:

@wieslawsoltes
Copy link
Copy Markdown
Owner

Alternative was implemented and merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants