{"id":24546,"date":"2024-03-07T12:16:49","date_gmt":"2024-03-07T11:16:49","guid":{"rendered":"https:\/\/nelkodev.com\/blog\/introduccion-al-desarrollo-de-videojuegos-con-javascript-y-html5-canvas\/"},"modified":"2024-06-03T18:44:17","modified_gmt":"2024-06-03T17:44:17","slug":"introduccion-al-desarrollo-de-videojuegos-con-javascript-y-html5-canvas","status":"publish","type":"post","link":"https:\/\/nelkodev.com\/en\/blog\/introduction-to-video-game-development-with-javascript-and-html5-canvas\/","title":{"rendered":"Introduction to Video Game Development with JavaScript and HTML5 Canvas"},"content":{"rendered":"<p>Video game development is one of the most interesting and creative branches within programming. It offers a unique combination of art, narrative, music and, of course, technology. With the arrival of HTML5 and the power of JavaScript, the possibility of creating dynamic and attractive games has opened up directly in our browser, accessible to a wide audience. In this article we are going to dive into the world of game development using JavaScript and the HTML5 Canvas element.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_80 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #ffffff;color:#ffffff\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #ffffff;color:#ffffff\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewbox=\"0 0 24 24\" version=\"1.2\" baseprofile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/nelkodev.com\/en\/blog\/introduction-to-video-game-development-with-javascript-and-html5-canvas\/#%C2%BFPor_Que_JavaScript_y_HTML5_Canvas_para_Desarrollo_de_Juegos\" >Why JavaScript and HTML5 Canvas for Game Development?<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/nelkodev.com\/en\/blog\/introduction-to-video-game-development-with-javascript-and-html5-canvas\/#HTML5_Canvas\" >HTML5 Canvas<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/nelkodev.com\/en\/blog\/introduction-to-video-game-development-with-javascript-and-html5-canvas\/#JavaScript_y_su_Ecosistema\" >JavaScript and its Ecosystem<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/nelkodev.com\/en\/blog\/introduction-to-video-game-development-with-javascript-and-html5-canvas\/#Comenzando_con_el_Desarrollo_de_Videojuegos_en_JavaScript\" >Getting Started with Video Game Development in JavaScript<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/nelkodev.com\/en\/blog\/introduction-to-video-game-development-with-javascript-and-html5-canvas\/#Configurando_el_Entorno_de_Desarrollo\" >Setting up the Development Environment<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/nelkodev.com\/en\/blog\/introduction-to-video-game-development-with-javascript-and-html5-canvas\/#Creando_un_Canvas\" >Creating a Canvas<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/nelkodev.com\/en\/blog\/introduction-to-video-game-development-with-javascript-and-html5-canvas\/#Dibujando_en_el_Canvas\" >Drawing on the Canvas<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/nelkodev.com\/en\/blog\/introduction-to-video-game-development-with-javascript-and-html5-canvas\/#Animacion_Basica\" >Basic Animation<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/nelkodev.com\/en\/blog\/introduction-to-video-game-development-with-javascript-and-html5-canvas\/#Deteccion_de_Entradas_del_Usuario\" >User Input Detection<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/nelkodev.com\/en\/blog\/introduction-to-video-game-development-with-javascript-and-html5-canvas\/#Estructuracion_de_un_Simple_Juego_en_Canvas\" >Structuring a Simple Canvas Game<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/nelkodev.com\/en\/blog\/introduction-to-video-game-development-with-javascript-and-html5-canvas\/#Creacion_del_Juego_Pong_en_JavaScript\" >Creating the Pong Game in JavaScript<\/a><ul class='ez-toc-list-level-4' ><li class='ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/nelkodev.com\/en\/blog\/introduction-to-video-game-development-with-javascript-and-html5-canvas\/#Estructura_Basica_del_Juego\" >Basic Game Structure<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/nelkodev.com\/en\/blog\/introduction-to-video-game-development-with-javascript-and-html5-canvas\/#Logica_del_Juego\" >Game Logic<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-4'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/nelkodev.com\/en\/blog\/introduction-to-video-game-development-with-javascript-and-html5-canvas\/#Implementacion_del_Juego\" >Game Implementation<\/a><\/li><\/ul><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/nelkodev.com\/en\/blog\/introduction-to-video-game-development-with-javascript-and-html5-canvas\/#Mejores_Practicas_y_Sugerencias_Avanzadas\" >Best Practices and Advanced Tips<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/nelkodev.com\/en\/blog\/introduction-to-video-game-development-with-javascript-and-html5-canvas\/#Patrones_de_Diseno\" >Design patterns<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/nelkodev.com\/en\/blog\/introduction-to-video-game-development-with-javascript-and-html5-canvas\/#Motores_y_Frameworks\" >Engines and Frameworks<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-18\" href=\"https:\/\/nelkodev.com\/en\/blog\/introduction-to-video-game-development-with-javascript-and-html5-canvas\/#Optimizacion\" >Optimization<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"%C2%BFPor_Que_JavaScript_y_HTML5_Canvas_para_Desarrollo_de_Juegos\"><\/span>Why JavaScript and HTML5 Canvas for Game Development?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"HTML5_Canvas\"><\/span>HTML5 Canvas<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The element <code>canvas<\/code> HTML5 provides a blank space, similar to a painter&#039;s canvas, on which we can draw and render graphics using JavaScript. The main advantage of using Canvas for gaming is that it is natively supported by most modern browsers, meaning that the games created will be easily accessible to users without the need for additional plugins or applications.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"JavaScript_y_su_Ecosistema\"><\/span>JavaScript and its Ecosystem<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>JavaScript is one of the most used programming languages in the world and has evolved significantly since its inception. This language enables the creation of dynamic gaming experiences and can handle complex logic, animation, user interaction, and more. In addition, there are numerous libraries and frameworks that make video game development even easier.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Comenzando_con_el_Desarrollo_de_Videojuegos_en_JavaScript\"><\/span>Getting Started with Video Game Development in JavaScript<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<h3><span class=\"ez-toc-section\" id=\"Configurando_el_Entorno_de_Desarrollo\"><\/span>Setting up the Development Environment<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Before we dive into the code, we need to set up our development environment:<\/p>\n<ol>\n<li><strong>Text editor:<\/strong> You can use any text editor or IDE you prefer. Some of the popular options include Visual Studio Code, Sublime Text, and Atom.<\/li>\n<li><strong>Modern browser:<\/strong> Preferably choose one with good development tools, such as Google Chrome or Mozilla Firefox.<\/li>\n<li><strong>Local server:<\/strong> Some browsers have security restrictions that prevent you from loading files from the local file system directly. To avoid this, you can run a local server using Node.js with http-server, Python with <code>http.server<\/code>, or any other that is comfortable for you.<\/li>\n<\/ol>\n<h3><span class=\"ez-toc-section\" id=\"Creando_un_Canvas\"><\/span>Creating a Canvas<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>To start drawing our game, we first have to create an element <code>canvas<\/code> in our HTML file:<\/p>\n<pre><code class=\"&quot;language-html&quot;\">&lt;!DOCTYPE html&gt;\n&lt;html lang=&quot;es&quot;&gt;\n&lt;head&gt;\n    &lt;meta charset=&quot;UTF-8&quot;&gt;\n    &lt;title&gt;My First Game on Canvas&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;canvas id=&quot;gameCanvas&quot; width=&quot;800&quot; height=&quot;600&quot;&gt;&lt;\/canvas&gt;\n    &lt;script src=&quot;script.js&quot;&gt;&lt;\/script&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<p>In our JavaScript script, we access and configure the Canvas context like this:<\/p>\n<pre><code class=\"&quot;language-javascript&quot;\">const canvas = document.getElementById(&#039;gameCanvas&#039;); const ctx = canvas.getContext(&#039;2d&#039;);<\/code><\/pre>\n<p><code>getContext(&#039;2d&#039;)<\/code> gives us an object through which we can draw 2D graphics. This will be our main means of creating the video game.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Dibujando_en_el_Canvas\"><\/span>Drawing on the Canvas<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>To demonstrate how to draw in Canvas, let&#039;s start with something simple, like a rectangle:<\/p>\n<pre><code class=\"&quot;language-javascript&quot;\">\/\/ Set the fill color ctx.fillStyle = &#039;#0095DD&#039;; \/\/ Draw rectangle ctx.fillRect(20, 40, 50, 50); \/\/ fillRect(x, y, width, height)<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Animacion_Basica\"><\/span>Basic Animation<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The animation is done by changing the graphics quickly. We can do this by drawing our game object, clearing the canvas, and then redrawing the object in a new position. Methods <code>requestAnimationFrame<\/code> o <code>setInterval<\/code> are commonly used for this process:<\/p>\n<pre><code class=\"&quot;language-javascript&quot;\">function draw() { \/\/ Clear canvas ctx.clearRect(0, 0, canvas.width, canvas.height); \/\/ Draw something new here requestAnimationFrame(draw); } draw(); \/\/ Start animation loop<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Deteccion_de_Entradas_del_Usuario\"><\/span>User Input Detection<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>For a game to be interactive, we need to handle user input. Usually, this includes keyboard or mouse events:<\/p>\n<pre><code class=\"&quot;language-javascript&quot;\">\/\/ Keyboard event document.addEventListener(&#039;keydown&#039;, function(event) { if(event.key == &quot;ArrowRight&quot;) { \/\/ Move object to the right } if(event.key == &quot;ArrowLeft&quot;) { \/\/ Move object to the left } });<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Estructuracion_de_un_Simple_Juego_en_Canvas\"><\/span>Structuring a Simple Canvas Game<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>We&#039;re going to structure a very basic game in Canvas to better understand how all of these components would work together.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Creacion_del_Juego_Pong_en_JavaScript\"><\/span>Creating the Pong Game in JavaScript<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<h4><span class=\"ez-toc-section\" id=\"Estructura_Basica_del_Juego\"><\/span>Basic Game Structure<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>The Pong game is a classic choice for beginners because of its simplicity. Next, we will see the basic components:<\/p>\n<ul>\n<li><strong>Game board:<\/strong> This is simply our Canvas.<\/li>\n<li><strong>Pallets:<\/strong> Two rectangles that the players control.<\/li>\n<li><strong>Ball:<\/strong> A circle that bounces off the palettes and the limits of the Canvas.<\/li>\n<\/ul>\n<h4><span class=\"ez-toc-section\" id=\"Logica_del_Juego\"><\/span>Game Logic<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>The ball bounces off the top and bottom walls and off the players&#039; paddles. If it crosses the left or right boundaries, a player scores a point and the ball restarts.<\/p>\n<h4><span class=\"ez-toc-section\" id=\"Implementacion_del_Juego\"><\/span>Game Implementation<span class=\"ez-toc-section-end\"><\/span><\/h4>\n<p>Below is an outline of how we could implement the Pong game:<\/p>\n<pre><code class=\"&quot;language-javascript&quot;\">\/\/ Game variables (ball, paddles, score, etc.) function initializeGame() { \/\/ Set initial game values } function updateGame() { \/\/ Update game positions and logic } function drawGame() { \/\/ Draw the game elements on the canvas \/\/ Should be called on each animation frame } function gameLoop() { \/\/ Game loop that will be constantly executed requestAnimationFrame(gameLoop); updateGame(); drawGame(); } initializeGame(); gameLoop(); \/\/ Start the game loop<\/code><\/pre>\n<h2><span class=\"ez-toc-section\" id=\"Mejores_Practicas_y_Sugerencias_Avanzadas\"><\/span>Best Practices and Advanced Tips<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Once you&#039;ve mastered basic game development in JavaScript and HTML5 Canvas, there are a number of advanced practices and techniques you can explore to improve your skills:<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Patrones_de_Diseno\"><\/span>Design patterns<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>As your game grows in complexity, consider using design patterns such as Model-View-Controller (MVC) to structure your code efficiently.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Motores_y_Frameworks\"><\/span>Engines and Frameworks<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Explore JavaScript game engines and frameworks, such as Phaser, PixiJS, and Three.js, which offer more functionality and optimization for game development.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Optimizacion\"><\/span>Optimization<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Optimization is key in game development. Make sure your game runs smoothly on different devices and consider techniques like using Web Workers for intensive tasks.<\/p>\n<p>Developing games with JavaScript and HTML5 Canvas opens up a world of creative possibilities. With practice, patience, and curiosity, you can create games that delight and challenge players around the world. Start your adventure as a game developer today and see how far it can take your creativity!<\/p>","protected":false},"excerpt":{"rendered":"<p>El desarrollo de videojuegos es una de las ramas m\u00e1s interesantes y creativas dentro de la programaci\u00f3n. Ofrece una combinaci\u00f3n \u00fanica de arte, narrativa, m\u00fasica y, por supuesto, tecnolog\u00eda. Con la llegada de HTML5 y el poder de JavaScript, se ha abierto la posibilidad de crear juegos din\u00e1micos y atractivos directamente en nuestro navegador, accesibles [&hellip;]<\/p>","protected":false},"author":1,"featured_media":24547,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[420,2207],"tags":[205,1404,90,211,1648,782,545,185,99,2208,93],"class_list":["post-24546","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","category-nodejs","tag-blog","tag-canvas","tag-con","tag-desarrollo","tag-entretenimiento","tag-html5","tag-introduccion","tag-javascript","tag-juegos","tag-nodejs","tag-videojuegos"],"_links":{"self":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/24546","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/comments?post=24546"}],"version-history":[{"count":0,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/24546\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media\/24547"}],"wp:attachment":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media?parent=24546"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/categories?post=24546"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/tags?post=24546"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}