Skip to content

Instantly share code, notes, and snippets.

@illustratordavid
Created December 4, 2014 14:40
Show Gist options
  • Select an option

  • Save illustratordavid/8120cf858bc09590f97f to your computer and use it in GitHub Desktop.

Select an option

Save illustratordavid/8120cf858bc09590f97f to your computer and use it in GitHub Desktop.
clone matrix, width, height, x and y of object http://jsbin.com/copayu
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>JS Bin</title>
<script src="http://snapsvg.io/assets/js/snap.svg-min.js"></script>
<script src="http://snapsvg.io/assets/js/prism.js"></script>
</head>
<body>
<div id="svg" width="400" height="400"></div>
<button onclick="clickrotate()">rotate</button>
<script id="jsbin-javascript">
var s = Snap(400,400);
s.attr('viewBox',"0 0 800,800");
var smallSquare = s.rect(20,20,199,150).attr({fill:"#ff6600"});
var cloneSquare = s.rect(0,0,20,20).attr({fill:"#99CC00", opacity:0.5});
var t = new Snap.matrix();
function clickrotate(){
t.rotate(45,smallSquare.getBBox().cx,
smallSquare.getBBox().cy);
smallSquare.transform(t);
// clone
cloneObject(smallSquare,cloneSquare);
setTimeout(clickrotate,1000);
}
function cloneObject(orig,clone) {
var m = orig.transform().localMatrix.clone();
clone.attr({'x':orig.attr('x'),
'y':orig.attr('y'),
'width':orig.attr('width'),
'height':orig.attr('height')
});
clone.transform(m);
}
$(function(){
clickrotate();
});
</script>
<script id="jsbin-source-javascript" type="text/javascript">var s = Snap(400,400);
s.attr('viewBox',"0 0 800,800");
var smallSquare = s.rect(20,20,199,150).attr({fill:"#ff6600"});
var cloneSquare = s.rect(0,0,20,20).attr({fill:"#99CC00", opacity:0.5});
var t = new Snap.matrix();
function clickrotate(){
t.rotate(45,smallSquare.getBBox().cx,
smallSquare.getBBox().cy);
smallSquare.transform(t);
// clone
cloneObject(smallSquare,cloneSquare);
setTimeout(clickrotate,1000);
}
function cloneObject(orig,clone) {
var m = orig.transform().localMatrix.clone();
clone.attr({'x':orig.attr('x'),
'y':orig.attr('y'),
'width':orig.attr('width'),
'height':orig.attr('height')
});
clone.transform(m);
}
$(function(){
clickrotate();
});</script></body>
</html>
var s = Snap(400,400);
s.attr('viewBox',"0 0 800,800");
var smallSquare = s.rect(20,20,199,150).attr({fill:"#ff6600"});
var cloneSquare = s.rect(0,0,20,20).attr({fill:"#99CC00", opacity:0.5});
var t = new Snap.matrix();
function clickrotate(){
t.rotate(45,smallSquare.getBBox().cx,
smallSquare.getBBox().cy);
smallSquare.transform(t);
// clone
cloneObject(smallSquare,cloneSquare);
setTimeout(clickrotate,1000);
}
function cloneObject(orig,clone) {
var m = orig.transform().localMatrix.clone();
clone.attr({'x':orig.attr('x'),
'y':orig.attr('y'),
'width':orig.attr('width'),
'height':orig.attr('height')
});
clone.transform(m);
}
$(function(){
clickrotate();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment