html5 - HTML 5 draw curved text on a image -
how can draw curved text on image in particular position below.
you can use <textpath>
svg elements draw text on path. can little bit fiddly, i'll walk through it.
first need create <defs>
section containing paths want text follow. make things easier if use <circle>
element, won't work; has <path>
element. fortunately, svg's path drawing support circular arcs, there's no need faff bézier curves.
here i'm defining 2 semicircular arcs centred on (0,0) radius of 120; cp1
goes clockwise on top, , cp2
goes anticlockwise around bottom:
<defs> <path id="cp1" d="m-120 0a120 120 0 0 1 120 0" fill="none" stroke="red" /> <path id="cp2" d="m-120 0a120 120 0 0 0 120 0" fill="none" stroke="red" /> </defs>
the actual drawing part of svg wrapped in <g>
element move origin centre of svg (which has dimensions of 300×300 in case):
<g transform="translate(150,150)">
now create <text>
element required appearance attributes, , put <textpath>
elements inside it. use text-anchor="middle"
keep text centrally justified, , set startoffset
attribute distance along path want text anchored. in case, offset equal 1 quarter of circumference of circle diameter 120, 2 × π × 120 / 4, or 188.5. dominant-baseline
attribute indicates how text aligned curve; setting of middle
means centre line of text aligned path, can use same radius upper , lower curves. (in cases can make text bit squished. can fix setting letter-spacing
attribute.)
<text font-size="36" font-family="courier new" font-weight="bold" fill="white"> <textpath xlink:href="#cp1" startoffset="188.5" text-anchor="middle" dominant-baseline="central">smooth roast</textpath> <textpath xlink:href="#cp2" startoffset="188.5" text-anchor="middle" dominant-baseline="central">coffee company</textpath> </text>
the horizontal text in middle of logo can done in same way, using ordinary <tspan>
elements instead of <textpath>
elements:
<text font-size="12" font-family="arial" font-weight="bold" fill="#c97"> <tspan x="-120" y="0" text-anchor="middle" dominant-baseline="central">since</tspan> <tspan x="120" y="0" text-anchor="middle" dominant-baseline="central">1981</tspan> </text>
and here's finished result:
<svg width="300" height="300" viewbox="0 0 300 300" xmlns:xlink="http://www.w3.org/1999/xlink"> <defs> <path id="cp1" d="m-120 0a120 120 0 0 1 120 0" fill="none" stroke="red" /> <path id="cp2" d="m-120 0a120 120 0 0 0 120 0" fill="none" stroke="red" /> </defs> <g transform="translate(150,150)"> <!-- circular background: --> <circle x="0" y="0" r="150" fill="#820" stroke="none" /> <circle x="0" y="0" r="145" fill="none" stroke="white" stroke-width="4" /> <circle x="0" y="0" r="98" fill="none" stroke="white" stroke-width="2" /> <!-- supposed coffee cup: --> <rect x="-45" y="-40" width="80" height="80" fill="white" stroke="none" /> <rect x="-45" y="30" width="80" height="20" rx="10" ry="10" fill="white" stroke="none" /> <ellipse cx="45" cy="0" rx="15" ry="20" fill="none" stroke="white" stroke-width="8" /> <!-- text manipulation starts here: --> <text font-size="36" font-family="courier new" font-weight="bold" fill="white"> <textpath xlink:href="#cp1" startoffset="188.5" text-anchor="middle" dominant-baseline="central">smooth roast</textpath> <textpath xlink:href="#cp2" startoffset="188.5" text-anchor="middle" dominant-baseline="central">coffee company</textpath> </text> <text font-size="12" font-family="arial" font-weight="bold" fill="#c97"> <tspan x="-120" y="0" text-anchor="middle" dominant-baseline="central">since</tspan> <tspan x="120" y="0" text-anchor="middle" dominant-baseline="central">1981</tspan> </text> </g> </svg>
Comments
Post a Comment