I find surrounding these lines of code not accurate.
from manim import *
class CodeTrackingAnimation(Scene):
def construct(self):
code_str = '''#include<iostream>
using namespace std;
int main(){
int sum = 0;
for(int i=0;i<n;i++){
sum += i;
}
return 0;
}'''
code = self.build_code_block(code_str)
for i in range(len(code.code)-1):
self.highlight(i, i+1)
def build_code_block(self, code_str):
# build the code block
code = Code(code=code_str, language='C++', background="window")
self.add(code)
# build sliding windows (SurroundingRectangle)
self.sliding_wins = VGroup()
height = code.code[0].height
for line in code.code:
self.sliding_wins.add(SurroundingRectangle(line).set_fill(YELLOW).set_opacity(0))
self.add(self.sliding_wins)
return code
def highlight(self, prev_line, line):
self.play(self.sliding_wins[prev_line].animate.set_opacity(0.3))
self.play(ReplacementTransform(self.sliding_wins[prev_line], self.sliding_wins[line]))
self.play(self.sliding_wins[line].animate.set_opacity(0.3))
Above is my code. I want the highlight (SurroundingRectangle precisely surround the codeline), but the SurroundingRectangle is out of the codeline's boundary (However, the first codeline's SurroundingRectangle is in the right position).
What's the problem with my code? How can I accurately highlight these codelines without using coordinates parameters?