Valentine's Day is here, and what better way to show your affection than with a hand-drawn heart? Previously, CSS-Tricks showcased various heart-drawing techniques, inspiring a delightful collection of CodePen submissions. One example is Temani Afif's CSS Shapes site, featuring a stylish heart created solely with CSS.
For a more personal touch, let's craft an SVG heart. No advanced vector software needed! We'll use a simple HTML document and the <svg></svg>
tag:
<svg></svg>
To visualize our creation, we'll use the viewBox
attribute. Think of this as our drawing canvas, defined by coordinates. We'll set a square viewBox:
<svg viewbox="0 0 10 10"></svg>
Now, let's draw! We'll utilize the <path></path>
element and its d
attribute to define our heart's shape using line segments. The key SVG path commands are: MoveTo (M, m), LineTo (L, l, H, h, V, v), Cubic Bézier curves (C, c, S, s), Quadratic Bézier curves (Q, q, T, t), Elliptical arc curves (A, a), and ClosePath (Z, z).
For this heart, we'll focus on MoveTo and LineTo. We start by moving to the coordinates (2,2):
<svg viewbox="0 0 10 10"><path d="M2,2"></path></svg>
Next, we draw a line to (4,4), then to (6,2):
<svg viewbox="0 0 10 10"><path d="M2,2 L4,4 L6,2"></path></svg>
This creates an upside-down triangle. To fix this, let's remove the default fill and add a stroke:
<svg viewbox="0 0 10 10"><path d="M2,2 L4,4 L6,2" fill="none" stroke="rebeccapurple"></path></svg>
Let's increase the stroke-width
for better visibility and add rounded ends using stroke-linecap="round"
:
<svg viewbox="0 0 10 10"><path d="M2,2 L4,4 L6,2" fill="none" stroke="rebeccapurple" stroke-width="4" stroke-linecap="round"></path></svg>
And there you have it – a perfectly imperfect, hand-drawn SVG heart, ready to be shared with someone special! ?
The above is the detailed content of Handwriting an SVG Heart, With Our Hearts. For more information, please follow other related articles on the PHP Chinese website!