From 48c9cc792972ea8f0c6917d1c88c70129f2ba573 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Thu, 12 Jan 2023 11:20:18 -0800 Subject: [PATCH] sketch --- d2renderers/d2sketch/sketch.go | 22 ++---- d2renderers/d2sketch/sketch_test.go | 5 ++ .../d2sketch/testdata/animated/sketch.exp.svg | 77 +++++++++++++++++++ d2renderers/d2svg/d2svg.go | 28 +------ d2target/d2target.go | 27 +++++++ 5 files changed, 116 insertions(+), 43 deletions(-) create mode 100644 d2renderers/d2sketch/testdata/animated/sketch.exp.svg diff --git a/d2renderers/d2sketch/sketch.go b/d2renderers/d2sketch/sketch.go index 4c4913a2b..ea9b7035b 100644 --- a/d2renderers/d2sketch/sketch.go +++ b/d2renderers/d2sketch/sketch.go @@ -163,20 +163,6 @@ func Paths(r *Runner, shape d2target.Shape, paths []string) (string, error) { return output, nil } -func connectionStyle(connection d2target.Connection) string { - out := "" - - out += fmt.Sprintf(`stroke:%s;`, connection.Stroke) - out += fmt.Sprintf(`opacity:%f;`, connection.Opacity) - out += fmt.Sprintf(`stroke-width:%d;`, connection.StrokeWidth) - if connection.StrokeDash != 0 { - dashSize, gapSize := svg.GetStrokeDashAttributes(float64(connection.StrokeWidth), connection.StrokeDash) - out += fmt.Sprintf(`stroke-dasharray:%f,%f;`, dashSize, gapSize) - } - - return out -} - func Connection(r *Runner, connection d2target.Connection, path, attrs string) (string, error) { roughness := 1.0 js := fmt.Sprintf(`node = rc.path("%s", {roughness: %f, seed: 1});`, path, roughness) @@ -185,10 +171,14 @@ func Connection(r *Runner, connection d2target.Connection, path, attrs string) ( return "", err } output := "" + animatedClass := "" + if connection.Animated { + animatedClass = " animated-connection" + } for _, p := range paths { output += fmt.Sprintf( - ``, - p, connectionStyle(connection), attrs, + ``, + animatedClass, p, connection.CSSStyle(), attrs, ) } return output, nil diff --git a/d2renderers/d2sketch/sketch_test.go b/d2renderers/d2sketch/sketch_test.go index c2b8dadea..f35eb1be7 100644 --- a/d2renderers/d2sketch/sketch_test.go +++ b/d2renderers/d2sketch/sketch_test.go @@ -39,6 +39,11 @@ func TestSketch(t *testing.T) { script: `winter.snow -> summer.sun `, }, + { + name: "animated", + script: `winter.snow -> summer.sun -> trees -> winter.snow: { style.animated: true } + `, + }, { name: "connection label", script: `a -> b: hello diff --git a/d2renderers/d2sketch/testdata/animated/sketch.exp.svg b/d2renderers/d2sketch/testdata/animated/sketch.exp.svg new file mode 100644 index 000000000..25a6c8c99 --- /dev/null +++ b/d2renderers/d2sketch/testdata/animated/sketch.exp.svg @@ -0,0 +1,77 @@ + + + + + + +wintersummertreessnowsun + + + \ No newline at end of file diff --git a/d2renderers/d2svg/d2svg.go b/d2renderers/d2svg/d2svg.go index b4b352b67..0f28eccb2 100644 --- a/d2renderers/d2svg/d2svg.go +++ b/d2renderers/d2svg/d2svg.go @@ -466,7 +466,7 @@ func drawConnection(writer io.Writer, labelMaskID string, connection d2target.Co animatedClass = " animated-connection" } fmt.Fprintf(writer, ``, - path, animatedClass, connectionStyle(connection), attrs) + path, animatedClass, connection.CSSStyle(), attrs) } if connection.Label != "" { @@ -966,32 +966,6 @@ func shapeStyle(shape d2target.Shape) string { return out } -func connectionStyle(connection d2target.Connection) string { - out := "" - - out += fmt.Sprintf(`stroke:%s;`, connection.Stroke) - out += fmt.Sprintf(`opacity:%f;`, connection.Opacity) - out += fmt.Sprintf(`stroke-width:%d;`, connection.StrokeWidth) - strokeDash := connection.StrokeDash - if strokeDash == 0 && connection.Animated { - strokeDash = 5 - } - if strokeDash != 0 { - dashSize, gapSize := svg.GetStrokeDashAttributes(float64(connection.StrokeWidth), strokeDash) - out += fmt.Sprintf(`stroke-dasharray:%f,%f;`, dashSize, gapSize) - - if connection.Animated { - dashOffset := -10 - if connection.SrcArrow != d2target.NoArrowhead && connection.DstArrow == d2target.NoArrowhead { - dashOffset = 10 - } - out += fmt.Sprintf(`stroke-dashoffset:%f;`, float64(dashOffset)*(dashSize+gapSize)) - out += fmt.Sprintf(`animation: dashdraw %fs linear infinite;`, gapSize*0.5) - } - } - return out -} - func embedFonts(buf *bytes.Buffer, fontFamily *d2fonts.FontFamily) { content := buf.String() buf.WriteString(`