{"id":2495,"date":"2025-06-07T10:31:57","date_gmt":"2025-06-07T10:31:57","guid":{"rendered":"https:\/\/diznr.com\/?p=2495"},"modified":"2025-06-07T10:31:57","modified_gmt":"2025-06-07T10:31:57","slug":"how-to-animate-ball-in-2d-dimensions-step-by-step-guide-with-demo-live","status":"publish","type":"post","link":"https:\/\/www.reilsolar.com\/pdf\/how-to-animate-ball-in-2d-dimensions-step-by-step-guide-with-demo-live\/","title":{"rendered":"How to Animate ball in 2D Dimensions Step by Step Guide with Live Demo"},"content":{"rendered":"<p>How to Animate ball in 2D Dimensions Step by Step Guide with Live Demo.<\/p>\n<p>[fvplayer id=&#8221;52&#8243;]<\/p>\n<p class=\"\" data-start=\"0\" data-end=\"248\">Animating a <strong data-start=\"12\" data-end=\"23\">2D ball<\/strong> can be done using various tools like <strong data-start=\"61\" data-end=\"135\">Adobe Animate, Unity, Python (Pygame), HTML5 Canvas, or CSS animations<\/strong>. Here\u2019s a <strong data-start=\"146\" data-end=\"168\">step-by-step guide<\/strong> to animate a ball using <strong data-start=\"193\" data-end=\"224\">HTML5 Canvas and JavaScript<\/strong> with a <strong data-start=\"232\" data-end=\"245\">live demo<\/strong>.<\/p>\n<h3 class=\"\" data-start=\"255\" data-end=\"323\"><strong data-start=\"259\" data-end=\"323\">Step-by-Step Guide to Animating a 2D Ball Using HTML5 Canvas<\/strong><\/h3>\n<h4 class=\"\" data-start=\"324\" data-end=\"362\"><strong data-start=\"329\" data-end=\"360\">Step 1: Create an HTML File<\/strong><\/h4>\n<p class=\"\" data-start=\"363\" data-end=\"427\">First, set up the HTML structure where the ball will be drawn.<\/p>\n<div class=\"contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary\">\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"!whitespace-pre language-html\"><span class=\"hljs-meta\">&lt;!DOCTYPE <span class=\"hljs-keyword\">html<\/span><\/span>&gt;<br \/>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">html<\/span><\/span> <span class=\"hljs-attr\">lang<\/span>=<span class=\"hljs-string\">\"en\"<\/span>&gt;<br \/>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">head<\/span><\/span>&gt;<br \/>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">meta<\/span><\/span> <span class=\"hljs-attr\">charset<\/span>=<span class=\"hljs-string\">\"UTF-8\"<\/span>&gt;<br \/>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">meta<\/span><\/span> <span class=\"hljs-attr\">name<\/span>=<span class=\"hljs-string\">\"viewport\"<\/span> <span class=\"hljs-attr\">content<\/span>=<span class=\"hljs-string\">\"width=device-width, initial-scale=1.0\"<\/span>&gt;<br \/>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">title<\/span><\/span>&gt;2D Ball Animation<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">title<\/span><\/span>&gt;<br \/>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">style<\/span><\/span>&gt;<br \/>\n<span class=\"hljs-selector-tag\">canvas<\/span> {<br \/>\n<span class=\"hljs-attribute\">background<\/span>: <span class=\"hljs-number\">#f0f0f0<\/span>;<br \/>\n<span class=\"hljs-attribute\">display<\/span>: block;<br \/>\n<span class=\"hljs-attribute\">margin<\/span>: auto;<br \/>\n}<br \/>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">style<\/span><\/span>&gt;<br \/>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">head<\/span><\/span>&gt;<br \/>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">body<\/span><\/span>&gt;<br \/>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">canvas<\/span><\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"myCanvas\"<\/span>&gt;<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">canvas<\/span><\/span>&gt;<br \/>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">script<\/span><\/span> <span class=\"hljs-attr\">src<\/span>=<span class=\"hljs-string\">\"script.js\"<\/span>&gt;<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">script<\/span><\/span>&gt;<br \/>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">body<\/span><\/span>&gt;<br \/>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">html<\/span><\/span>&gt;<br \/>\n<\/code><\/div>\n<\/div>\n<h4 class=\"\" data-start=\"870\" data-end=\"922\"><strong data-start=\"875\" data-end=\"922\">Step 2: Set Up the JavaScript for Animation<\/strong><\/h4>\n<p class=\"\" data-start=\"923\" data-end=\"980\">Create a <code data-start=\"932\" data-end=\"943\">script.js<\/code> file to draw and animate the ball.<\/p>\n<div class=\"contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary\">\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"!whitespace-pre language-javascript\"><code class=\"!whitespace-pre language-javascript\"><span class=\"hljs-keyword\">const<\/span> canvas = <span class=\"hljs-variable language_\">document<\/span>.<span class=\"hljs-title function_\">getElementById<\/span>(<span class=\"hljs-string\">\"myCanvas\"<\/span>);<br \/>\n<span class=\"hljs-keyword\">const<\/span> ctx = canvas.<span class=\"hljs-title function_\">getContext<\/span>(<span class=\"hljs-string\">\"2d\"<\/span>);<\/code><\/code><span class=\"hljs-comment\">\/\/ Set canvas width &amp; height<\/span><br \/>\ncanvas.<span class=\"hljs-property\">width<\/span> = <span class=\"hljs-number\">500<\/span>;<br \/>\ncanvas.<span class=\"hljs-property\">height<\/span> = <span class=\"hljs-number\">300<\/span>;<code class=\"!whitespace-pre language-javascript\"><code class=\"!whitespace-pre language-javascript\"><\/code><\/code><span class=\"hljs-comment\">\/\/ Ball properties<\/span><br \/>\n<span class=\"hljs-keyword\">let<\/span> ball = {<br \/>\n<span class=\"hljs-attr\">x<\/span>: <span class=\"hljs-number\">50<\/span>,<br \/>\n<span class=\"hljs-attr\">y<\/span>: <span class=\"hljs-number\">50<\/span>,<br \/>\n<span class=\"hljs-attr\">radius<\/span>: <span class=\"hljs-number\">20<\/span>,<br \/>\n<span class=\"hljs-attr\">dx<\/span>: <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-comment\">\/\/ X velocity<\/span><br \/>\n<span class=\"hljs-attr\">dy<\/span>: <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-comment\">\/\/ Y velocity<\/span><br \/>\n<span class=\"hljs-attr\">color<\/span>: <span class=\"hljs-string\">&#8220;blue&#8221;<\/span><br \/>\n};<code class=\"!whitespace-pre language-javascript\"><code class=\"!whitespace-pre language-javascript\"><\/code><\/code><span class=\"hljs-comment\">\/\/ Function to draw the ball<\/span><br \/>\n<span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title function_\">drawBall<\/span>() {<br \/>\nctx.<span class=\"hljs-title function_\">beginPath<\/span>();<br \/>\nctx.<span class=\"hljs-title function_\">arc<\/span>(ball.<span class=\"hljs-property\">x<\/span>, ball.<span class=\"hljs-property\">y<\/span>, ball.<span class=\"hljs-property\">radius<\/span>, <span class=\"hljs-number\">0<\/span>, <span class=\"hljs-title class_\">Math<\/span>.<span class=\"hljs-property\">PI<\/span> * <span class=\"hljs-number\">2<\/span>);<br \/>\nctx.<span class=\"hljs-property\">fillStyle<\/span> = ball.<span class=\"hljs-property\">color<\/span>;<br \/>\nctx.<span class=\"hljs-title function_\">fill<\/span>();<br \/>\nctx.<span class=\"hljs-title function_\">closePath<\/span>();<br \/>\n}<\/p>\n<p><code class=\"!whitespace-pre language-javascript\"><code class=\"!whitespace-pre language-javascript\"><\/code><\/code><span class=\"hljs-comment\">\/\/ Function to update ball position<\/span><br \/>\n<span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title function_\">update<\/span>() {<br \/>\nctx.<span class=\"hljs-title function_\">clearRect<\/span>(<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">0<\/span>, canvas.<span class=\"hljs-property\">width<\/span>, canvas.<span class=\"hljs-property\">height<\/span>); <span class=\"hljs-comment\">\/\/ Clear the canvas<\/span><\/p>\n<p><code class=\"!whitespace-pre language-javascript\"><code class=\"!whitespace-pre language-javascript\"><\/code><\/code><span class=\"hljs-title function_\">drawBall<\/span>();<\/p>\n<p><code class=\"!whitespace-pre language-javascript\"><code class=\"!whitespace-pre language-javascript\"><\/code><\/code><span class=\"hljs-comment\">\/\/ Move the ball<\/span><br \/>\nball.<span class=\"hljs-property\">x<\/span> += ball.<span class=\"hljs-property\">dx<\/span>;<br \/>\nball.<span class=\"hljs-property\">y<\/span> += ball.<span class=\"hljs-property\">dy<\/span>;<\/p>\n<p><code class=\"!whitespace-pre language-javascript\"><code class=\"!whitespace-pre language-javascript\"><\/code><\/code><span class=\"hljs-comment\">\/\/ Bounce the ball off the walls<\/span><br \/>\n<span class=\"hljs-keyword\">if<\/span> (ball.<span class=\"hljs-property\">x<\/span> + ball.<span class=\"hljs-property\">radius<\/span> &gt; canvas.<span class=\"hljs-property\">width<\/span> || ball.<span class=\"hljs-property\">x<\/span> &#8211; ball.<span class=\"hljs-property\">radius<\/span> &lt; <span class=\"hljs-number\">0<\/span>) {<br \/>\nball.<span class=\"hljs-property\">dx<\/span> *= &#8211;<span class=\"hljs-number\">1<\/span>; <span class=\"hljs-comment\">\/\/ Reverse direction<\/span><br \/>\n}<br \/>\n<span class=\"hljs-keyword\">if<\/span> (ball.<span class=\"hljs-property\">y<\/span> + ball.<span class=\"hljs-property\">radius<\/span> &gt; canvas.<span class=\"hljs-property\">height<\/span> || ball.<span class=\"hljs-property\">y<\/span> &#8211; ball.<span class=\"hljs-property\">radius<\/span> &lt; <span class=\"hljs-number\">0<\/span>) {<br \/>\nball.<span class=\"hljs-property\">dy<\/span> *= &#8211;<span class=\"hljs-number\">1<\/span>; <span class=\"hljs-comment\">\/\/ Reverse direction<\/span><br \/>\n}<\/p>\n<p><code class=\"!whitespace-pre language-javascript\"><code class=\"!whitespace-pre language-javascript\"><\/code><\/code><span class=\"hljs-title function_\">requestAnimationFrame<\/span>(update); <span class=\"hljs-comment\">\/\/ Recursive animation loop<\/span><br \/>\n}<\/p>\n<p><code class=\"!whitespace-pre language-javascript\"><code class=\"!whitespace-pre language-javascript\"><\/code><\/code><span class=\"hljs-comment\">\/\/ Start animation<\/span><br \/>\n<span class=\"hljs-title function_\">update<\/span>();<\/p>\n<\/div>\n<\/div>\n<h3 class=\"\" data-start=\"2115\" data-end=\"2135\"><strong data-start=\"2119\" data-end=\"2135\">How It Works<\/strong><\/h3>\n<ol data-start=\"2136\" data-end=\"2548\">\n<li class=\"\" data-start=\"2136\" data-end=\"2192\">\n<p class=\"\" data-start=\"2139\" data-end=\"2192\"><strong data-start=\"2139\" data-end=\"2164\">Create an HTML Canvas<\/strong> where the ball will move.<\/p>\n<\/li>\n<li class=\"\" data-start=\"2193\" data-end=\"2258\">\n<p class=\"\" data-start=\"2196\" data-end=\"2258\"><strong data-start=\"2196\" data-end=\"2222\">Define ball properties<\/strong> (position, speed, color, radius).<\/p>\n<\/li>\n<li class=\"\" data-start=\"2259\" data-end=\"2318\">\n<p class=\"\" data-start=\"2262\" data-end=\"2318\"><strong data-start=\"2262\" data-end=\"2282\">Use <code data-start=\"2268\" data-end=\"2280\">drawBall()<\/code><\/strong> to render the ball on the canvas.<\/p>\n<\/li>\n<li class=\"\" data-start=\"2319\" data-end=\"2399\">\n<p class=\"\" data-start=\"2322\" data-end=\"2399\"><strong data-start=\"2322\" data-end=\"2340\">Use <code data-start=\"2328\" data-end=\"2338\">update()<\/code><\/strong> to move the ball by changing its position (<code data-start=\"2384\" data-end=\"2387\">x<\/code> and <code data-start=\"2392\" data-end=\"2395\">y<\/code>).<\/p>\n<\/li>\n<li class=\"\" data-start=\"2400\" data-end=\"2479\">\n<p class=\"\" data-start=\"2403\" data-end=\"2479\"><strong data-start=\"2403\" data-end=\"2442\">Use <code data-start=\"2409\" data-end=\"2440\">requestAnimationFrame(update)<\/code><\/strong> to create a smooth animation loop.<\/p>\n<\/li>\n<li class=\"\" data-start=\"2480\" data-end=\"2548\">\n<p class=\"\" data-start=\"2483\" data-end=\"2548\"><strong data-start=\"2483\" data-end=\"2512\">Check for wall collisions<\/strong> to bounce the ball off the edges.<\/p>\n<\/li>\n<\/ol>\n<h3 class=\"\" data-start=\"2555\" data-end=\"2572\"><strong data-start=\"2559\" data-end=\"2572\">Live Demo<\/strong><\/h3>\n<p class=\"\" data-start=\"2573\" data-end=\"2690\">You can copy-paste this code into an HTML file and open it in a browser to see the <strong data-start=\"2656\" data-end=\"2673\">animated ball<\/strong> in action!<\/p>\n<p class=\"\" data-start=\"2692\" data-end=\"2801\">Would you like me to modify this animation, such as adding gravity or making the ball react to user input?<\/p>\n<p data-start=\"0\" data-end=\"271\">Animating a ball in <strong data-start=\"20\" data-end=\"37\">2D dimensions<\/strong> is a great beginner project to understand basic animation concepts like <strong data-start=\"110\" data-end=\"122\">position<\/strong>, <strong data-start=\"124\" data-end=\"136\">velocity<\/strong>, <strong data-start=\"138\" data-end=\"148\">timing<\/strong>, and <strong data-start=\"154\" data-end=\"171\">frame updates<\/strong>. Here&#8217;s a <strong data-start=\"182\" data-end=\"204\">step-by-step guide<\/strong> with a <strong data-start=\"212\" data-end=\"270\">live demo example using HTML + JavaScript (Canvas API)<\/strong>.<\/p>\n<hr data-start=\"273\" data-end=\"276\" \/>\n<h2 data-start=\"278\" data-end=\"328\">\ud83c\udfac <strong data-start=\"284\" data-end=\"328\">Step-by-Step Guide: Animate a Ball in 2D<\/strong><\/h2>\n<hr data-start=\"330\" data-end=\"333\" \/>\n<h3 data-start=\"335\" data-end=\"363\">\ud83d\udee0\ufe0f <strong data-start=\"343\" data-end=\"363\">Tools You\u2019ll Use<\/strong><\/h3>\n<ul data-start=\"364\" data-end=\"465\">\n<li data-start=\"364\" data-end=\"390\">\n<p data-start=\"366\" data-end=\"390\">HTML5 <code data-start=\"372\" data-end=\"382\">&lt;canvas&gt;<\/code> element<\/p>\n<\/li>\n<li data-start=\"391\" data-end=\"431\">\n<p data-start=\"393\" data-end=\"431\">JavaScript (for drawing and animation)<\/p>\n<\/li>\n<li data-start=\"432\" data-end=\"465\">\n<p data-start=\"434\" data-end=\"465\">A browser (Chrome\/Edge\/Firefox)<\/p>\n<\/li>\n<\/ul>\n<hr data-start=\"467\" data-end=\"470\" \/>\n<h3 data-start=\"472\" data-end=\"512\">\u2705 <strong data-start=\"478\" data-end=\"512\">Step 1: Set Up HTML and Canvas<\/strong><\/h3>\n<div class=\"contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary\">\n<div class=\"flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none rounded-t-[5px]\">html<\/div>\n<div class=\"sticky top-9\">\n<div class=\"absolute end-0 bottom-0 flex h-9 items-center pe-2\">\n<div class=\"bg-token-sidebar-surface-primary text-token-text-secondary dark:bg-token-main-surface-secondary flex items-center rounded-sm px-2 font-sans text-xs\"><button class=\"flex gap-1 items-center select-none px-4 py-1\" aria-label=\"Copy\">Copy<\/button><span class=\"\" data-state=\"closed\"><button class=\"flex items-center gap-1 px-4 py-1 select-none\">Edit<\/button><\/span><\/div>\n<\/div>\n<\/div>\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"whitespace-pre! language-html\"><code class=\"whitespace-pre! language-html\"><span class=\"hljs-meta\">&lt;!DOCTYPE <span class=\"hljs-keyword\">html<\/span><\/span>&gt;<br \/>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">html<\/span><\/span>&gt;<br \/>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">head<\/span><\/span>&gt;<br \/>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">title<\/span><\/span>&gt;2D Ball Animation<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">title<\/span><\/span>&gt;<br \/>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">style<\/span><\/span>&gt;<br \/>\n<span class=\"hljs-selector-tag\">canvas<\/span> {<br \/>\n<span class=\"hljs-attribute\">background-color<\/span>: <span class=\"hljs-number\">#f0f0f0<\/span>;<br \/>\n<span class=\"hljs-attribute\">display<\/span>: block;<br \/>\n<span class=\"hljs-attribute\">margin<\/span>: <span class=\"hljs-number\">50px<\/span> auto;<br \/>\n<span class=\"hljs-attribute\">border<\/span>: <span class=\"hljs-number\">1px<\/span> solid <span class=\"hljs-number\">#ccc<\/span>;<br \/>\n}<br \/>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">style<\/span><\/span>&gt;<br \/>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">head<\/span><\/span>&gt;<br \/>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">body<\/span><\/span>&gt;<br \/>\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">canvas<\/span><\/span> <span class=\"hljs-attr\">id<\/span>=<span class=\"hljs-string\">\"ballCanvas\"<\/span> <span class=\"hljs-attr\">width<\/span>=<span class=\"hljs-string\">\"600\"<\/span> <span class=\"hljs-attr\">height<\/span>=<span class=\"hljs-string\">\"400\"<\/span>&gt;<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">canvas<\/span><\/span>&gt;<\/code><\/code><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">script<\/span><\/span> <span class=\"hljs-attr\">src<\/span>=<span class=\"hljs-string\">&#8220;ball-animation.js&#8221;<\/span>&gt;<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">script<\/span><\/span>&gt;<br \/>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">body<\/span><\/span>&gt;<br \/>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">html<\/span><\/span>&gt;<\/div>\n<\/div>\n<hr data-start=\"879\" data-end=\"882\" \/>\n<h3 data-start=\"884\" data-end=\"944\">\u2705 <strong data-start=\"890\" data-end=\"944\">Step 2: Create JavaScript File (ball-animation.js)<\/strong><\/h3>\n<div class=\"contain-inline-size rounded-md border-[0.5px] border-token-border-medium relative bg-token-sidebar-surface-primary\">\n<div class=\"flex items-center text-token-text-secondary px-4 py-2 text-xs font-sans justify-between h-9 bg-token-sidebar-surface-primary dark:bg-token-main-surface-secondary select-none rounded-t-[5px]\">javascript<\/div>\n<div class=\"sticky top-9\">\n<div class=\"absolute end-0 bottom-0 flex h-9 items-center pe-2\">\n<div class=\"bg-token-sidebar-surface-primary text-token-text-secondary dark:bg-token-main-surface-secondary flex items-center rounded-sm px-2 font-sans text-xs\"><button class=\"flex gap-1 items-center select-none px-4 py-1\" aria-label=\"Copy\">Copy<\/button><span class=\"\" data-state=\"closed\"><button class=\"flex items-center gap-1 px-4 py-1 select-none\">Edit<\/button><\/span><\/div>\n<\/div>\n<\/div>\n<div class=\"overflow-y-auto p-4\" dir=\"ltr\"><code class=\"whitespace-pre! language-javascript\"><code class=\"whitespace-pre! language-javascript\"><span class=\"hljs-keyword\">const<\/span> canvas = <span class=\"hljs-variable language_\">document<\/span>.<span class=\"hljs-title function_\">getElementById<\/span>(<span class=\"hljs-string\">\"ballCanvas\"<\/span>);<br \/>\n<span class=\"hljs-keyword\">const<\/span> ctx = canvas.<span class=\"hljs-title function_\">getContext<\/span>(<span class=\"hljs-string\">\"2d\"<\/span>);<\/code><\/code><span class=\"hljs-comment\">\/\/ Ball properties<\/span><br \/>\n<span class=\"hljs-keyword\">let<\/span> x = canvas.<span class=\"hljs-property\">width<\/span> \/ <span class=\"hljs-number\">2<\/span>;<br \/>\n<span class=\"hljs-keyword\">let<\/span> y = canvas.<span class=\"hljs-property\">height<\/span> \/ <span class=\"hljs-number\">2<\/span>;<br \/>\n<span class=\"hljs-keyword\">let<\/span> dx = <span class=\"hljs-number\">2<\/span>; <span class=\"hljs-comment\">\/\/ horizontal velocity<\/span><br \/>\n<span class=\"hljs-keyword\">let<\/span> dy = <span class=\"hljs-number\">2<\/span>; <span class=\"hljs-comment\">\/\/ vertical velocity<\/span><br \/>\n<span class=\"hljs-keyword\">let<\/span> radius = <span class=\"hljs-number\">20<\/span>;<code class=\"whitespace-pre! language-javascript\"><code class=\"whitespace-pre! language-javascript\"><\/code><\/code><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title function_\">drawBall<\/span>() {<br \/>\nctx.<span class=\"hljs-title function_\">beginPath<\/span>();<br \/>\nctx.<span class=\"hljs-title function_\">arc<\/span>(x, y, radius, <span class=\"hljs-number\">0<\/span>, <span class=\"hljs-title class_\">Math<\/span>.<span class=\"hljs-property\">PI<\/span> * <span class=\"hljs-number\">2<\/span>);<br \/>\nctx.<span class=\"hljs-property\">fillStyle<\/span> = <span class=\"hljs-string\">&#8220;#007BFF&#8221;<\/span>;<br \/>\nctx.<span class=\"hljs-title function_\">fill<\/span>();<br \/>\nctx.<span class=\"hljs-title function_\">closePath<\/span>();<br \/>\n}<\/p>\n<p><code class=\"whitespace-pre! language-javascript\"><code class=\"whitespace-pre! language-javascript\"><\/code><\/code><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title function_\">update<\/span>() {<br \/>\n<span class=\"hljs-comment\">\/\/ Clear the canvas<\/span><br \/>\nctx.<span class=\"hljs-title function_\">clearRect<\/span>(<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">0<\/span>, canvas.<span class=\"hljs-property\">width<\/span>, canvas.<span class=\"hljs-property\">height<\/span>);<\/p>\n<p><code class=\"whitespace-pre! language-javascript\"><code class=\"whitespace-pre! language-javascript\"><\/code><\/code><span class=\"hljs-comment\">\/\/ Draw the ball<\/span><br \/>\n<span class=\"hljs-title function_\">drawBall<\/span>();<\/p>\n<p><code class=\"whitespace-pre! language-javascript\"><code class=\"whitespace-pre! language-javascript\"><\/code><\/code><span class=\"hljs-comment\">\/\/ Bounce off the walls<\/span><br \/>\n<span class=\"hljs-keyword\">if<\/span> (x + dx &gt; canvas.<span class=\"hljs-property\">width<\/span> &#8211; radius || x + dx &lt; radius) {<br \/>\ndx = -dx;<br \/>\n}<br \/>\n<span class=\"hljs-keyword\">if<\/span> (y + dy &gt; canvas.<span class=\"hljs-property\">height<\/span> &#8211; radius || y + dy &lt; radius) {<br \/>\ndy = -dy;<br \/>\n}<\/p>\n<p><code class=\"whitespace-pre! language-javascript\"><code class=\"whitespace-pre! language-javascript\"><\/code><\/code><span class=\"hljs-comment\">\/\/ Move the ball<\/span><br \/>\nx += dx;<br \/>\ny += dy;<\/p>\n<p><code class=\"whitespace-pre! language-javascript\"><code class=\"whitespace-pre! language-javascript\"><\/code><\/code><span class=\"hljs-comment\">\/\/ Repeat the animation<\/span><br \/>\n<span class=\"hljs-title function_\">requestAnimationFrame<\/span>(update);<br \/>\n}<\/p>\n<p><code class=\"whitespace-pre! language-javascript\"><code class=\"whitespace-pre! language-javascript\"><\/code><\/code><span class=\"hljs-title function_\">update<\/span>(); <span class=\"hljs-comment\">\/\/ Start animation<\/span><\/p>\n<\/div>\n<\/div>\n<hr data-start=\"1806\" data-end=\"1809\" \/>\n<h2 data-start=\"1811\" data-end=\"1846\">\ud83d\udc40 <strong data-start=\"1817\" data-end=\"1846\">What Happens in the Demo?<\/strong><\/h2>\n<ul data-start=\"1847\" data-end=\"1971\">\n<li data-start=\"1847\" data-end=\"1896\">\n<p data-start=\"1849\" data-end=\"1896\">A blue ball appears at the center of the canvas<\/p>\n<\/li>\n<li data-start=\"1897\" data-end=\"1918\">\n<p data-start=\"1899\" data-end=\"1918\">It moves diagonally<\/p>\n<\/li>\n<li data-start=\"1919\" data-end=\"1971\">\n<p data-start=\"1921\" data-end=\"1971\">It <strong data-start=\"1924\" data-end=\"1935\">bounces<\/strong> when it hits the edge of the canvas<\/p>\n<\/li>\n<\/ul>\n<hr data-start=\"1973\" data-end=\"1976\" \/>\n<h2 data-start=\"1978\" data-end=\"2008\">\ud83e\udde0 <strong data-start=\"1984\" data-end=\"2008\">Key Concepts Covered<\/strong><\/h2>\n<div class=\"_tableContainer_16hzy_1\">\n<div class=\"_tableWrapper_16hzy_14 group flex w-fit flex-col-reverse\">\n<table class=\"w-fit min-w-(--thread-content-width)\" data-start=\"2010\" data-end=\"2333\">\n<thead data-start=\"2010\" data-end=\"2043\">\n<tr data-start=\"2010\" data-end=\"2043\">\n<th data-start=\"2010\" data-end=\"2028\" data-col-size=\"sm\">Concept<\/th>\n<th data-start=\"2028\" data-end=\"2043\" data-col-size=\"sm\">Description<\/th>\n<\/tr>\n<\/thead>\n<tbody data-start=\"2079\" data-end=\"2333\">\n<tr data-start=\"2079\" data-end=\"2133\">\n<td data-start=\"2079\" data-end=\"2098\" data-col-size=\"sm\"><code data-start=\"2081\" data-end=\"2089\">Canvas<\/code><\/td>\n<td data-start=\"2098\" data-end=\"2133\" data-col-size=\"sm\">The drawing area in the browser<\/td>\n<\/tr>\n<tr data-start=\"2134\" data-end=\"2192\">\n<td data-start=\"2134\" data-end=\"2162\" data-col-size=\"sm\"><code data-start=\"2136\" data-end=\"2161\">requestAnimationFrame()<\/code><\/td>\n<td data-start=\"2162\" data-end=\"2192\" data-col-size=\"sm\">Smooth animation at 60 fps<\/td>\n<\/tr>\n<tr data-start=\"2193\" data-end=\"2246\">\n<td data-start=\"2193\" data-end=\"2212\" data-col-size=\"sm\"><code data-start=\"2195\" data-end=\"2202\">dx\/dy<\/code><\/td>\n<td data-start=\"2212\" data-end=\"2246\" data-col-size=\"sm\">Velocity in x and y directions<\/td>\n<\/tr>\n<tr data-start=\"2247\" data-end=\"2295\">\n<td data-start=\"2247\" data-end=\"2266\" data-col-size=\"sm\"><code data-start=\"2249\" data-end=\"2266\">ctx.clearRect()<\/code><\/td>\n<td data-start=\"2266\" data-end=\"2295\" data-col-size=\"sm\">Clears the previous frame<\/td>\n<\/tr>\n<tr data-start=\"2296\" data-end=\"2333\">\n<td data-start=\"2296\" data-end=\"2315\" data-col-size=\"sm\"><code data-start=\"2298\" data-end=\"2309\">ctx.arc()<\/code><\/td>\n<td data-start=\"2315\" data-end=\"2333\" data-col-size=\"sm\">Draws the ball<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"sticky end-(--thread-content-margin) h-0 self-end select-none\">\n<div class=\"absolute end-0 flex items-end\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<hr data-start=\"2335\" data-end=\"2338\" \/>\n<h2 data-start=\"2340\" data-end=\"2363\">\ud83e\uddea Want to Try More?<\/h2>\n<p data-start=\"2365\" data-end=\"2373\">You can:<\/p>\n<ul data-start=\"2374\" data-end=\"2521\">\n<li data-start=\"2374\" data-end=\"2417\">\n<p data-start=\"2376\" data-end=\"2417\">Add gravity for a more realistic bounce<\/p>\n<\/li>\n<li data-start=\"2418\" data-end=\"2460\">\n<p data-start=\"2420\" data-end=\"2460\">Add user controls (arrow keys to move)<\/p>\n<\/li>\n<li data-start=\"2461\" data-end=\"2495\">\n<p data-start=\"2463\" data-end=\"2495\">Create multiple bouncing balls<\/p>\n<\/li>\n<li data-start=\"2496\" data-end=\"2521\">\n<p data-start=\"2498\" data-end=\"2521\">Add collision detection<\/p>\n<\/li>\n<\/ul>\n<hr data-start=\"2523\" data-end=\"2526\" \/>\n<h2 data-start=\"2528\" data-end=\"2575\">\ud83c\udf81 BONUS: Want a Live CodePen\/JSFiddle Link?<\/h2>\n<p data-start=\"2576\" data-end=\"2622\">Or a zip file with the code and editable demo?<\/p>\n<p data-start=\"2624\" data-end=\"2666\" data-is-last-node=\"\" data-is-only-node=\"\">Let me know and I\u2019ll generate one for you!<\/p>\n<h3 data-start=\"2624\" data-end=\"2666\"><a href=\"https:\/\/helpx.adobe.com\/pdf\/animate_reference.pdf\" target=\"_blank\" rel=\"noopener\">How to Animate ball in 2D Dimensions Step by Step Guide with Live Demo<\/a><\/h3>\n<h3 class=\"LC20lb MBeuO DKV0Md\"><a href=\"https:\/\/booksite.elsevier.com\/samplechapters\/9780240520544\/9780240520544.pdf\" target=\"_blank\" rel=\"noopener\">introduction to 2D-animation working practice<\/a><\/h3>\n","protected":false},"excerpt":{"rendered":"<p>How to Animate ball in 2D Dimensions Step by Step Guide with Live Demo <\/p>\n","protected":false},"author":64,"featured_media":2496,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1619,1536],"tags":[2003,2004],"class_list":["post-2495","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-3d-game-design","category-animation","tag-how-to-animate-ball-in-2d-dimensions","tag-step-by-step-guide-with-live-demo-of-2d-dimension"],"_links":{"self":[{"href":"https:\/\/www.reilsolar.com\/pdf\/wp-json\/wp\/v2\/posts\/2495","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.reilsolar.com\/pdf\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.reilsolar.com\/pdf\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.reilsolar.com\/pdf\/wp-json\/wp\/v2\/users\/64"}],"replies":[{"embeddable":true,"href":"https:\/\/www.reilsolar.com\/pdf\/wp-json\/wp\/v2\/comments?post=2495"}],"version-history":[{"count":0,"href":"https:\/\/www.reilsolar.com\/pdf\/wp-json\/wp\/v2\/posts\/2495\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.reilsolar.com\/pdf\/wp-json\/wp\/v2\/media\/2496"}],"wp:attachment":[{"href":"https:\/\/www.reilsolar.com\/pdf\/wp-json\/wp\/v2\/media?parent=2495"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.reilsolar.com\/pdf\/wp-json\/wp\/v2\/categories?post=2495"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.reilsolar.com\/pdf\/wp-json\/wp\/v2\/tags?post=2495"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}