Skip to content

Instantly share code, notes, and snippets.

@zanarmstrong
Last active September 8, 2015 03:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zanarmstrong/e2d22ae47d24179b574c to your computer and use it in GitHub Desktop.
Save zanarmstrong/e2d22ae47d24179b574c to your computer and use it in GitHub Desktop.
jittery line (original)
body {
background-color: #323232;
}
p {
color: white;
font-family: 'Raleway', sans-serif;
}
.mainPath {
stroke: white;
stroke-width: 3;
fill: none;
}
"use strict";
// standard parameters
var margin = {
top: 20,
right: 0,
bottom: 0,
left: 0
},
width = 1400 - margin.left - margin.right,
height = 500 - margin.bottom - margin.top;
// new parameters
var points = [];
var jitter = 12;
// define the function for the line with jitter
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');
// standard svg intro + mousemove
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));
});
// update line with new points
function update() {
svg.selectAll("path").remove();
svg.append('path')
.attr("d", lineFunction(points))
.attr("class", "mainPath");
}
// update point array
function tick(coord) {
points.push({
x: coord[0],
y: coord[1]
});
if (points.length > 40) {
points.shift()
}
update()
};
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Jitter Line</title>
<link href='http://fonts.googleapis.com/css?family=Raleway' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="animatedMouseMove.css">
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body>
<p>Use your mouse to sketch a (jittery) line.</p>
<section id="box" class="main">
</section>
<!-- call JS files -->
<script src="animatedMouseMove.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment