Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Fast prototyping tricks - Create reproducible elements

View README.md
View README.md
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg width="400px" height="800px" viewBox="0 0 400 800" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:sketch="http://www.bohemiancoding.com/sketch/ns">
<title>Trick - Reproducible elements</title>
<description>Created with Sketch (http://www.bohemiancoding.com/sketch)</description>
<defs>
<rect id="path-1" x="0" y="0" width="360" height="580"></rect>
<mask id="mask-2" sketch:name="Mask" fill="white">
<use xlink:href="#path-1"></use>
</mask>
</defs>
<g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd" sketch:type="MSPage">
<g id="Trick:-Reproducible-element" sketch:type="MSLayerGroup">
<rect id="iPhone" fill="#000000" sketch:type="MSShapeGroup" x="0" y="0" width="400" height="800" rx="50"></rect>
<circle id="HomeButton" fill="#202020" sketch:type="MSShapeGroup" cx="200" cy="720" r="40"></circle>
<g id="ScreenGroup" transform="translate(20.000000, 80.000000)">
<use id="Mask" sketch:type="MSShapeGroup" xlink:href="#path-1"></use>
<g id="ScreenContent" mask="url(#mask-2)">
<rect id="ScreenBackground" fill="#2B2B2B" sketch:type="MSShapeGroup" x="0" y="0" width="360" height="580"></rect>
<g id="Element" transform="translate(13.000000, 22.000000)">
<rect id="ElementBackground" fill="#3498DB" sketch:type="MSShapeGroup" x="0" y="0" width="335" height="88" rx="10"></rect>
<circle id="Picture" fill="#E74C3C" sketch:type="MSShapeGroup" cx="39" cy="44" r="28"></circle>
<path d="M301.170525,30.3378906 L315.131468,43.7877177 L301.170525,58.8476562" id="Arrow" stroke="#FFFFFF" stroke-width="8" stroke-linecap="round" sketch:type="MSShapeGroup"></path>
<text id="Title" sketch:type="MSTextLayer" font-family="Helvetica Neue" font-size="28" font-weight="bold" fill="#FFFFFF">
<tspan x="83" y="39">Song name</tspan>
</text>
<text id="By" opacity="0.4" sketch:type="MSTextLayer" font-family="Helvetica Neue" font-size="20" font-weight="bold" sketch:alignment="right" fill="#FFFFFF">
<tspan x="83" y="69">BY:</tspan>
</text>
<text id="Artist" sketch:type="MSTextLayer" font-family="Helvetica Neue" font-size="20" font-weight="bold" fill="#FFFFFF">
<tspan x="123" y="69">ARTIST NAME</tspan>
</text>
</g>
</g>
</g>
</g>
</g>
</svg>
View README.md
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
<!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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.