Skip to content

Instantly share code, notes, and snippets.

@vicapow
Created June 26, 2013 06:28
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 vicapow/5865200 to your computer and use it in GitHub Desktop.
Save vicapow/5865200 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<title></title>
<style>
body, html {
margin: 0;
overflow: hidden;
}
.block{
position: absolute;
}
</style>
</head>
<body>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script>
var w = window.innerWidth
, h = window.innerHeight
, block = {
big : {
w : 100
, h : 100
}
, small : {
w : 10
, h : 10
}
}
d3.select('body').append('div')
.attr('class','block')
.style({
'width' : block.big.w + 'px'
, 'height' : block.big.h + 'px'
, 'background-color' : 'red'
, '-webkit-transform' : 'translate('
+ ( w / 2 - block.big.w / 2 ) + 'px,'
+ ( h / 2 - block.big.h / 2 ) + 'px'
+ ')'
})
var rows = 10
, cols = 10
d3.select('body').on('click', function(){
d3.select('body').selectAll('.block')
.data(d3.range(rows * cols))
.enter()
.append('div')
.attr('class', 'block')
.style({
'width' : block.big.w + 'px'
, 'height' : block.big.h + 'px'
, 'background-color' : 'white'
, '-webkit-transform' : function(d, i){
var col = d % cols
, row = Math.floor( d / cols )
, bw = block.big.w
, bh = block.big.h
, x = w / 2 + row * ( bw + 2) - bw / 2
, y = h / 2 + col * ( bh + 2 ) - bh / 2
return 'translate(' + x + 'px,' + y + 'px)'
}
})
.transition()
.duration(1000)
.style({
'width' : block.small.w + 'px'
, 'height' : block.small.h + 'px'
, 'background-color' : 'orange'
, '-webkit-transform' : function(d, i){
var col = d % cols
, row = Math.floor( d / cols )
, bw = block.small.w
, bh = block.small.h
, x = w / 2 + row * ( bw + 2) - bw / 2
, y = h / 2 + col * ( bh + 2 ) - bh / 2
return 'translate(' + x + 'px,' + y + 'px)'
}
})
})
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment