Last active
August 15, 2016 05:54
-
-
Save bhvaleri/aa4d7c9a3ea52ec932ab8d1ebce98949 to your computer and use it in GitHub Desktop.
Wall of Clocks 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Wall of Clocks 1</title> | |
<script src="https://d3js.org/d3.v4.min.js"></script> | |
</head> | |
<body> | |
<script> | |
const margin = { top: 0, right: 0, bottom: 0, left: 0 }; | |
const startingWindowWidth = window.innerWidth, | |
startingWindowHeight = window.innerHeight; | |
const width = startingWindowWidth - margin.left - margin.right, | |
height = startingWindowHeight - margin.top - margin.bottom; | |
const svg = d3.select('body').append('svg') | |
.attr('width', width + margin.left + margin.right) | |
.attr('height', height + margin.top + margin.bottom); | |
const clocksGroup = svg.append('g') | |
.attr('transform', `translate(${margin.left},${margin.top})`); | |
// const radius = width / 4; | |
const x1 = 0; | |
const y1 = 0; | |
const strokeWidth = 4; | |
function secondsToLine(seconds, radius) { | |
// each second gets 6 degrees | |
// force clock wise | |
// ask Colin why this works? | |
const angle = -((seconds * (2 * Math.PI) / 60) - (Math.PI / 2)); | |
return { | |
x2: Math.cos(angle) * radius, | |
y2: -(Math.sin(angle) * radius) | |
} | |
} | |
let clockCounter = 0; | |
function addClock(startingSeconds, radius, translateX, translateY) { | |
const clock = clocksGroup.append('g').attr('transform', `translate(${translateX},${translateY})`); | |
clock.append('circle') | |
.attr('cx', x1) | |
.attr('cy', y1) | |
.attr('r', radius) | |
.style('fill', 'none') | |
.style('stroke-width', strokeWidth) | |
.style('stroke', 'black'); | |
const startingCoordinates = secondsToLine(startingSeconds, radius); | |
const secondHandClassName = `second-hand-${clockCounter}`; | |
clockCounter++; | |
const secondHand = clock.append('line') | |
.attr('class', `second-hand ${secondHandClassName}`) | |
.attr('x1', x1) | |
.attr('x2', startingCoordinates.x2) | |
.attr('y1', y1) | |
.attr('y2', startingCoordinates.y2) | |
.style('stroke', 'black') | |
.style('stroke-width', strokeWidth); | |
let secondCount = startingSeconds; | |
setInterval(() => { | |
secondCount = (secondCount + 1) % 60; | |
const newCoordinates = secondsToLine(secondCount, radius); | |
d3.select(`.${secondHandClassName}`) | |
.transition() | |
.attr('x2', newCoordinates.x2) | |
.attr('y2', newCoordinates.y2); | |
}, 100) | |
}; | |
const circleRadius = 50; | |
// lets create a grid! | |
const gridDimensions = { | |
m: Math.floor(width / (circleRadius * 2)), | |
n: Math.floor(height / (circleRadius * 2)) | |
} | |
let startingTime = 0; | |
for(let i = 0; i < gridDimensions.m; i++) { | |
for(let j = 0; j < gridDimensions.n; j++) { | |
addClock(startingTime++, circleRadius, (circleRadius*2*i) + circleRadius, (circleRadius*2*j) + circleRadius); | |
} | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment