Advertisement
ipsBruno

(Javascript) Classe para Barra de Progresso em Canvas

Apr 8th, 2012
647
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  *  Copyright (c) 2012 [iPs]TeaM
  3.  *  Bruno da Silva (brunoemail@r7.com)
  4.  *  Simples e eficiente biblioteca para criar efeito progressbar em javascript canvas
  5.  
  6.  * www.brunodasilva.com
  7.  * www.ips-team.forumeiros.com
  8. */
  9.  
  10. <script>
  11. function createProgressBar(x, y, largura, altura, color, maximo, canvasid) {
  12.         this.valorprogresso = 0
  13.         this.maximo = maximo
  14.         this.largura = largura
  15.         this.altura = altura
  16.         this.cnv = document.getElementById(canvasid).getContext('2d')
  17.         this.background = document.getElementById(canvasid).style.background;
  18.         this.posicaox = x
  19.         this.posicaoy = y
  20.         this.cor = color
  21.         this.updateProgress = function () {
  22.                 if (this.valorprogresso < this.maximo) {
  23.                         this.cnv.fillStyle = this.cor
  24.                         this.cnv.fillRect(this.posicaox + ((this.largura /  this.maximo) * (this.valorprogresso)), y,this.largura /  this.maximo, this.altura)
  25.                         this.valorprogresso+=1
  26.  
  27.                         if (this.textmiddle) {
  28.                                 this.cnv.fillStyle =this.background
  29.                                 this.cnv.fillRect(this.posicaox - 3, y + (this.tamanhof + this.altura), this.tamanhof * 5, this.tamanhof + this.altura - 1)
  30.                                 this.cnv.fillStyle = this.textcolor
  31.                                 this.cnv.font = this.textfonte
  32.                                 if ((((this.valorprogresso / this.maximo) * 100) >> 0) == 100) {
  33.                                         this.cnv.fillText("Completo!", this.posicaox - 1, this.altura + y + (this.tamanhof * 2)-3)
  34.                                 } else {
  35.                                         this.cnv.fillText((((this.valorprogresso / this.maximo) * 100) >> 0) + " %", this.posicaox - 2, this.altura + y + (this.tamanhof * 2))
  36.                                 }
  37.                         }
  38.                 }
  39.                 return (this.valorprogresso < this.maximo);
  40.         }
  41.         this.getProgress = function () {
  42.                 return this.valorprogresso;
  43.         }
  44.         this.setProgress = function (v) {
  45.                 return this.valorprogresso = v;
  46.         }
  47.         this.showText = function (ativado, color, fonte, tamanho) {
  48.                 this.textcolor = color
  49.                 this.tamanhof = tamanho
  50.                 this.textfonte = tamanho + "pt " + fonte
  51.                 this.textmiddle = ativado;
  52.         }
  53.         return true;
  54. }
  55.  
  56. </script>
  57.  
  58.  
  59. <canvas style="background-color:black" id='canvasID' width=640 height=480></canvas>
  60.  
  61. <script>
  62. // =============== [ Aqui um exemplo de uso ] ========================
  63.  
  64. bar =  new createProgressBar(200, 300, 300, 10, "green", 300, "canvasID");
  65. bar.showText(true, "white", "verdana", 10);
  66.  
  67. executarMinhaProgress();
  68.  
  69. function executarMinhaProgress() {
  70.     if(bar.updateProgress()) {         
  71.         setTimeout(executarMinhaProgress , 5);
  72.     }
  73. }
  74.  
  75. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement