Skip to content

Instantly share code, notes, and snippets.

@araguaci
Last active April 30, 2024 05:12
Show Gist options
  • Save araguaci/6863b19ee30c2d43bd715a71b4f8e411 to your computer and use it in GitHub Desktop.
Save araguaci/6863b19ee30c2d43bd715a71b4f8e411 to your computer and use it in GitHub Desktop.
Low Poly Art Generator
<!--
https://github.com/evansque/polygonize
-->
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<div class="container">
<header id="header">
<div class="cell-size-value-wrapper">
<span>Cell size:</span>
<span id="cell-size-value">30</span>
</div>
<input type="range" id="cell-size" min="10" max="100" value="30" onchange="updateVisibleValue()">
<label for="image-file" class="button">
<i class="fa fa-upload"></i>
Upload an image
</label>
<input type="file" id="image-file" onchange="readURL(this)">
</header>
<div id="canvas">
Just add a picture and see the magic
</div>
</div>
<div id="loading" class="loading">Processing...</div>
// https://github.com/evansque/polygonize
let src = '';
function updateVisibleValue() {
document.getElementById('cell-size-value').innerText = document.getElementById('cell-size').value;
if (src) {
poly()
}
}
function readURL(input) {
if (input.files && input.files[0]) {
const reader = new FileReader();
reader.onload = event => {
src = event.target.result;
poly();
document.getElementById('image-file').value = '';
};
reader.readAsDataURL(input.files[0]);
}
}
function poly() {
loading(true);
Polygonize({
src: src,
cellSize: parseInt(document.getElementById('cell-size').value),
progress: function (progress) {
// console.log(progress + '%');
},
onSuccess: function(canvas) {
const canvasContainer = document.getElementById('canvas')
canvasContainer.innerHTML = '';
canvasContainer.appendChild(canvas);
loading(false);
}
});
}
function loading(show) {
let loading = document.getElementById('loading');
loading.style.display = show ? "flex" : "none";
}
<script src="https://cdn.rawgit.com/AymanJitan/polygonize/master/dist/polygonize.js"></script>
<script src="https://cdn.rawgit.com/ironwallaby/delaunay/master/delaunay.js"></script>
* {
margin: 0;
padding: 0;
}
html,
body,
.container {
height: 100%;
font-family: Arial, Helvetica, sans-serif;
}
input[type="file"] {
display: none;
}
.button {
background: #1abc9c;
color: #fff;
float: right;
margin: 10px 10px 10px 0px;
padding: 13px 18px;
font-size: 12px;
border-radius: 3px;
cursor: pointer;
}
.button:hover {
background: #18ac8e;
}
.container {
display: flex;
flex-direction: column;
flex-wrap: nowrap;
justify-content: center;
align-content: stretch;
align-items: stretch;
}
.container #header {
height: 60px;
min-height: 60px;
background: #34495e;
z-index: 2;
order: 0;
flex: 0 1 auto;
align-self: auto;
display: flex;
flex-flow: row;
position: fixed;
left:0;
right:0;
top: 0;
}
.container #canvas {
order: 1;
flex: 1 1 auto;
align-self: stretch;
display: flex;
justify-content: center;
align-items: center;
color: #bbb;
font-size: 24px;
padding: 80px 20px 20px 20px;
text-align: center;
background: #f6f6f6;
}
.container #canvas canvas {
max-width: 75%;
max-height: 75%;
margin: 0 auto;
display: block;
align-self: center;
}
#cell-size {
width: 60%;
margin: 28px;
flex: 1;
}
.cell-size-value-wrapper {
font-size: 13px;
color: #fff;
line-height: 60px;
padding: 0 15px;
}
input[type=range] {
-webkit-appearance: none;
margin: 10px 0;
width: 100%;
}
input[type=range]:focus {
outline: none;
}
input[type=range]::-webkit-slider-runnable-track {
width: 100%;
height: 2px;
cursor: pointer;
animate: 0.2s;
box-shadow: 0px 0px 0px transparent, 0px 0px 0px transparent;
background: #fff;
border-radius: 25px;
border: 0px solid transparent;
}
input[type=range]::-webkit-slider-thumb {
box-shadow: 0px 0px 0px transparent, 0px 0px 0px transparent;
border: 0px solid transparent;
height: 16px;
width: 16px;
border-radius: 8px;
background: #1abc9c;
cursor: pointer;
-webkit-appearance: none;
margin-top: -6px;
}
input[type=range]:focus::-webkit-slider-runnable-track {
background: #fff;
}
input[type=range]::-moz-range-track {
width: 100%;
height: 2px;
cursor: pointer;
animate: 0.2s;
box-shadow: 0px 0px 0px transparent, 0px 0px 0px transparent;
background: #fff;
border-radius: 25px;
border: 0px solid transparent;
}
input[type=range]::-moz-range-thumb {
box-shadow: 0px 0px 0px transparent, 0px 0px 0px transparent;
border: 0px solid transparent;
height: 16px;
width: 16px;
border-radius: 8px;
background: #1abc9c;
cursor: pointer;
margin-top: -6px;
}
.loading {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
background: rgba(0, 0, 0, 0.6);
z-index: 10;
line-height: 100%;
color: #fff;
text-align: center;
display: flex;
justify-content: center;
align-items: center;
font-size: 36px;
display: none;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment