diff --git a/examples/demo.css b/examples/demo.css index 7bb43577..530f3784 100644 --- a/examples/demo.css +++ b/examples/demo.css @@ -132,8 +132,8 @@ h1 { #gif_element { display: block; - padding: 50px; margin: 0 auto; - + width: 100%; + height: auto; } diff --git a/examples/demo.js b/examples/demo.js index f610fe3b..29a312ff 100644 --- a/examples/demo.js +++ b/examples/demo.js @@ -27,11 +27,13 @@ window.onload = function() { $("#addStep select").on("change", ui.selectNewStepUi); $("#addStep #add-step-btn").on("click", ui.addStepUi); - $('#addStep #download-btn').click(function() { - $('img:last()').trigger( "click" ); - + + $('#download-btn').click(function() { + $('.img-thumbnail:last()').trigger("click"); + return false; - }); + }); + $('body').on('click', 'button.remove', ui.removeStepUi); $('#save-seq').click(() => { sequencer.saveSequence(window.prompt("Please give a name to your sequence..."), sequencer.toString()); @@ -39,6 +41,81 @@ window.onload = function() { refreshOptions(); }); + var isWorkingOnGifGeneration = false; + + $('.js-view-as-gif').on('click', function(event) { + // Prevent user from triggering generation multiple times + if (isWorkingOnGifGeneration) return; + + isWorkingOnGifGeneration = true; + + var button = event.target; + button.disabled = true; + + + try { + // Select all images from previous steps + var imgs = document.getElementsByClassName("img-thumbnail"); + + var imgSrcs = []; + + for (var i = 0; i < imgs.length; i++) { + imgSrcs.push(imgs[i].src); + } + + var options = { + 'gifWidth': imgs[0].width, + 'gifHeight': imgs[0].height, + 'images': imgSrcs, + 'frameDuration': 7, + } + + gifshot.createGIF(options, function(obj) { + if(!obj.error) { + // Final gif encoded with base64 format + var image = obj.image; + var animatedImage = document.createElement('img'); + + animatedImage.id = "gif_element"; + animatedImage.src = image; + + + var modal = $('#js-download-gif-modal'); + + $("#js-download-as-gif-button").one("click", function() { + // Trigger download + download(image, "index.gif", "image/gif"); + + // Close modal + modal.modal('hide'); + }) + + var gifContainer = document.getElementById("js-download-modal-gif-container"); + + // Clear previous results + gifContainer.innerHTML = ''; + + // Insert image + gifContainer.appendChild(animatedImage); + + + // Open modal + modal.modal(); + + button.disabled = false; + isWorkingOnGifGeneration = false; + } + }); + } + catch(e) { + console.error(e); + button.disabled = false; + isWorkingOnGifGeneration = false; + + } + }); + + // image selection and drag/drop handling from examples/lib/imageSelection.js sequencer.setInputStep({ dropZoneSelector: "#dropzone", diff --git a/examples/index.html b/examples/index.html index 0cf00795..6ed5736b 100644 --- a/examples/index.html +++ b/examples/index.html @@ -23,6 +23,9 @@ + + + diff --git a/package.json b/package.json index 153aba10..805da0e5 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "buffer": "~5.0.2", "commander": "^2.11.0", "data-uri-to-buffer": "^2.0.0", + "downloadjs": "^1.4.7", "fisheyegl": "^0.1.2", "font-awesome": "~4.5.0", "get-pixels": "~3.3.0",