rotation - How to rotate child "bones" in a dog's tail in SVG? -
i'm overwhelmed approaches , tools svg, , find super simple way position , draw stick-figure dog's "tail", consisting of maybe 5 "bones". each of line or rectangle now. i'd each child bone have settable rotation parent bone. think of five-segment snake.
for example, if bones' rotations 10,10,10,10,10 tail curves smoothly down, circle. if rotations -20,-10,0,10,20 tail has "s"-curve shape it. i'd love have static page this, ideally i'd have kind of textbox/slider each of 5 bones, or @ least example of how reference , tweak bones via javascript. there simple approach can make tail? i'll use library if there's 1 adds value case, simple/small preferred. thanks!
yes, make each bone "child" of previous one.
here's example svg. make snippet full screen see inputs.
function adjusttail(evt) { var name = evt.target.getattribute('name'); var val = evt.target.value; console.log("name="+name+" val="+val); var xform = (name=="s1") ? "" : "translate(50,0) "; xform += "rotate(" + val + ")"; document.getelementbyid(name).setattribute("transform", xform); return false; } document.getelementbyid("i1").addeventlistener("change", adjusttail); document.getelementbyid("i2").addeventlistener("change", adjusttail); document.getelementbyid("i3").addeventlistener("change", adjusttail); document.getelementbyid("i4").addeventlistener("change", adjusttail); document.getelementbyid("i5").addeventlistener("change", adjusttail);
input { width: 3em; }
<svg width="500" height="300"> <g id="tail1" stroke="black" stroke-width="10" fill="none" transform="translate(100,150)"> <g id="s1" transform="rotate(-20)"> <line x1="0" y1="0" x2="50" y2="0"/> <g id="s2" transform="translate(50,0) rotate(-10)"> <line x1="0" y1="0" x2="50" y2="0"/> <g id="s3" transform="translate(50,0) rotate(0)"> <line x1="0" y1="0" x2="50" y2="0"/> <g id="s4" transform="translate(50,0) rotate(10)"> <line x1="0" y1="0" x2="50" y2="0"/> <g id="s5" transform="translate(50,0) rotate(20)"> <line x1="0" y1="0" x2="50" y2="0"/> </g> </g> </g> </g> </g> </g> </svg> <div> <input id="i1" type="text" name="s1" value="-20"> <input id="i2" type="text" name="s2" value="-10"> <input id="i3" type="text" name="s3" value="0"> <input id="i4" type="text" name="s4" value="10"> <input id="i5" type="text" name="s5" value="20"> </div>
Comments
Post a Comment