From b30b539dab5c329d874a1c5f235fa2518605da10 Mon Sep 17 00:00:00 2001 From: Timofey Barinov Date: Fri, 26 Oct 2018 20:53:49 +0300 Subject: [PATCH] Added gif download modal (#442) * Enhance gif view * Gif modal download button & Fix gif downloading * Fix gif generation aspect ratio * Fix fix gif downloading * Clarified Download(PNG) button --- examples/demo.css | 4 +-- examples/demo.js | 85 ++++++++++++++++++++++++++++++++++++++++++--- examples/index.html | 69 ++++++++++++++---------------------- package.json | 1 + 4 files changed, 111 insertions(+), 48 deletions(-) diff --git a/examples/demo.css b/examples/demo.css index 7bb4357727..530f3784f0 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 f610fe3be1..29a312ff27 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 0cf0079588..6ed5736b1a 100644 --- a/examples/index.html +++ b/examples/index.html @@ -23,6 +23,9 @@ + + + diff --git a/package.json b/package.json index 153aba10cb..805da0e5fa 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",