Skip to content

Instantly share code, notes, and snippets.

@enthal
Last active November 14, 2020 02:55
Show Gist options
  • Select an option

  • Save enthal/3a7e4923c274cb678f60 to your computer and use it in GitHub Desktop.

Select an option

Save enthal/3a7e4923c274cb678f60 to your computer and use it in GitHub Desktop.
D3/SVG "finger" articulation: https://bl.ocks.org/enthal/3a7e4923c274cb678f60
<!DOCTYPE html>
<meta charset="utf-8">
<style>
svg {
fill: none;
stroke: #000;
stroke-width: 4px;
}
svg rect.mouse {
fill: none;
stroke: none;
pointer-events: all;
}
svg .hand > line {
stroke: green;
}
</style>
<body></body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js" charset="utf-8"></script>
<script>
var width = 960, height = 500;
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var g0 = svg.append("g")
.attr("transform", "translate(100,200)")
.append("g");
g0.append("line")
.attr("x2", 200);
var wrist = g0.append("g")
.attr("transform", "translate(200)")
.append("g");
var hands = [];
var knuckles = [];
function addHand(length) {
var hand = wrist.append("g").classed("hand", true);
hands.push(hand);
hand.append("line")
.attr("x2", length);
var knuckle = hand.append("g")
.attr("transform", "translate("+length+")")
.append("g")
knuckles.push(knuckle);
var finger = knuckle.append("g");
finger.append("line")
.attr("x2", 50);
}
addHand(100);
addHand(120);
addHand(110);
svg.append("rect").classed("mouse", true)
.attr("width", width)
.attr("height", height)
.on("click", move);
move();
function move() {
g0
.attr("transform", "rotate(0)")
.transition() .duration(900) .attr("transform", "rotate(-25)") .ease("ease-in")
.transition() .duration(600) .attr("transform", "rotate(-22)")
.transition() .duration(600) .attr("transform", "rotate(-10)") .ease("ease-out");
wrist
.attr("transform", "rotate(-15)")
.transition() .duration( 900) .attr("transform", "rotate(50)") .ease("ease-out")
.transition() .duration(1400) .attr("transform", "rotate(70)");
hands[1]
.attr("transform", "rotate(-30)")
.transition() .duration(1800) .attr("transform", "rotate(-20)") .ease("linear");
hands[2]
.attr("transform", "rotate(-50)")
.transition() .duration(1800) .attr("transform", "rotate(-35)") .ease("linear");
knuckles[0]
.attr("transform", "rotate(20)")
.transition() .duration(1900) .attr("transform", "rotate(100)") .ease("ease-out");
knuckles[1]
.attr("transform", "rotate(15)")
.transition() .duration(1800) .attr("transform", "rotate(60)") .ease("ease-out");
knuckles[2]
.attr("transform", "rotate(15)")
.transition() .duration(1700) .attr("transform", "rotate(40)") .ease("ease-out");
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment