Handwriting An SVG Heart, With Our Hearts

Handwriting An SVG Heart, With Our Hearts

In response to native grocery shops, it’s the Valentine’s Day season once more, and what higher method to categorical our love than with the image of affection: a coronary heart. Some time again on CSS-Tips, we shared several ways to draw hearts, and the response was dreamy. Try all these superb, heart-filled submissions on this collection on CodePen:

Temani Afif’s CSS Shapes web site provides an excellent fashionable coronary heart utilizing solely CSS:

Now, to indicate my love, I wished to do one thing private, one thing artful, one thing with a gentle quantity of effort.

L is for Love Traces

Handwriting a love be aware is a traditional romantic gesture, however have you ever thought-about handwriting an SVG? We received’t want some fancy vector drawing software to specific our love. As an alternative, we are able to open a clean HTML doc and add an <svg> tag:

<svg>

</svg>

We’ll want a method to see what we’re doing contained in the “SVG realm” (as I prefer to name it), which is what the viewBox attribute gives. The 2D aircraft upon which vector graphics render is as infinite as our love, fairly actually, full with an x- and y-axis and all (like from math class).

We’ll set the beginning coordinates as 0 0 and finish coordinates as 10 10 to make a good-looking, sq. viewBox. Oh, and by the best way, we don’t concern ourselves over pixels, rem values, or every other unit varieties; that is vector graphics, and we play by our personal guidelines.

We add in these coordinates to the viewBox as a string of values:

<svg viewBox="0 0 10 10">

</svg>

Now we are able to start drawing our coronary heart, with our coronary heart. Let’s make a line. To do this, we’ll must know much more about coordinates, and the place to stay ’em. We’re in a position to attract a line with many factors utilizing the <path> component, which defines paths utilizing the d attribute. SVG path commands are difficult to memorize, however the effort means you care. The trail instructions are:

  • MoveTo: M, m
  • LineTo: L, l, H, h, V, v
  • Cubic Bézier curve: C, c, S, s
  • Quadratic Bézier Curve: Q, q, T, t
  • Elliptical arc curve: A, a
  • ClosePath: Z, z

We’re solely thinking about drawing line segments for now, so collectively we’ll discover the primary two: MoveTo and LineTo. MDN romantically describes MoveTo as choosing up a drawing instrument, comparable to a pen or pencil: we aren’t but drawing something, simply transferring our pen to the purpose the place we need to start our confession of affection.

We’ll MoveTo (M) the coordinates of (2,2) represented within the d attribute as M2,2:

<svg viewBox="0 0 10 10">
  <path d="M2,2" />
</svg>

Not stunning then to search out that LineTo is akin to placing pen to paper and drawing from one level to a different. Let’s draw the primary phase of our coronary heart by drawing a LineTo (L) with coordinates (4,4), represented as L2,2 subsequent within the d attribute:

<svg viewBox="0 0 10 10">
  <path d="M2,2 L4,4" />
</svg>

We’ll add a closing line phase as one other LineTo L with coordinates (6,2), once more appended to the d attribute as L6,2:

<svg viewBox="0 0 10 10">
  <path d="M2,2 L4,4 L6,2" />
</svg>
Handwriting An SVG Heart, With Our Hearts

When you cease to preview what we’ve achieved to date, you could be confused because it renders an upside-down triangle; that’s not fairly a coronary heart but, Let’s repair that.

SVG shapes apply a fill by default, which we are able to take away with fill="none":

<svg viewBox="0 0 10 10">
  <path d="M2,2 L4,4 L6,2" fill="none" />
</svg>

Reasonably than filling within the form, as an alternative, let’s show our line path by including a stroke, including shade to our coronary heart.

<svg viewBox="0 0 10 10">
  <path 
    d="M2,2 L4,4 L6,2" 
    fill="none" 
    stroke="rebeccapurple" />
</svg>

Subsequent, add some weight to the stroke by growing the stroke-width:

<svg viewBox="0 0 10 10">
  <path 
    d="M2,2 L4,4 L6,2" 
    fill="none" 
    stroke="rebeccapurple" 
    stroke-width="4" />
</svg>

Lastly, apply a stroke-linecap of spherical (sorry, no time for butt jokes) to spherical off the beginning and finish factors of our line path, giving us that traditional image of affection:

<svg viewBox="0 0 10 10">
  <path 
    d="M2,2 L4,4 L6,2" 
    fill="none"
    stroke="rebeccapurple"
    stroke-width="4"
    stroke-linecap="spherical" />
</svg>

Perfection. Now all that’s left to do is ship it to that particular somebody.

💜

Leave a Reply