Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added factor and modulo controls, added svg download button #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 70 additions & 5 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Snap from 'snapsvg'
import css from './s.css'
import mp3 from './art.mp3'

const modulo = 200

const padding = 20
const radius = (Math.min(window.innerHeight, window.innerWidth) / 2 - padding)
const circleCenterX = radius + (padding / 2)
Expand All @@ -27,6 +27,8 @@ const joinTwoNumbers = (a, b) => {

surface.line(aX, aY, bX, bY).attr({
stroke: `hsl(${color}, 100%, 50%)`,
// stroke: `black`,
opacity: 0.5,
strokeWidth: 1,
strokeLinecap: 'round'
})
Expand All @@ -41,16 +43,79 @@ const getCoordForNumber = (number) => {
return [x, y]
}

let factor = 1
let factor = getParameterByName("f") || 1
let modulo = getParameterByName("m") || 200
document.querySelector('#factor').value = factor * 1.0;
document.querySelector('#modulo').value = modulo * 1.0;

const draw = () => {

const _draw = () => {
surface.clear()
for (let i = 0; i < modulo; i++) {
joinTwoNumbers(i, i * factor % modulo)
}
// const [intPart, decimal] = `${factor}`.split('.')
// surface.text(5, 15, `Factor: ${intPart}${decimal ? '.' + decimal.charAt(0) : ''}`)
factor += 0.005
document.querySelector('#ratio').innerHTML = modulo/factor;

}

const draw = () => {
_draw();
factor *= 1.0
factor += 0.01
document.querySelector('#factor').value = factor;

}


function getParameterByName(name, url = window.location.href) {
name = name.replace(/[\[\]]/g, '\\$&');
var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, ' '));
}


function downloadSVGAsText() {
const svg = document.querySelector('svg');
const base64doc = btoa(unescape(encodeURIComponent(svg.outerHTML)));
const a = document.createElement('a');
const e = new MouseEvent('click');
a.download = factor + "-" + modulo + '.svg';
a.href = 'data:image/svg+xml;base64,' + base64doc;
a.dispatchEvent(e);
}


let intervalId;

function pause() {
clearInterval(intervalId);
}

function play() {
intervalId = setInterval(() => Promise.resolve().then(draw), 5);
}

setInterval(() => Promise.resolve().then(draw), 100)
_draw();

function updateFactor() {
factor = document.querySelector('#factor').value * 1.0;
_draw();
}

function updateModulo() {
modulo = document.querySelector('#modulo').value * 1.0;
_draw();
}

const downloadSVG = document.querySelector('#downloadSVG');
downloadSVG.addEventListener('click', downloadSVGAsText);
document.querySelector('#play').addEventListener('click', play);
document.querySelector('#pause').addEventListener('click', pause);

document.querySelector('#factor').addEventListener('change', updateFactor);
document.querySelector('#modulo').addEventListener('change', updateModulo);
16 changes: 14 additions & 2 deletions index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,20 @@
</head>
<body>
<a class="i" href="https://github.com/22a/cardioid#cardioid" target="_blank"> ℹ️ </a>
<audio autoplay loop>
<!--<audio autoplay loop>
<source src="3" type="audio/ogg">
</audio>
</audio>-->
<p>
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811
</p>
<button id="downloadSVG">Download .svg</button>

<button id="play">Play</button>
<button id="pause">Pause</button>

<input type="number" id="factor" value="2" step="1">
<input type="number" id="modulo" value="200" step="1">

<div id="ratio"></div>
</body>
</html>