1

So I am developing a reader for my comic and since the images take some time to download I wanted to preload them. Doing so meant having to create a Progress to display the progress of pages being downloaded before the reader can start reading it. I have it set up as you can see on this link here

I was wondering if there was a simple light weight code I can use to improve on. I was considering using Pace.JS but if there is a more light weight simple alternative then I'd prefer that cause I also want to learn how the ProgressBar works and perhaps improve it and learn more :D

If you check the link provided above it shows the page with the code I will put below implemented. After the pages load I will put jquery to hide the loading div. If possible can there be a way to also change the text alongside the download bar?

Sorry for so many questions, still learning >~<

HTML

<div id="loading">
<img id="loading_logo" class="animated infinite bounce" src="source_images/new_logo_compressed.png" />
    <div id="progressbar">
    <div></div>
    </div>
    <div class="text">Loading... 30%</div>
</div>
<a href="#" id="left" class="button hvr-icon-back"><div id="button_text">Next Page</div></a>
<a href="#" id="right" class="button hvr-icon-forward"><div id="button_text">Last Page</div></a>

<div >
<img id="manga" src="source_images/00.jpg" />
</div>

CSS

#loading {
background-color: black;
opacity: 0.75;
position: fixed;
z-index:1;
height:100%;
width:100%;
}

#loading_logo {
display: block;
margin-left: auto;
margin-right: auto;
padding-top: 30vh;
padding-bottom: 5vh;
width: 20vw;
}
#progressbar {
border: 3px solid #fff;
border-radius: 20px;
padding: 2px;    
margin-left: 20vw;
margin-right: 20vw;

}
#progressbar > div {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
background-color: #fff;
width: 30%;
height: 18px;
border: 1px solid rgba(0, 0, 0, 0);
border-radius: 20px;
}
.text {
color: #fff;
margin-top: 15px;
font-size: 21px;
font-weight: bold;
text-align: center;
}

Jquery (that makes reader work)

var count = 0;
var images = ["source_images/00.jpg", "source_images/01.jpg", "source_images/02.jpg", "source_images/03.jpg", "source_images/04.jpg", "source_images/05.jpg", "source_images/06.jpg"];

// Preloading Images

$.preloadImages = function() {
for (var i = 0; i < arguments.length; i++) {
$("<img />").attr("src", arguments[i]);
}
}

$.preloadImages("source_images/00.jpg", "source_images/01.jpg", "source_images/02.jpg", "source_images/03.jpg", "source_images/04.jpg", "source_images/05.jpg", "source_images/06.jpg");


$(function manga () {
$("#left").click(function () {
    ++count;
    var img = images[count % images.length];
    //alert(img);
    $("#manga").attr("src", img);
    //alert("clicked");
    //manga();
});
$("#right").click(function () {
    if(count == 0)
    {
        count = images.length-1;
    }
    else {
        --count;
    }

    var img = images[count % images.length];
    //alert(img);
    $("#manga").attr("src", img);
    //alert("clicked");
    //manga();
});
//manga();
});
otaku8bit
  • 13
  • 5

3 Answers3

0

i would prefer pace.JS as you already mentioned.. in this post someone tries something similar: How to show a running progress bar while page is loading

If u wanna start learning jquery u can go here: http://www.jquery-tutorial.net/

there is a nice documentation about DOM manipulation & Ajax Calls. That will help you

Community
  • 1
  • 1
Shouldz
  • 58
  • 6
  • I was considering dispersing the code throughout the currently jquery I have for calling all the images but it seems like a hassle for every chapter that I plan to make. I've made progress bars in Unity when I use to program games but C# and Jquery are not really the same so I'm unsure if there is a way to measure the assets as they load to affect the progress bar aside from just adding values? – otaku8bit Sep 29 '15 at 07:57
0

This works

var imgs =  { "first":0,"last":6, "path":"http://chocobento.x10.mx/chocobento/source_images/", "current":0}, 
    images = [],
    width = 0,
    pct = Math.ceil(100/(imgs.last-imgs.first))
    count=0;

function preload() {
  for (var i=imgs.first;i<=imgs.last;i++) {
    images[i] = new Image();
    images[i].onload=progress;
  }
  next();
} 
function next() {
  if (imgs.current > imgs.last) {
    $("#loading").hide(); // or setTimeout(function() {$("#loading").hide(); },200);
    return;
  }
  images[imgs.current].src=imgs.path+pad(imgs.current)+".jpg";      
  imgs.current++;
}
function pad(num) { return num >= 10?num:("0"+num).slice(-2);}

function progress() {
  width+=pct;
  if (width>100) width=100;
  $("#progressbar>div").css({"width":width+"%"});
  $(".text").html("Loading... "+width+"%");
  next();
}  

$(function() {
  preload();
  $("#left").click(function () {
    ++count;
    if (count>imgs.last) count = imgs.first; // wrap
    $("#manga").attr("src", images[count].src);
  });
  $("#right").click(function () {
     --count;
    if(count < imgs.first) count = imgs.last; // wrap
    $("#manga").attr("src", images[count].src);
  });
});
#loading {
 background-color: black;
 opacity: 0.75;
 position: fixed;
 z-index:1;
 height:100%;
 width:100%;
    }

    #loading_logo {
 display: block;
 margin-left: auto;
 margin-right: auto;
 padding-top: 30vh;
    padding-bottom: 5vh;
 width: 20vw;
    }
    #progressbar {
    border: 3px solid #fff;
    border-radius: 20px;
    padding: 2px;    
 margin-left: 20vw;
    margin-right: 20vw;

    }
    #progressbar > div {
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    background-color: #fff;
    width: 1%;
    height: 18px;
    border: 1px solid rgba(0, 0, 0, 0);
    border-radius: 20px;
    }
    .text {
    color: #fff;
    margin-top: 15px;
    font-size: 21px;
    font-weight: bold;
 text-align: center;
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="loading">
    <img id="loading_logo" class="animated infinite bounce" src="http://chocobento.x10.mx/chocobento/source_images/new_logo_compressed.png" />
        <div id="progressbar">
       <div></div>
     </div>
        <div class="text">Loading... 0%</div>
    </div>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • should I just add this to my existing jquery? Want me to show you what it is so we can find the best way to implement it? :D – otaku8bit Sep 29 '15 at 07:59
  • or should i just paste the code below separately and edit it to accommodate the code? – otaku8bit Sep 29 '15 at 08:14
  • See the update. The code replaces your progress code/preload code – mplungjan Sep 29 '15 at 08:21
  • So do I place this where the preload is right now? :o – otaku8bit Sep 29 '15 at 08:24
  • Yes, it replaces `var count = 0; var images = ["source_images/00.jpg", "source_images/01.jpg", "source_images/02.jpg", "source_images/03.jpg", "source_images/04.jpg", "source_images/05.jpg", "source_images/06.jpg"]; // Preloading Images $.preloadImages = function() { for (var i = 0; i < arguments.length; i++) { $("").attr("src", arguments[i]); } } $.preloadImages("source_images/00.jpg", "source_images/01.jpg", "source_images/02.jpg", "source_images/03.jpg", "source_images/04.jpg", "source_images/05.jpg", "source_images/06.jpg"); ` – mplungjan Sep 29 '15 at 08:25
  • I edited your load function into mine - my html is missing the manga image and arrows though – mplungjan Sep 29 '15 at 08:29
  • Seems to be working, do I place the "hide Loading Div" code in the next function or should I create a separate function that hides the div once the cnt equals 100? – otaku8bit Sep 29 '15 at 08:30
  • also don't worry about the arrows, those will be removed as I built the next part of the interface. – otaku8bit Sep 29 '15 at 08:31
  • I just updated the function of the next and previous to use my code for holding the image source – mplungjan Sep 29 '15 at 08:33
  • I keep getting this arrow when navigating with the menu buttons. "count is not defined" – otaku8bit Sep 29 '15 at 08:38
  • I added more HTML as well so maybe that can help you. – otaku8bit Sep 29 '15 at 08:39
  • Wait nvm I simply added a cnt variable to the top and it worked. I know this is outside the question but every time I click next on the button it seems to cache a new copy of the images in the resources. Is there a way to just preload it once and let it access the cache? – otaku8bit Sep 29 '15 at 08:58
  • Fhew...it's getting to be a coding request more than just fixing a broken script. I will have a look – mplungjan Sep 29 '15 at 09:02
  • No its fine, to be fair thats outside the bounds of this question. So instead can you adjust the cnt and num measurements cause I have an image that is called 00.jpg which is the cover. So it loads 6 images but there are technically 7 if you count the 00.jpg-06.jpg – otaku8bit Sep 29 '15 at 09:09
  • nvm I fixed it, basically I just changed the source image's len to 7. And changed the function part so it looks as followed, "cnt>imgs.len-1" – otaku8bit Sep 29 '15 at 09:26
  • this works beautifully all we need now is to hide the loading screen div as soon as width equals 100. I put earlier but it seems that once it reaches 100 it doesn't give it time to visually display its done first before hiding the div. Do you recommend I set a delay before the function to hide the div? :D – otaku8bit Sep 29 '15 at 09:52
  • I added `if (imgs.current > imgs.last) { $("#loading").hide(); return; }` or `if (imgs.current > imgs.last) { setTimeout(function() {$("#loading").hide(); },200); return; }` – mplungjan Sep 29 '15 at 09:54
  • so i added this, to the function and it works beautifully. You don't have to add it but I want to put it in the comments in case anyone who comes across this wants to find out. `function progress() { width+=pct; if (width>100) width=100; $("#progressbar>div").css({"width":width+"%"}); $(".text").html("Loading... "+width+"%"); next(); if (width=100) setTimeout( function() { $("#loading").hide(); }, 1200); ; }` – otaku8bit Sep 29 '15 at 09:57
  • Width=100 is an assignment not a comparison – mplungjan Sep 29 '15 at 10:42
  • 1
    Yea I realized that I should have used ==, but instead used your code since it fixed the endless call requests I would see in the inspector. How did you manage to fix the endless call requests? – otaku8bit Sep 29 '15 at 10:46
-1

You can accomplish that without using Pace.Js. This only needs jQuery

$.ajax({
xhr: function()
{
  var xhr = new window.XMLHttpRequest();
  //Upload progress
  xhr.upload.addEventListener("progress", function(evt){
  if (evt.lengthComputable) {
    var percentComplete = evt.loaded / evt.total;
    //Do something with upload progress
    console.log(percentComplete);
  }
}, false);
//Download progress
xhr.addEventListener("progress", function(evt){
  if (evt.lengthComputable) {
    var percentComplete = evt.loaded / evt.total;
    //Do something with download progress
    console.log(percentComplete);
  }
}, false);
  return xhr;
},
type: 'POST',
url: "/",
data: {},
success: function(data){
//Do something success-ish
}
});
Sandeep
  • 401
  • 3
  • 11
  • I haven't gotten around to AJAX but I am guessing I should now lol. But on that serious note, is there a way to change the properties of the HTML and CSS to simulate that progress visually? Like adjusting the Divs I provided? (i know this is such a newbie question) – otaku8bit Sep 29 '15 at 08:12