Skip to content

Instantly share code, notes, and snippets.

@maelp
Last active August 29, 2015 14:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save maelp/11516401 to your computer and use it in GitHub Desktop.
Save maelp/11516401 to your computer and use it in GitHub Desktop.
Fast prototyping tricks - Create reproducible elements
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
width: 960px;
padding-top: 40px;
margin: auto;
position: relative;
}
svg {
width: 100%;
max-height: 400px;
}
</style>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script type='text/javascript'>
var songs = [
{
title: 'The past is a...',
artist: 'Of Montreal'
},
{
title: 'She came in...',
artist: 'Miley Cyrus'
},
{
title: 'Robotnik is...',
artist: 'Hitboxx'
},
{
title: 'Young',
artist: 'Chevalier Avant-...'
}];
var colors = d3.scale.category10();
function getElementTranslation(el) {
return d3.transform(el.attr('transform')).translate;
}
function newElement(parent, template, song, color) {
var _el = template.node().cloneNode(true);
var el = d3.select(_el);
el.select('#Picture').attr('fill', color);
el.select('#Title tspan').text(song.title);
el.select('#Artist tspan').text(song.artist);
parent.node().insertBefore(_el);
return el;
}
window.addEventListener('load', function () {
d3.select('svg').remove();
d3.xml('iPhone.svg', 'image/svg+xml', function (error, data) {
d3.select('body').node().appendChild(data.documentElement);
var svg = d3.select('svg')
.style('width', '100%');
var appScreen = svg.select('#ScreenBackground');
var screenWidth = +appScreen.attr('width'),
screenHeight = +appScreen.attr('height');
var screenContent = svg.select('#ScreenContent');
var tplElement = svg.select('#Element');
var tplHeight = +tplElement.select('#ElementBackground').attr('height');
var tplOrigTransform = getElementTranslation(tplElement);
var padding = 10;
var onClick = function () {
var el = d3.select(this);
var x = el.node().origPos[0],
y = el.node().origPos[1];
el.transition()
.ease('sin')
.attr('transform', 'translate(' + (x + 50) + ', ' + y + ')')
.transition()
.attr('transform', 'translate(' + x + ', ' + y + ')');
};
for(var i = 0 ; i < songs.length ; i++)
{
var el = newElement(screenContent, tplElement, songs[i], colors(i));
var x = tplOrigTransform[0];
var y = tplOrigTransform[1] + (tplHeight + padding) * i;
el.node().origPos = [x, y];
el
.attr('transform', 'translate(' + (x + 100) + ', ' + y +')')
.style('cursor', 'pointer')
.style('opacity', 0.0)
.on('click', onClick);
el
.transition()
.delay(i * 500)
.duration(1000)
.attr('transform', 'translate(' + x + ', ' + y +')')
.style('opacity', 1.0);
}
tplElement.style('display', 'none');
});
});
</script>
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment