Simplificare cod javascript

Discutii despre tot ce nu se incadreaza in celelalte categorii.

Moderators: Moderatori ajutatori, Moderatori

Post Reply
User avatar
-BlacKodE-
Fost moderator
Fost moderator
Posts: 1193
Joined: 16 Nov 2013, 11:57
Detinator Steam: Da
SteamID: blackode
Reputatie: Fost Super moderator
Membru Club eXtreamCS (1 luna)
Fost Membru Club eXtreamCS (5 apr - 5 mai - acces in club)
Has thanked: 16 times
Been thanked: 10 times

27 Nov 2016, 15:04

Salut ma poate ajuta cineva sa simplific codul acesta?
| Afiseaza codul
function Scroller(canvas, scrollingText, speed, textColor)
{
    this.canvas = canvas;
    this.text = scrollingText;
    this.textWidth = 0;
    this.textHeight = 12;
    this.rate = speed;
    this.textColor = textColor;
    this.showText = true;
    this.font = "12px Arial";
    var self = this;
    this.animId;

    this.xloc = 0;
    this.yloc = 0;
    this.yloc2 = 50;
    this.yloc3 = 100;
    this.yloc4 = 150;
    this.yloc5 = 200;
    this.yloc6 = 250;
    this.yloc7 = 300;
    this.yloc8 = 350;

    this.doAnimation = function()
    {
        this.animId = requestAnimationFrame(function() { self.doAnimation(); });
        if(this.textWidth == 0)
        {
            var context = this.canvas.getContext("2d");
            context.font = this.font;
            this.textWidth = context.measureText(this.text).width;
        }

            if(this.yloc > this.canvas.height + this.textHeight)
            {
               this.yloc = 0;
            }   
            else
            {
                this.yloc += this.rate;
            }

            if(this.yloc2 > this.canvas.height + this.textHeight)
            {
               this.yloc2 = 0;
            }   
            else
            {
                this.yloc2 += this.rate;
            }

            if(this.yloc3 > this.canvas.height + this.textHeight)
            {
               this.yloc3 = 0;
            }   
            else
            {
                this.yloc3 += this.rate;
            }

            if(this.yloc4 > this.canvas.height + this.textHeight)
            {
               this.yloc4 = 0;
            }   
            else
            {
                this.yloc4 += this.rate;
            }

            if(this.yloc5 > this.canvas.height + this.textHeight)
            {
               this.yloc5 = 0;
            }   
            else
            {
                this.yloc5 += this.rate;
            }

            if(this.yloc6 > this.canvas.height + this.textHeight)
            {
               this.yloc6 = 0;
            }   
            else
            {
                this.yloc6 += this.rate;
            }

            if(this.yloc7 > this.canvas.height + this.textHeight)
            {
               this.yloc7 = 0;
            }   
            else
            {
                this.yloc7 += this.rate;
            }

            if(this.yloc8 > this.canvas.height + this.textHeight)
            {
               this.yloc8 = 0;
            }   
            else
            {
                this.yloc8 += this.rate;
            }

        this.drawText();
    }
    this.drawText = function()
    {
        var context = this.canvas.getContext("2d");
        context.clearRect(0, 0, this.canvas.width, this.canvas.height);
        if(this.showText)
        {
            context.font = this.font;
            context.fillStyle = this.textColor;
            context.fillText(this.text, this.xloc, this.yloc);
            context.fillText(this.text, this.xloc, this.yloc2);
            context.fillText(this.text, this.xloc, this.yloc3);
            context.fillText(this.text, this.xloc, this.yloc4);
            context.fillText(this.text, this.xloc, this.yloc5);
            context.fillText(this.text, this.xloc, this.yloc6);
            context.fillText(this.text, this.xloc, this.yloc7);
            context.fillText(this.text, this.xloc, this.yloc8);
        }
    };
}
Am incercat sa fac un array si for ca sa scap de acele yloc1,2,3.... Dar nu functioneaza.
Are cineva o idee?
RoyalServer 2
riderel
Membru, skill +1
Membru, skill +1
Posts: 159
Joined: 20 Aug 2013, 01:24
Detinator Steam: Da
CS Status: Working :)
Location: /dev/null
Contact:

27 Nov 2016, 17:38

Experienta mea in javascript este cam limitata, nu am facut decat vreo 2-3 plugine pentru chrome si alea direct in javascript, fara jquery si alte prostii.
Daca nu iti functioneaza am sa te rog sa imi oferi mai multe date ( de exemplu .. cum si cu ce variabile chemi functia ).
Daca functioneaza dar iti ofera aceleasi date din array cand se executa drawtext(); , adauga un timeout de 100ms, ar trebui sa ajunga.
Javascript nu este un limbaj tocmai sincronizat.
De exemplu, chiar daca intra in for loop el va considera ca este un pas si va actiona drawtext(); chiar daca nu este gata loop-ul.
| Afiseaza codul
function Scroller(canvas, scrollingText, speed, textColor)
{
    this.canvas = canvas;
    this.text = scrollingText;
    this.textWidth = 0;
    this.textHeight = 12;
    this.rate = speed;
    this.textColor = textColor;
    this.showText = true;
    this.font = "12px Arial";
    var self = this;
    this.animId;

    this.xloc = 0;
    this.yloc = ["0", "50", "100", "150", "200", "250", "300", "350"];
	
    this.doAnimation = function()
    {
        this.animId = requestAnimationFrame(function() { self.doAnimation(); });
        if(this.textWidth == 0)
        {
            var context = this.canvas.getContext("2d");
            context.font = this.font;
            this.textWidth = context.measureText(this.text).width;
        }
	for (i = 0; i < this.yloc.length; i++) 
	{ 
		if(this.yloc > this.canvas.height + this.textHeight)
		{
			this.yloc = 0;
		}
		else
		{
			this.yloc += this.rate;
		}	
	}	
        this.drawText();
    };
    this.drawText = function()
    {
        var context = this.canvas.getContext("2d");
        context.clearRect(0, 0, this.canvas.width, this.canvas.height);
        if(this.showText)
        {
            context.font = this.font;
            context.fillStyle = this.textColor;
			for (i = 0; i < this.yloc.length; i++) { 
				context.fillText(this.text, this.xloc, this.yloc);
			}
        }
    };
}
Post Reply

Return to “Discutii generale”

  • Information