added file download to meme

This commit is contained in:
bill bittner 2017-06-20 13:07:49 -07:00
parent 54d92a2c08
commit 0eebf30d48
9 changed files with 319 additions and 99 deletions

View file

@ -15,4 +15,10 @@
.row {
margin: 10px 0px 10px 0px;
}
canvas {
background-color: blue;
width: 100%;
height: auto;
}

View file

@ -0,0 +1,98 @@
var canvas = document.getElementById('meme-canvas');
var img = document.getElementById('start-image');
var canvasWidth;
var canvasHeight;
var fontSize = 28;
var topText = document.getElementById('top-text');
var bottomText = document.getElementById('bottom-text');
var ctx = canvas.getContext('2d');
var downloadLink = document.getElementById("meme-download-link");
var memeFileName = document.getElementById("meme-file-name");
// create the canvas
img.onload = function() {
// get dimensions of the start img
canvasWidth = img.width;
canvasHeight = img.height;
// hide start image
img.hidden = true;
// size the canvas
canvas.width = canvasWidth;
canvas.height = canvasHeight;
// draw the starting meme
drawMeme()
}
// if the text changes, re-draw the meme
topText.addEventListener('keyup', drawMeme);
bottomText.addEventListener('keyup', drawMeme);
// draw the image and draw the text over it
function drawMeme() {
ctx.clearRect(0, 0, canvasWidth, canvasHeight);
ctx.drawImage(img, 0, 0, canvasWidth, canvasHeight);
ctx.lineWidth = 4;
ctx.font = fontSize + 'px sans-serif';
ctx.strokeStyle = 'black';
ctx.fillStyle = 'white';
ctx.textAlign = 'center';
ctx.textBaseline = 'top';
var text1 = topText.value;
text1 = text1.toUpperCase();
x = canvasWidth / 2;
y = 0;
wrapText(ctx, text1, x, y, canvasWidth, fontSize, false);
ctx.textBaseline = 'bottom';
var text2 = bottomText.value;
text2 = text2.toUpperCase();
y = canvasHeight;
wrapText(ctx, text2, x, y, canvasHeight, fontSize, true);
}
function wrapText(context, text, x, y, maxWidth, lineHeight, fromBottom) {
var pushMethod = (fromBottom)?'unshift':'push';
lineHeight = (fromBottom)?-lineHeight:lineHeight;
var lines = [];
var y = y;
var line ='';
var words = text.split(' ');
for (var i = 0; i < words.length; i++) {
var testLine = line + ' ' + words[i];
var metrics = context.measureText(testLine);
var testWidth = metrics.width;
if (testWidth > maxWidth) {
lines[pushMethod](line);
line = words[i] + ' ';
} else {
line = testLine;
}
}
lines[pushMethod](line);
for (var k in lines ) {
context.strokeText(lines[k], x, y + lineHeight * k);
context.fillText(lines[k], x, y + lineHeight * k);
}
}
// save the meme
function downloadMeme() {
var dt = canvas.toDataURL('image/jpeg');
this.href = dt;
};
function updateFileName(){
downloadLink.download = this.value;
}
memeFileName.addEventListener('keyup', updateFileName);
downloadLink.addEventListener('click', downloadMeme, false);

View file

@ -0,0 +1,144 @@
// define variables
var socket = io();
var uploader = new SocketIOFileUpload(socket);
var stagedFiles = null;
/* helper functions */
// create a progress animation
function createProgressBar(element, size){
var x = 1;
var adder = 1;
function addOne(){
var bars = '<p>|';
for (var i = 0; i < x; i++){ bars += ' | '; }
bars += '</p>';
element.innerHTML = bars;
if (x === size){
adder = -1;
} else if ( x === 0){
adder = 1;
}
x += adder;
};
setInterval(addOne, 300);
}
// preview file and stage the image for upload
function previewAndStageFile(selectedFile){
var preview = document.getElementById('image-preview');
var dropzone = document.getElementById('drop-zone');
var previewReader = new FileReader();
preview.style.display = 'block';
dropzone.style.display = 'none';
previewReader.onloadend = function () {
preview.src = previewReader.result;
};
if (selectedFile) {
previewReader.readAsDataURL(selectedFile); // reads the data and sets the img src
document.getElementById('publish-name').value = selectedFile.name.substring(0, selectedFile.name.indexOf('.')); // updates metadata inputs
stagedFiles = [selectedFile]; // stores the selected file for upload
} else {
preview.src = '';
}
}
// update the publish status
function updatePublishStatus(msg){
document.getElementById('publish-status').innerHTML = msg;
}
// process the drop-zone drop
function drop_handler(ev) {
console.log("drop");
ev.preventDefault();
// if dropped items aren't files, reject them
var dt = ev.dataTransfer;
if (dt.items) {
if (dt.items[0].kind == 'file') {
var droppedFile = dt.items[0].getAsFile();
previewAndStageFile(droppedFile);
} else {
console.log("no files were found")
}
} else {
console.log("no items were found")
}
}
// prevent the browser's default drag behavior
function dragover_handler(ev) {
ev.preventDefault();
}
// remove all of the drag data
function dragend_handler(ev) {
var dt = ev.dataTransfer;
if (dt.items) {
for (var i = 0; i < dt.items.length; i++) {
dt.items.remove(i);
}
} else {
ev.dataTransfer.clearData();
}
}
/* configure the submit button */
document.getElementById('publish-submit').addEventListener('click', function(event){
event.preventDefault();
// make sure a file was selected
if (stagedFiles) {
// make sure only 1 file was selected
if (stagedFiles.length > 1) {
alert("Only one file allowed at a time");
return;
}
// make sure the content type is acceptable
switch (stagedFiles[0].type) {
case "image/png":
case "image/jpeg":
case "image/gif":
case "video/mp4":
uploader.submitFiles(stagedFiles);
break;
default:
alert("Only .png, .jpeg, .gif, and .mp4 files are currently supported");
break;
}
}
})
/* socketio-file-upload listeners */
uploader.addEventListener('start', function(event){
var name = document.getElementById('publish-name').value;
var license = document.getElementById('publish-license').value;
var nsfw = document.getElementById('publish-nsfw').value;
event.file.meta.name = name;
event.file.meta.license = license;
event.file.meta.nsfw = nsfw;
event.file.meta.type = stagedFiles[0].type;
// re-set the html in the publish area
document.getElementById('publish-active-area').innerHTML = '<div id="publish-status"></div><div id="progress-bar"></div>';
// start a progress animation
createProgressBar(document.getElementById('progress-bar'), 12);
});
uploader.addEventListener('progress', function(event){
var percent = event.bytesLoaded / event.file.size * 100;
updatePublishStatus('File is ' + percent.toFixed(2) + '% loaded to the server');
});
/* socket.io message listeners */
socket.on('publish-status', function(msg){
updatePublishStatus(msg);
});
socket.on('publish-failure', function(msg){
document.getElementById('publish-active-area').innerHTML = '<p>' + JSON.stringify(msg) + '</p><p> --(✖╭╮✖)→ </p><strong>For help, post the above error text in the #speech channel on the <a href="https://lbry.slack.com/" target="_blank">lbry slack</a></strong>';
});
socket.on('publish-complete', function(msg){
var publishResults = '<p>Your publish is complete!</p>';
publishResults += '<p><strong>NOTE: the transaction still needs to be mined by the network before you can access it! This will take a few minutes. To view the transaction on the blockchain explorer click the Transaction ID link below.</strong></p>';
publishResults += '<p>Your Transaction ID is: <a target="_blank" href="https://explorer.lbry.io/#!/transaction?id=' + msg.result.txid + '">' + msg.result.txid + '</a></p>';
publishResults += '<p>Your Claim ID is: ' + msg.result.claim_id + '</p>';
publishResults += '<p>Here is a link to the claim where your asset will be published: <a target="_blank" href="https://spee.ch/' + msg.name + '">spee.ch/' + msg.name + '</a></p>';
publishResults += '<p>Here is a direct link to where your asset will be stored: <a target="_blank" href="https://spee.ch/' + msg.name + '/' + msg.result.claim_id + '">spee.ch/' + msg.name + '/' + msg.result.claim_id + '</a></p>';
publishResults += '<p><a href="/">Reload to publish another asset</a></p>';
document.getElementById('publish-active-area').innerHTML = publishResults;
});

View file

@ -4,7 +4,7 @@ const logger = require('winston');
module.exports = (app, ua, googleAnalyticsId) => {
// route to fetch all free public claims
app.get('/meme-fodder', ({ originalUrl }, res) => {
app.get('/lbrymemefodder', ({ originalUrl }, res) => {
logger.debug(`GET request on ${originalUrl}`);
res.status(200).render('memeFodder');
});

View file

@ -1,90 +1,15 @@
<div class="container">
{{> topBar}}
{{ image }}
<div class="row">
{{> memeMaker}}
</div>
<div class="row">
{{> memeFodderResults}}
</div>
</div>
<script>
var canvas = document.getElementById('meme-canvas');
ctx = canvas.getContext('2d');
<script src="/assets/js/memeDraw.js"></script>
// set the size
var img = document.getElementById('start-image');
var canvasWidth = 400;
var canvasHeight = 300;
canvas.width = canvasWidth;
canvas.height = canvasHeight;
var topText = document.getElementById('top-text');
var bottomText = document.getElementById('bottom-text');
img.onload = function() {
drawMeme()
}
topText.addEventListener('keydown', drawMeme);
topText.addEventListener('keyup', drawMeme);
topText.addEventListener('change', drawMeme);
bottomText.addEventListener('keydown', drawMeme);
bottomText.addEventListener('keyup', drawMeme);
bottomText.addEventListener('change', drawMeme);
function drawMeme() {
ctx.clearRect(0, 0, canvasWidth, canvasHeight);
ctx.drawImage(img, 0, 0, canvasWidth, canvasHeight);
ctx.lineWidth = 4;
ctx.font = '20pt sans-serif';
ctx.strokeStyle = 'black';
ctx.fillStyle = 'white';
ctx.textAlign = 'center';
ctx.textBaseline = 'top';
var text1 = document.getElementById('top-text').value;
text1 = text1.toUpperCase();
x = canvasWidth / 2;
y = 0;
wrapText(ctx, text1, x, y, 300, 28, false);
ctx.textBaseline = 'bottom';
var text2 = document.getElementById('bottom-text').value;
text2 = text2.toUpperCase();
y = canvasHeight;
wrapText(ctx, text2, x, y, 300, 28, true);
}
function wrapText(context, text, x, y, maxWidth, lineHeight, fromBottom) {
var pushMethod = (fromBottom)?'unshift':'push';
lineHeight = (fromBottom)?-lineHeight:lineHeight;
var lines = [];
var y = y;
var line ='';
var words = text.split(' ');
for (var i = 0; i < words.length; i++) {
var testLine = line + ' ' + words[i];
var metrics = context.measureText(testLine);
var testWidth = metrics.width;
if (testWidth > maxWidth) {
lines[pushMethod](line);
line = words[i] + ' ';
} else {
line = testLine;
}
}
lines[pushMethod](line);
for (var k in lines ) {
context.strokeText(lines[k], x, y + lineHeight * k);
context.fillText(lines[k], x, y + lineHeight * k);
}
}
</script>
<script src="/socket.io/socket.io.js"></script>
<script src="/siofu/client.js"></script>

View file

@ -1,7 +1,7 @@
<div class="col-md-12">
<div class="card" id="documentation">
<div class="card-title card-block grey lighten-1 white-text">
<h2>Site Navigation</h2>
<h2>Documentation</h2>
</div>
<div class="row">
<div class="col-md-12">

View file

@ -1,16 +1,26 @@
<div class="col-md-12">
<div class="card" id="examples">
<div class="card-title card-block grey lighten-1 white-text">
<h2>Examples</h2>
<h2>What Is Spee.ch?</h2>
</div>
<div class="card-block">
<ul>
<li class="list-square"><a href="/coconuts">spee.ch/coconuts</a></li>
<li class="list-square"><a href="/wood">spee.ch/wood</a></li>
<li class="list-square"><a href="/doitlive">spee.ch/doitlive</a></li>
<li class="list-square"><a href="/doitlive/all">spee.ch/doitlive/all</a></li>
<li class="list-square"><a href="/doitlive/ca3023187e901df9e9aabd95d6ae09b6cc69b3f0">spee.ch/doitlive/ca3023187e901df9e9aabd95d6ae09b6cc69b3f0</a></li>
</ul>
<div class="row">
<div class="col-md-6" id="contribute">
<h2>Spee.ch is for sharing</h2>
<p>Spee.ch is a platform by which you can publish images to the Lbry blockchain. Just upload an image, title it, and send it off into the lbry ecosystem.</p>
<p>Spee.ch is also a platform to serve you those images. It's like have a personal chef that will serve you a meal anywhere in the world. All you have to do is ask for it, by using "spee.ch/" + the name of a claim.</p>
<p>If you want a specific image, just ask for it with the claim_id by using "spee.ch/" + the name of the claim + "/" + the claim id.</p>
</div>
<div class="col-md-6" id="bugs">
<h2>Examples</h2>
<h5>Use spee.ch to serve the top asset at a lbry claim:</h5>
<a href="/coconuts">spee.ch/coconuts</a><br>
<a href="/wood">spee.ch/wood</a><br>
<a href="/doitlive">spee.ch/doitlive</a><br>
<h5>Use spee.ch to show you all the assets at a lbry claim:</h5>
<a href="/doitlive/all">spee.ch/doitlive/all</a>
<h5>Use spee.ch to serve you a specific asset by claim id:</h5>
<a href="/doitlive/ca3023187e901df9e9aabd95d6ae09b6cc69b3f0">spee.ch/doitlive/ca3023187e901df9e9aabd95d6ae09b6cc69b3f0</a>
</div>
</div>
</div>
</div>

View file

@ -0,0 +1,14 @@
<div class="col-md-12">
<div class="card" id="publish">
<div class="card-title card-block grey lighten-1 white-text">
<h2>Recent Publishes</h2>
</div>
<div class="card-block" id="publish-active-area">
<div class="row">
<p>Below are some of the most recent entries published via /meme-fodder</p>
(pull all the claims published to 'lbryMemeFodder' and display most recent 20 here)
(maybe a voting system? Is there a way to allow people to donate funds to a claimId so that it will show up higher in the results?)
</div>
</div>
</div>
</div>

View file

@ -1,23 +1,46 @@
<div class="col-md-12">
<div class="card" id="publish">
<div class="card-title card-block grey lighten-1 white-text">
<h2>Meme Generator</h2>
<h2>/LbryMemeFodder</h2>
</div>
<div class="card-block" id="publish-active-area">
<form id="meme-form" action="" method="" enctype="multipart/form-data">
<div class="row">
<p>Congratulations, you found <i>/LbryMemeFodder</i>. Here's the game...</p>
<p>(1) <i>/LbryMemeFodder</i> will always use the winning public, free image published to <a href="lbry://meme-fodder">lbry://meme-fodder</a>. (meaning the most recent, highest bid). Want to put a different image on the chopping block? Go publish it!</p>
<p>(2) Create a meme based on the current claim with the tool below. Think you got a winner? <a href="https://twitter.com/hashtag/LbryMemeFodder" target="_blank">Share it with the community</a> and see what they think!</p>
</div>
<div class="row">
<div class="col-md-6">
<canvas id="meme-canvas">
If you can see this, canvas is not supported.
</canvas>
<img id="start-image" src="http://spee.ch/meme-fodder" alt="a picture to make your meme with" hidden="true"/>
<img id="start-image" src="/meme-fodder" alt="a picture to make your meme with"/>
</div>
<div class="col-md-6">
<input type="text" value="Is it ready?" id="top-text" />
<input type="text" value="Who cares, ship it!" id="bottom-text" />
<div class="row">
<div class="col-md-12">
<label for="top-text">Meme:</label>
<input id="top-text" type="text" value="Is it ready?" />
</div>
</div>
<div class="row">
<div class="col-md-12">
<input id="bottom-text" type="text" value="Who cares, ship it!" />
</div>
</div>
<div class="row">
<div class="col-md-12">
<label for="meme-name">File Name:</label>
<input id="meme-file-name" class="form-control" type="text" value="YourFileName.jpg" />
</div>
</div>
<div class="row">
<div class="col-md-12">
<a id="meme-download-link" download="YourFileName.jpg"><button>Save and Publish</button></a>
</div>
</div>
</div>
</div>
</form>
</div>
</div>
</div>