2

I have made a board with 156X64 divs 3 pixel each with border radius, so it looks like a board out of LED. I have string representing 0 or 1 of each 7X5 matrix of letters.

var lgeoa="00100001000001000001100011000101110";//7X5 matrix letter A var lgeob="10000111000010001010100011000101110";//7X5 matrix letter B and so on... 

Drawing letter means change corresponding div background color. It is working fine, but after that I wanted to animate them the problem started. I clear line and draw in every 10 milliseconds, but its very very laggy. So please how can this code be optimized to work without lags? P.S. Surprisingly it's working better in IE11 rather than in chrome.

Here is a fiddle

photo

8
  • 5
    You'd get better/faster results using a canvas Commented Apr 29, 2014 at 19:36
  • Yes, I know that but want to be in divs, not in HTML5. Commented Apr 29, 2014 at 19:39
  • 1
    That's a lot of divs and internal gubbins for the browser to cope with when you animate... Commented Apr 29, 2014 at 19:40
  • divs are perfectly valid in HTML5, incidentally. Commented Apr 29, 2014 at 19:41
  • So what? There is no way to make smooth animation? Commented Apr 29, 2014 at 19:45

1 Answer 1

4

There's a lot of optimization that can be done here. I'll point out a couple.

Starting with the animate function, the first thing I notice is that you're running a bit of code every 10ms. Why don't we check out what's being run?

function animate() { var string = "აბგდევზთიკლმნოპჟრსტუფქღყშჩცძწჭხჯჰ ტესტ ტესტ აი ემ ე თეიბლ ტექსტი იწერება აქ"; //string to animate position = 150; //initial position of string window.setInterval(function () { clearLine(0); drawOnBoard(string, position, 0); position = position - 1; }, 10); } 

Clearline is the first function.

function clearLine(n){ for(var i=n*symbolHeight*lineWidth+n*lineWidth;i<(n+1)*symbolHeight*lineWidth+n*lineWidth;i++) leds[i].style.backgroundColor="black"; } 

That's a bit of a mess in the for loop. My understanding is that non-compiled code will run all of that math for every single iteration. So let's move it out of the for loop.

function clearLine(n) { var initial = n * symbolHeight * lineWidth + n * lineWidth; var length = (n + 1) * symbolHeight * lineWidth + n * lineWidth; for (var i = initial; i < length; i++) leds[i].style.backgroundColor = "black"; } 

Ah but there's still more to be done. I see that both equations have a lot of shared math.

function clearLine(n) { var whateverThisIs = symbolHeight * lineWidth + n * lineWidth; var initial = n * whateverThisIs; var length = (n + 1) * whateverThisIs; for (var i = initial; i < length; i++) leds[i].style.backgroundColor = "black"; } 

I saw that you're moving on so I'll stop working on this for now. There's still plenty more to optimize.

Here's a fiddle of the updated version.

Sign up to request clarification or add additional context in comments.

8 Comments

Thank you very much indeed! Yeah for sure there are optimizations, but its not enough to make smooth animation, because there are bunch of divs on the scene and browser lags because of them, not because of calculation(math).
@Mr.ILIA I agree with your decision. Changing tactics is probably for the best.
Thanks everybody, topic is closed ;)
I tried to change div into svg and actually the performance got better, but still not that smooth so I moved on canvas.
@Mr.ILIA Awesome! I've been optimizing the original program for fun today. Maybe it'll help your new code? jsfiddle.net/5CXQx/5
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.