Skip to content

Instantly share code, notes, and snippets.

Last active June 3, 2016 14:41
Show Gist options
  • Save enjalot/65ae9c0fc95337107448 to your computer and use it in GitHub Desktop.
Save enjalot/65ae9c0fc95337107448 to your computer and use it in GitHub Desktop.
matrix: reboot

Matrix: reboot

Remaking with SVG and d3.js

This technical experiment currently lacks the finesse and wistfulness of the original. I'm most inspired by the genius (yet obvious in retrospect) technique of showing both the original point and the transformed point connected by a line. Even the idea to use a letter is brilliant to me: it is familiar and intuitive, it allows for discrete sampling without introducing other concepts like pixels.

My hope is that by going through the exercise of making it more data-driven I can expand on the concept to introduce things like rotation. Perhaps using d3 will also make the code more concise, but that's not certain as the original is relatively short.

I'm still working on porting all of the original interactions, but I'm quite pleased with the work in progress.


I wrote all this code inside building-blocks as a way to "dogfood" it and do an initial usability test. Working with multiple files has certainly exposed some gaps, but it was overall quite a pleasant experience.

<!DOCTYPE html>
<meta charset="utf-8">
<script src=""></script>
<link rel="stylesheet" type="text/css" href="style.css">
<script src="scrubbing.js"></script>
<svg width=960 height=250></svg>
<div id="math">
<div id="mtx_transform" class="matrix unselectable" style="width:180px">
<input value="1.5"/><input value="0.0"/><input value="0.0"/>
<input value="0.0"/><input value="1.0"/><input value="0.0"/>
<div plain style="position: absolute; top: 120px;">0</div>
<div plain style="position: absolute; top: 120px; left:70px">0</div>
<div plain style="position: absolute; top: 120px; left:130px">1</div>
<div class="label">
the transformation matrix
<span>(adjust the numbers!)</span>
<div id="mtx_input" class="matrix unselectable" style="width:60px">
<div plain style="position: absolute; top: 0px;">x</div>
<div plain style="position: absolute; top: 60px;">y</div>
<div plain style="position: absolute; top: 120px;">1</div>
<div class="label">
a vector
<span>(hover over the dots)</span>
<div class="equals"></div>
<div id="mtx_expanded" class="matrix unselectable" expanded>
<div><span class="left">1</span>*<span class="right">1</span></div>
<div plus><span class="left">1</span>*<span class="right">1</span></div>
<div plus><span class="left">1</span>*<span class="right">1</span></div>
<div><span class="left">0</span>*<span class="right">0</span></div>
<div plus><span class="left">0</span>*<span class="right">0</span></div>
<div plus><span class="left">0</span>*<span class="right">0</span></div>
<div><span class="left">0</span>*<span class="right">x</span></div>
<div plus><span class="left">0</span>*<span class="right">y</span></div>
<div plus><span class="left">1</span>*<span class="right">1</span></div>
<div class="label">
how to multiply transformation matrix &amp; vector
<span>(hover over each cell)</span>
<div class="equals"></div>
<div id="mtx_output" class="matrix" style="width:60px">
<div class="label">
new vector
<span>(hover over the dots)</span>
var transform = {}; // global transform
var t = transform; // convenience
var bullets = []; // global data
var mtx_inputs = document.querySelectorAll("#mtx_input div");
var mtx_outputs = document.querySelectorAll("#mtx_output div");
var mtx_expanded_left = document.querySelectorAll("#mtx_expanded span.left");
var mtx_expanded_right = document.querySelectorAll("#mtx_expanded span.right");
var mtx_expanded = document.querySelectorAll("#mtx_expanded")[0];
var mtx_transforms = document.querySelectorAll("#mtx_transform input");
function calculate(x,y){
x = x || 0;
y = y || 0;
var x2 = t.a*x + t.b*y + t.tx;
var y2 = t.c*x + t.d*y + t.ty;
return {x:x2, y:y2};
function render() {
var xscale = d3.scale.linear()
.domain([-1, 1])
.range([250, 610]);
var yscale = d3.scale.linear()
.domain([-1, 1])
.range([200, 50])
var transformed = {
return calculate(d.x, d.y)
var svg ="svg");
function hover(d,i) {
mtx_inputs[0].innerHTML = bullets[i].x.toFixed(1);
mtx_inputs[1].innerHTML = bullets[i].y.toFixed(1);[0]).style("border", "3px solid red");[1]).style("border", "3px solid red");
function filter(f,j) { return j === i }
.filter(filter).style("stroke", "red")
.filter(filter).style("stroke", "red")
.filter(filter).style({stroke:"red", fill:"red"})
function mouseout(d,i) {
mtx_inputs[0].innerHTML = "x";
mtx_inputs[1].innerHTML = "y";[0]).style("border", "3px solid #eee");[1]).style("border", "3px solid #eee");
d3.selectAll("line").style("stroke", "#111");
d3.selectAll("circle.bullet").style("stroke", "#111")
d3.selectAll("circle.transformed").style({stroke:"#111", fill:"#111"})
var lines = svg.selectAll("line")
.on("mouseover", hover)
.on("mouseout", mouseout)
x1: function(d,i) { return xscale(d.x) },
y1: function(d,i) { return yscale(d.y) },
x2: function(d,i) { return xscale(transformed[i].x)},
y2: function(d,i) { return yscale(transformed[i].y)},
stroke: "#111"
var circlesB = svg.selectAll("circle.bullet")
circlesB.enter().append("circle").classed("bullet", true)
.on("mouseover", hover)
.on("mouseout", mouseout)
r: 4,
fill: "none",
stroke: "#111"
cx: function(d) { return xscale(d.x) },
cy: function(d) { return yscale(d.y) },
var circlesT = svg.selectAll("circle.transformed")
circlesT.enter().append("circle").classed("transformed", true)
.on("mouseover", hover)
.on("mouseout", mouseout)
r: 8,
fill: "#111",
stroke: "#111"
cx: function(d) { return xscale(d.x) },
cy: function(d) { return yscale(d.y) },
function updateMatrixLeft() {
for(var i=0;i<6;i++){
var m = mtx_expanded_left[i];
var t = mtx_transforms[i];
m.innerHTML = t.value;
transform.a = parseFloat(mtx_transforms[0].value) || 0;
transform.b = parseFloat(mtx_transforms[1].value) || 0;
transform.tx = parseFloat(mtx_transforms[2].value) || 0;
transform.c = parseFloat(mtx_transforms[3].value) || 0;
transform.d = parseFloat(mtx_transforms[4].value) || 0;
transform.ty = parseFloat(mtx_transforms[5].value) || 0;
for(var i=0;i<mtx_transforms.length;i++){
var input = mtx_transforms[i];
input.onchange = updateMatrixLeft;
// get the data and trigger the initial rendering
d3.json("bullets.json", function(err, data) {
bullets = {
return {x: d.x, y: d.y};
// Make inputs scrubbable
var Mouse = {};
var scrubInput = null;
var scrubPosition = {x:0, y:0};
var scrubStartValue = 0;
var scrubId;
function makeScrubbable(input){
input.onmousedown = function(e){
scrubInput =;
scrubPosition.x = e.clientX;
scrubPosition.y = e.clientY;
scrubStartValue = parseFloat(input.value);
input.onclick = function(e){;
function setupScrubbing(cb) {
window.onmousemove = function(e){
// Mouse
Mouse.x = e.clientX;
Mouse.y = e.clientY;
// Scrubbing
if(!scrubInput) return;
var deltaX = e.clientX - scrubPosition.x;
deltaX = Math.round(deltaX/10)*0.1; // 0.1 for every 10px
var val = scrubStartValue + deltaX;
scrubInput.value = (Math.round(val*10)/10).toFixed(1);
scrubId = null;
window.onmouseup = function(){
scrubInput = null;
body { margin: 0; overflow-x: none; }
svg {
background-color: #cccccc;
width: 960px;
height: 220px;
margin: 0px auto;
margin-top: -5px;
font-family: monospace;
/** HACK **/
-webkit-transform: scale(0.9);
-moz-transform: scale(0.9);
-ms-transform: scale(0.9);
transform: scale(0.9);
.matrix, .equals{
position: relative;
float: left;
padding: 0 10px;
.matrix > input, .matrix > div{
float:left; margin:5px; position: relative;
width:50px; height:50px;
font-size: 15px; line-height: 50px; text-align: center;
background: #eee
.matrix > input{
border: 2px solid #bbb;
display: block;
width:44px; height:44px;
font-size: 15px;
font-family: monospace; cursor: col-resize;
.matrix[expanded] > div{
position: relative;
width:80px; margin:5px 10px;
font-size: 12px; cursor: pointer;
.matrix[expanded] > div[plus]:before{
content: '+';
position: absolute; left: -16px;
font-size: 20px; text-align: center;
width:0px; height:0px;
color: #000;
.matrix:before, .matrix:after{
width:20px; height:190px;
border: 5px solid #000;
.matrix[highlight=yes]:before, .matrix[highlight=yes]:after{
border-color: #DD3838;
border-right: none;
border-left: none;
content: '';
position: absolute; margin:auto;
top:0; bottom:0; left:0; right:0;
border: 5px solid #000;border-left: none;border-right: none;
.matrix > .label, .matrix[expanded] > .label{
font-size: 15px;float: none;background: none;
width: 100%;
position: absolute;margin: 0;
top: 195px;left: 0px;
line-height: 20px;font-family: Helvetica, Arial, sans-serif;
.matrix > .label > span{
color: #888;
.matrix > div[plain]{
border: 3px solid #eee;
width: 44px; height: 44px;
-webkit-user-select: none; /* Chrome all / Safari all */
-moz-user-select: none; /* Firefox all */
-ms-user-select: none; /* IE 10+ */
/* No support for these yet, use at own risk */
-o-user-select: none;
user-select: none;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment