index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Jitter Line</title>
<link href='//fonts.googleapis.com/css?family=Raleway' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="animatedMouseMove.css">
<script src="//d3js.org/d3.v3.min.js" charset="utf-8"></script>
</head>
<body>
<p>Use your mouse to sketch a (jittery) line.</p>
<section id="box" class="main">
</section>
<script src="animatedMouseMove.js"></script>
</body>
</html>
animatedMouseMove.js
"use strict";
var margin = {
top: 20,
right: 0,
bottom: 0,
left: 0
},
width = 1400 - margin.left - margin.right,
height = 500 - margin.bottom - margin.top;
var points = [];
var jitter = 12;
var lineFunction = d3.svg.line()
.x(function(d) {
return d.x + Math.random() * jitter - jitter/2 - margin.left;
console.log('x here')
})
.y(function(d) {
return d.y + Math.random() * jitter - jitter/2 - margin.top;
})
.interpolate('basis');
var svg = d3.select(".main").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.on("mousemove", function() {
tick(d3.mouse(this));
});
function update() {
svg.selectAll("path").remove();
svg.append('path')
.attr("d", lineFunction(points))
.attr("class", "mainPath");
}
function tick(coord) {
points.push({
x: coord[0],
y: coord[1]
});
if (points.length > 40) {
points.shift()
}
update()
};