this video I'll show you how to create this animation using manim manim is a python library that lets you create animations using Code python is a programming language and the library is like a mod that you can add to it it adds extra features in this case it lets you create animations in two of my previous videos I showed you how to install manim on Windows and Mac and in this video I want to show you how to get started using manim so first things first let's open vs code here we'll want to create a new file you can either click here or go up to file and new text file or command n if you're really cool here it says that you can select a language so I'm going to click here and we want python so I'll type in Python and click on it I'm also closing this welcome screen next I want to save this somewhere so I'm clicking command s or controls on Windows choosing my desktop and I'll say that the name of this file is Shrek beautiful if you followed the installation guide you should already have the manim side view pyland and python extensions installed into vs code so you'll be able to see the manim side view icon over here this one lets you preview animations that you create inside of vs code without having to navigate through your file system also I'll open the animation that we want to recreate inside of VLC so that it's easier for me to go frame by frame to make it easier to see what we're trying to copy you can go one frame at a time in VLC by clicking the E on the keyboard but before we start animating we first need to import manim into python remember it's a library so I'm going to say from manim import snowflake what this does is it essentially Imports the whole manim Library into our file this white dot here means that this file hasn't yet been saved so I'm going to click command s to save it next we'll want to create a scene essentially each scene is a new video file and you can create multiple scenes within one python file I'm also going to open my reference file and keep it over here so I don't make many mistakes so to create a scene you're going to say class then the name of your scene this can be whatever you want I'm going to put xd12 3 and then inside of parentheses you're going to say scene and at the end add a colon after you click enter vs code will immediately indent this line for you and python is a programming language that really cares about indentation so I don't want to delete this I want to leave the indentation it means that every single line of code that I write here will live under this class scene so everything I write here will live under this you can also collapse this and expand this if you want to finish creating the scene we're also going to say def construct and inside of parentheses self and at the end add a colon this just created a new scene with the name of xd1 23 and every single line of code in this scene has to live under de construct self so don't delete the indentation if I write something here this text here is now living under class xd1 123 scene I want everything to live under deaf construct self if I want it to be included in this scene so don't forget this double indentation okay and now we have an empty scene like an empty canvas that we can use to create animations so let's open the animation and see how it begins first there's this white square on the left so let's start by creating a white square in this case this square is actually a rectangle with equal sides so I'm going to say rectangle and then inside of parentheses I can choose between all of these parameters here this is something that vs code suggests if you don't see these suggestions you can click shift command space or shift control space on windows so here I can see that I can Define the height of the rectangle I'm going to say that height equals to something like 0. 5 also when I add a comma I can Define some more parameters and I can see here that there's height and there's also width I said that this needs to be a square so the height and the width need to be the same so I'll say width equals 0. 5 now we have this rectangle here but I also want to assign it to a variable I'll say rect 1 equals my rectangle and this is something that I just made up it's a variable so now whenever I say rect one python is going to understand that I mean this rectangle and now I have this rectangle but I've just created it and I haven't put it on the screen to put something on the screen in manim you use the self.
playay command if you want to show object appearing moving or disappearing on the screen you always want to use the self. playay command now inside of its parentheses I'm going to say write and then rect one it's going to use the right animation type to animate the rectangle on the screen so now to run this code I'm going to come here and say run python file it should open this terminal here and then click on the manim side view extension and select the name of my scene this name here xd1 123 since I have only one scene this is the only option and once I click the scene should be rendered and as you can see we have a rectangle at the bottom here I'm going to say self. we and then put three inside of parentheses this tells the animation to pause for 3 seconds now to reender this I can click command s or contrl S on Windows for Save and then it will rerender it now there's a 3 second pause after the rectangle appears on the screen beautiful but in the animation that we want to recreate this is fully white and my rectangle is empty so I'm going to come here inside of the rectangle parentheses add a comma after width and say fill uncore opacity equals 1 this sets The Fill opacity which is how visible The Filling of the rectangle is to one or 100% if I said 0.
25 that would mean 25% I'll rerun this animation with command s and now the rectangle is fully white now notice here that there are actually two rectangles and they're both white so so what I can do is just copy this one by the way inside of vs code if you come anywhere on the line that you want to copy and click control or command C and then control or command V it will immediately copy and paste this line without you having to select it so now we have the same thing but I want this to be the second rectangle so I'll change the variable so that these become separate things remember we just created this rectangle but we haven't put it on the screen because we didn't specify it anywhere inside of the self. playay command so copy this command and say let's play the second rectangle as well and let's see what happens there's the first one and the second one overlaps the first one so how do we move them I'm going to make this a little smaller and then I'll come after the first rectangle say period shift left * 5 this will shift the rectangle five times to the left let's run this code and see what happens so here's our first rectangle and here is the second one as you can see this one is is over to the left if I said shift right five times it should be over here yes I'm going to revert this back to left but what does this five mean by default the scene in manim is made up of 14x 8 squares if you want you can show the coordinate system like this you create a variable then say number plane add coordinates and then you play this on the screen which will show the coordinate system so this is how it looks like and as you can see this rectangle is shifted to the left by 1 2 3 4 5 squares and since we didn't Define another position for the second rectangle it's just in the center now there are a couple of different ways to move objects around in man the first one is with the shift command so you can say shift left by five and then shift up by two so it should end up here yes so it shifted left by five and up by two but this is not super efficient what we can also do is use another command I'll use it for the second rectangle which is period move uncore 2 which lets us move things to specific cordinates immediately without playing with left right up or down so here we want to add curly brackets and then we can Define coordinates like this X Y and Z the Z coordinate will always be zero because we're working with a 2d environment and you can simply pick whatever X and Y values you want so this is for the second square and in the animation that we want to read create the second Square starts off over there so exactly the opposite as the first Square which means that the x coordinate will be five and the y-coordinate will be zero since the square is on the xaxis so I'll say move to 5 0 0 and I'll also remove the coordinates so that they're no longer visible and right now the first square is here and the second one is here now let's see what happens next the second Square moved from there to the first Square square and that means we're going to write something inside of the self. playay command since something happened on the screen I'm going to add a few spaces here and say self.
playay and this time we don't need to write out anything on the screen since both of these objects are already on the screen we want to move the second rectangle to the first rectangle so I'm going to say rect 2 which is my second rectangle do animate do nextore 2 this command puts something next to another object and I have to Define next to what so I want the second rectangle to be next to the first rectangle so I'll say rect one and then I have to say on which side so do I want it to be up do I want it to be left down or right in this case I want the rectangle to move to the right side of the first Square so I'll say right so with this whole shebang will do is animate the second rectangle from its current position to end up next to the first rectangle angle on the right side of it let's see what we get perfect now again if I change it from right to something like up it will move over here but I want it on the right side since this is the animation that we want to recreate now just like next two you can use the shift or the move two commands to animate something happening on the screen for example here we put both of these rectangles in their positions before they appear on the screen but another thing we could have done is made them appear in the center and then instead of putting shift left five here I could have said self. playay right rectangle number one and then self. playay rectangle number one do animate do shift left by five squares so this puts the first rectangle in the center and then moves it to the left by five squares so I just want to show you that you can also use the shift or the move two commands inside of self.
playay by also adding animate next to the object that you want to animate okay so this is what we have right now let's go back to our animation and see what happens next so now these two squares transformed into five squares with different colors so first we need to create these five squares and then transform these two white squares into these five squares now we could do this with a loop but for the purposes of this tutorial I'm just going to create five more rectangles I'm going to name them R1 R2 R3 R4 and R5 by the way in vs code if you hold option or alt you can put multiple cursors and type at the same time so if I type now it types in all five lines which is very useful if you want to type the same thing so I want to type rectangle then open parentheses say height equals to 0. 5 and width equals to 0. 5 as well also let's do fill score opacity equals to 1 just like we did with the first two rectangles now instead of moving each of these rectangles next to each other like this like we did with the first two we can put all of them in a group and then arrange all of the objects in that group nicely next to each other it's a lot easier than doing this so I will name the group group and I'll say that it's equal to V Group which means Vector group and inside of the parentheses I can say what I want to put in the group so I want to put all of the rectangles or one or two or three 3 R4 and R5 now all of these are in a group I can still animate them individually but if I want I can animate the whole group which will animate all the objects that are inside that group but I also want to arrange all of them nicely in a straight line like this so I'll come to a new line say group.
arrange and empty parenthesis this arranges every object in a group left to right and puts their Collective Center in the center of the frame so everything will be nice and centered and arranged like this but of course now nothing will show up on the screen because I didn't put this group or any of these rectangles inside of self doplay so I'll come here to the bottom say self. playay right and group so it will use the right animation to create the rectangles on the screen let's see how it looks like there's the first Square the second one and then all five rectangles appear on the screen but I want to transform these two rectangles into these five rectangles so instead of right I'll use replacement transform and inside of parentheses I have to say transform from what to what so I want to transform from these two rectangles to these five rectangles these five rectangles are in a group so I can just say transform to the whole group but these two are separate right now so I can also put them in the group and then transform the first group of rectangles into another group that way it counts this as one object and this as another object and it will transform from this to this so I'll come here above because I want to create the group before I transform it and I'll name this group G2 say it's equal to vgroup and then inside of parentheses rect one and rect 2 so these two rectangles and then inside of self. playay I'll Transform from G2 to group so from this shape to this shape let's see how that looks like nice so now we have this but the rectangles here are colored in different colors now you can do do this many different ways for example I could come to the rectangle and add a comma say color equals red and now if I play this the first rectangle will be red but this is quite tedious so what you can also do is if you have objects in a group you can color that whole group so I'll say group because I named the group of all of these rectangles group right here I'll say set uncore color but I don't want all of them to have the same color in the animation that we want to replicate all of these have different color colors so I'll say set underscore color by underscore gradient and this will allow me to set a gradient a gradient is basically when one color changes from the other or multiple colors blend and change so the first color is red I'll say red comma by the way colors in manm need to be capitalized then I have orange then I have this sort of yellow and I think I used yellow and from these options I chose yellow C these are just different shades of the same color and finally I have green so what happened here is essentially we colored this group and inside of this group there are five of these rectangles from the color red going into Orange going into yellow and going into green so the gradient will start from the left and color these rectangles left to right let's hit command or contrl S to save and render and see what we get beautiful but I noticed that I didn't use the right green color for the very end this one is a little too blank so instead of green I'm going to use purecore green which will give me the purest green color 0255 it's as green as it gets so now we've come to this point in the animation let's see what happens next there are two rectangles that appear outside of all the squares and these are called surrounding rectangles you can create a surrounding rectangle that surrounds an object so let's come here to the bottom and create the rectangles since there are two I'm going to name the first one S1 say that it's equal to surrounding and then I can press tab to autocomplete to surrounding rectangle and inside of parentheses I have to say what it surrounds so in this case it surrounds five of these squares and remember we put all of them in a group so we can say that this rectangle surrounds the group now again this is not going to show up on the screen because we didn't put it inside of self.
playay so I'm going to come here say self. playay write and S1 which will animate made the surrounding rectangle on the screen as well let's see how that looks like nice well the rectangle is yellow and in the animation that we want to make it's white so I'm going to come here to the surrounding rectangle add a comma and see if there are any more parameters for color and there are so I'm going to say color equals white but I also want the second surrounding rectangle surrounding the first rectangle so I'll say S2 is equal to surrounding rectangle and this time I wanted to surround the first surrounding rectangle angle and I also want it to be white and as well as writing S1 on the screen I want to add a comma and write S2 on the screen as well if I put two things or two animations inside of self. playay at the same time it will play both of them on the screen at the same time so let's see what we get beautiful now I also noticed that two of these rectangles in the animation don't get drawn evenly so the second one takes a little bit more time time and lags behind the first one and in our animation both of them appear at the same time so how do we change that inside of self.
playay you can Define how long an animation runs with the run time command so if I add a comma and say runor time equals to three this animation will run for 3 seconds by default it's 1 second so if I play this both of the rectangles should be drawn over a period of 3 seconds see it's a lot slower but in this case I want only the second Rec rectangle to be drawn slower so I can copy this command and put it inside of this right command so if it's inside these parentheses it will only apply to the thing that's being drawn here and instead of saying three I'm going to say something like 1. 5 so now the second rectangle should be drawn slower than the first one by 0. 5 seconds beautiful let's look at what's next now we have a bunch of numbers that appeared on top of each rectangle and numbers are basically text you can create text with the text command and inside of parentheses as well as quotation marks type out your text so I'm going to say 1 2 3 four and five now this is just text on its own so I'm going to assign it to a variable T you can choose whatever you want here I just chose the letter T and this text by default should be in the center of the screen which means it will cover up the rectangles so let's place it above this shape the outer thing here is the second surrounding rectangle so I'm going to say period next under _ 2 the second surrounding rectangle and up so now this text should be next to the second surrounding rectangle the second line here and it should be above it now if I would play this nothing would show up on the screen because I didn't put my text inside of the self.
playay command so I'm going to say self. playay write and T to write out my text and if I play this there should be text above all of the squares beautiful but in this animation the text is a little big bigger and it seems like every single number aligns with the rectangle so what we can do is add another period and say scale 1. 5 which will make this text bigger by 1.
5 times I could have also increased the font by adding a comma here and saying font uncore size equals to something like 96 but this time I chose to do it with the scale command so now if I play this the text should be a little bigger nice also what I could have done to have every single number as a separate variable was to create a new one for each number so I could have said T1 is equal to text and then one the same with T2 and so on this probably would have been a little smarter but the reason I did it this way was to show you how arrays work in Python so let's see what happens next now each number becomes bigger as each rectangle becomes smaller and it goes from the first one to the fifth one since something is happening on the screen we're going to use the self. playay command once again and to make this sort of highlight getting bigger effect we're going to use the indicate command and inside of parentheses we want to say what we want to indicate so for now let's say t let's say that we want to indicate our text if I play this let's see what happens so now all of the numbers jumped up and became yellow in this animation they jump up and become red so we can say indicate T then add a comma and Define a color and I'll say the color is red now if I play this all the numbers will jump up and they will become red instead of yellow but the problem now is that these numbers here highlight one by one and all of our numbers highlight all at the same time and this is why it was probably better to create each text one by one that way we could have indicated each one but there is another way of course you see each symbol inside of the text command act as object inside of a python array so we can call them one by one by adding curly brackets each object inside of an array has its own index the first object is number zero the second one number one the third one number two and so on so 0 1 2 3 4 5 6 7 8 we have eight objects here and spaces count as objects as well so now I want to indicate the first number which is object number zero so I'll say indicate t0 with color red and now if I play this only the first number should jump up with color red yes but now at the same time that this is happening we also want the rectangle to shrink and also become red so inside of the same self. playay command I'm going to add a comma and Define another animation that I want to play so this time we're going to indicate the first Square I'll say indicate add parentheses then I'll say what I want to indicate this is rectangle number one then add a comma say color equals red since by default the color is yellow and now if I play this both the number and the rectangle should indicate yes but the problem is that this rectangle also jumps up instead of shrinking down and we can change that here by adding another comma and looking through these parameters to find something that will let us make it smaller it says here that by default the scale factor is 1.
2 which means the object increases its size by 1. 2 so I'll say scale underscore factor is equal to 0. 5 which will now shrink it instead of expanding it and let's see what happens now so the number expands and the rectangle shrinks I think it shrinks a little more in this animation compared to our animation so I'm going to decrease this from 0.
5 to 0. 3 and now we're done with the first number but this whole shebang here will be very similar for other numbers so I can just click crl C and contrl V to copy and paste and all I have have to do is change this and this to be the second number and the second rectangle the rectangle is easy I'll say R2 and now I want the number two to indicate so this is object number zero this is one and this is two so I'll say indicate T2 which should indicate the number two I'll copy this once again now I want the third rectangle and the number three so 0 1 2 3 4 this is object number four copy once again this is object number six and rectangle number four and once again rectangle number five and object number 8 and now if I play this every single number should indicate but here it says list index out of range which means that probably spaces don't count as objects so I'm taking back everything I said about arrays so it turns out that for some reason this is the object number zero this is object number one number two number three and number four so spaces don't count so instead of saying 0 2 4 6 8 eight I'm going to say 0 1 2 3 and 4 and let's see if that works yes so we should get the same animation now I noticed that here when these numbers bloat up they don't actually overlap with this whole shape and the Gap here is a little bigger for me the Gap is very small so I'm going to come here where I said where the numbers should be on the screen I said next to the second surrounding rectangle and up and I'm going to add a comma and add a buff value of 0. 1 buff basically means the space between two objects so in this case the space between the numbers and the object that they're next to so this little space over here and if I play this the numbers should be a little higher just kidding that means I chose a lesser buff value than the default one so I'm going to say 0.
3 and see how that looks like now the numbers are high enough okay let's see what happens next so everything that's on the screen slowly transforms into a tiny Dot and then this dot expands and Fades to Black so first let's create the dot I'm going to call it D equals to Dot and inside of parentheses I'll say color equals red so we created a red dot now we want everything on the screen to transform into this dot so since we want to see something happening on the screen we're going to use the self doplay command again and inside of it we're going to use the same replacement transform command that we used before so replacement transform and now we want to say transform from what to what again we don't have all of these objects counting as one object right now so we need to put them in a group I'll name this group G3 and say that it's equal to vgroup and inside of its parentheses I have to put everything that is on the screen so we have all of these rectangles which are already in a group so I can just put group here so now all of them are in this group then I have the numbers which I called T and then I have two surrounding rectangles S1 and S2 now everything currently visible on the screen is inside of this G3 group and now I can transform from G3 to my dot let's see how that looks like WP and finally we want this dot to start expanding and once it expands and fills the whole screen we want it to fade to black so since something is happening on the screen I'll say self. playay open parenthesis and now I want to increase the size of the dot so I'll say d. animate do scale and let's scale it to something like 150 times since the dot is pretty small now again I wouldn't need to use the animate command if I wanted to scale the dot offc screen so if I wanted to make the dot super big before I showed it on the screen and I didn't want to animate it getting bigger I could have just came here said period scale to something like 30 times and it would have had the scale 30 immediately if it's not inside of sou dopl we don't have to use the animate command so now if I play this everything will transform into a large dot so let's undo that and make it transform into to a little Dot and then let's scale this dot once it already is on the screen so this is how it looks like now so this is how it looks like now next we want to fade out this Dot and there's an animation for that as well I'll say self.