2D Matrix Demo
by dchudz
HTML
<script src="http://mbostock.github.com/d3/d3.js?2.7.1"></script>
<div class = matrixformd>
<form>
<div align="left">
Transformation:<br>
<input class = "matrix" type="text" size="3" value="1" id="a"><input class = "matrix" type="text" size="3" value="0" id="b"><br>
<input class = "matrix" type="text" size="3" value="0" id="c"><input class = "matrix" type="text" size="3" value="1" id="d">
<a href="#" id="reset">Reset</a>
</div>
</form>
</div>
<div id="plane1div" class="planediv"></div>
<div id="plane2div" class="planediv"></div>
CSS
body {
font: 16px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.overlay {
fill: "black";
opacity: .5;
pointer-events: all;
}
.dot {
stroke: #000;
}
.planediv {
width:50%;
float:left;
height:1000px;
}
JavaScript
var margin = {top: 20, right: 20, bottom: 20, left: 20},
width = 800 - margin.left - margin.right,
height = 800 - margin.top - margin.bottom;
var x = d3.scale.linear()
.range([0, width]).domain([-10,10]);
var y = d3.scale.linear()
.range([height, 0]);
function makePlane(addTo, offsetX, offsetY) {
var color = d3.scale.category10();
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
marginLeft = margin.left + offsetX;
marginTop = margin.top + offsetY;
var svg = d3.select(addTo).append("svg")
.append("g")
.attr("transform", "translate(" + marginLeft + "," + marginTop + ")");
x.domain([-10,10]).nice();
y.domain([-10,10]).nice();
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis).attr("transform", "translate(0," + height/2 + ")")
.append("text")
.attr("class", "label")
.attr("x", width)
.attr("y", -6)
.style("text-anchor", "end")
.text("x");
svg.append("g")
.attr("class", "y axis")
.call(yAxis).attr("transform", "translate(" + width/2 + ",0)")
.append("text")
.attr("class", "label")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("y")
return svg
};
function mouseclick() {
xClick = x.invert(d3.mouse(this)[0]);
yClick = y.invert(d3.mouse(this)[1]);
point = [xClick, yClick];
addPoint(point);
}
function drag() {
xClick = x.invert(d3.event.x); //d3.mouse(this)[0]);
yClick = y.invert(d3.event.y); //d3.mouse(this)[1]);
point = [xClick, yClick];
addPoint(point);
}
function reset() {
d3.selectAll(".dot").remove();
points=[];
}
function multiply(M, point) {
var Mx = M.a*point[0] + M.b*point[1];
var My = M.c*point[0] + M.d*point[1];
return [Mx, My];
}
function addPoint(point) {
point1 =...