Textmarker Highlight Effect (Stretch SVG over width of a span element)
Hey there! I want to make an animated text marker highlighter effect for headings on my website. I made some SVG's that I can animate using
stroke-dashoffset
, but if I have more than one short word, the SVG only centers in the text but does not grow in width. I tried setting width/height and some other attributes, but when it grows in width, the height grows to preserve the aspect ratio and overflows to the bottom of the span. Can anybody tell me how I can make the SVG "fill"/"stretch" in the width of the container? (Any other improvements of my code are appreciated too.)
Here is my code so far:
9 Replies
MDN Web Docs
preserveAspectRatio - SVG: Scalable Vector Graphics | MDN
The preserveAspectRatio attribute indicates how an element with a viewBox providing a given aspect ratio must fit into a viewport with a different aspect ratio.
I tried the
XminYmin meet
but that did not help at all. Or can youe explain to me what you mean exactly?It looks like this for example.
Try
preserveAspectRatio="none"
.I think that does the trick, š still need to do some fine-tuning but now it stenches finally. I did not know I could set it to none... š Thanks a lot.
Do you have any improvements for my code? Or for my plan at all? Just being curious for better solutions.
Your plan seems reasonable to me.
You might look at the
pathLength
attribute. That would let you set something like pathLength="1"
& then you wouldn't have to use trial & error to determine what dash-offset
& dash-array
need to be. Also, it would let you more easily reuse the CSS for different paths.
(I used the same effect when converting a PDF to EPUB a while back. The cover image appears static, but after about 30 seconds it unwinds itself & then comes back.)Ohhhh thats smart! Thanks a lot!
@dys š Do you maby know if i can make a pattern for my path to give it a brush/marker look? I know how to define a pattern but when i use it as stroke on my path I only see a part of the brush and not the brush appearing "multiple times" after each other. I currently look on how to make a dash array from my path.
https://codepen.io/FreakeyPlays/pen/BagXXzm
Thats how far I have come with a path and apllying a pattern to the stroke, but I cannot get the edges to show the Pattern it is always a flat cut and the pattern just repeats itself on the y axis.
https://blog.tentaclelabs.com/posts/2022/04/draw-a-pattern-along-a-path-using-svg
Found this what looks promising but I cant get it to work rn. have to try later
Tentacle Labs Blog
Draw a pattern along a path using SVG | Tentacle Labs Blog
Recently I wanted to create the effect of a cable for an application Iām working on. The cable should have a textile look, similar to a rope, which is quite common right now (for example the charging cable of the current MacBook Pro). Iām using SVG for rendering, so applying a pattern to a stroke path should be easy, right?