{"id":431,"date":"2019-04-22T18:48:25","date_gmt":"2019-04-22T17:48:25","guid":{"rendered":"https:\/\/www.maxcanvas.org\/?page_id=431"},"modified":"2026-10-25T22:13:26","modified_gmt":"2026-10-25T22:13:26","slug":"parallax","status":"publish","type":"page","link":"https:\/\/www.maxcanvas.org\/?page_id=431","title":{"rendered":"Parallax"},"content":{"rendered":"\n<figure><iframe loading=\"lazy\" width=\"905px\" height=\"705px\" src=\"https:\/\/www.maxcanvas.org\/MaxCanvas\/Examples\/MC%20-%2010%20-%20Parallax.html\"><\/iframe><\/figure>\n\n\n\n<p>Parallax demonstrates how to tile a dynamic background and give the illusion on depth to a 2d scene.  It uses the art work of the very talented <a href=\"https:\/\/raventale.itch.io\/parallax-background\" target=\"new\">Liz Molnar<\/a> (link opens in a new page).  Press A and D to scroll the screen.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Code<\/h4>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ MaxCanvas Project\n\/\/ 10-Parallax.JS File (April 2019)\n\/\/\n\/\/ Required to drag MC.js into Intellisense\n\/\/ thanks to https:\/\/visualstudiomagazine.com\/blogs\/tool-tracker\/2018\/07\/use-javascript-code.aspx (Dec 2018)\n\/\/\/ &lt;reference path=\"MC.js\" \/>\n\nwindow.onload = (function() {\n\n    \/\/ AQUIRE HTML DOM CANVAS REFERENCE\n    var c=document.getElementById(\"canvas\");\n\n    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n    \/\/ INITIALISE MC Engine \/\/\n    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n    MC.init(c);\n\n    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n    \/\/ END OF Initialisation \/\/\n    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n    \/\/-----------------------------------------------------------------------------\n    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n    \/\/ VARIABLE DECLARATION \/\/\n    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\n    var BG = new MC.SpriteBin();\n    var pictures = [];\n    var ImageNames = [\"Images\/11_background.png\",\n        \"Images\/10_distant_clouds.png\",\n        \"Images\/09_distant_clouds1.png\",\n        \"Images\/08_clouds.png\",\n        \"Images\/07_huge_clouds.png\",\n        \"Images\/06_hill2.png\",\n        \"Images\/05_hill1.png\",\n        \"Images\/04_bushes.png\",\n        \"Images\/03_distant_trees.png\",\n        \"Images\/02_trees and bushes.png\",\n        \"Images\/01_ground.png\"];\n\n    for (var i = 0, len = ImageNames.length; i &lt; len; i++) {\n        pictures[i] = new MC.Picture(ImageNames[i]);\n\n        BG.push(getImageSprite(i));\n\n        var sec = getImageSprite(i);\n        sec.dev.update = function () { };\n\n        BG.push(sec);\n    }\n\n    function getImageSprite(id) {\n     var ret = new MC.Sprite({\n                type: \"pict\",\n                picture: pictures[id],\n                scale: 0.45\n            });\n            ret.dev.mod = id;\n            ret.dev.update = function () {\n                if (MC.keys.a.down) {\n                    ret.vel.set(-50 * ret.dev.mod, 0);\n                }\n                else if (MC.keys.d.down) {\n                    ret.vel.set(50 * ret.dev.mod, 0);\n                }\n                else if (!MC.keys.a.down &amp;&amp; !MC.keys.d.down) {\n                    ret.vel.set(0, 0);\n                }\n            };\n            return ret;\n    }\n\n    function updateSecond() {\n        for (var i = 0, len = BG.bin.length; i &lt; len; i = i +2) {\n            var p = BG.bin[i];\n            var width = p.picture.width * p.scale;\n            if (p.pos.x &lt; 0) p.moveBy(new MC.Point(width, 0));\n            else if (p.pos.x > MC.canvas.width) p.moveBy(new MC.Point(-width, 0));\n\n            var p1 = p.pos.clone();\n            if (p1.x > MC.canvas.midX) width *= -1;\n            p1.add(width, 0);\n            BG.bin[i+1].moveTo(p1);\n        }\n    }\n\n\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\/\/ END OF declarations  \/\/\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\/\/-----------------------------------------------------------------------------\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\/\/      GAME LOOP       \/\/\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\n\/\/ first call, request subsequently made within\ngameLoop();\n\nfunction gameLoop() {\n    \/\/ ESSENTIAL MAINTAINANCE\n    requestAnimationFrame(gameLoop);\n    MC.game.update();                   \n    \n    \/\/ NON-PHYSICS UPDATES\n    BG.update();\n    updateSecond();\n\n    \/\/ RENDER\n    \/\/ Stage 1.  Clear the canvas\n\n    \/\/ Stage 2.  Render Sprites\n     BG.render();\n\n    \/\/ Stage 3.  Render GUI\n    MC.draw.text(\"[a] left - right [d]\", new MC.Point(20, 25));\n\n}\n\n}); \/\/ EoF\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Parallax demonstrates how to tile a dynamic background and give the illusion on depth to a 2d scene. It uses the art work of the very talented Liz Molnar (link opens in a new page). Press A and D to scroll the screen. Code<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_oct_exclude_from_cache":false,"footnotes":""},"class_list":["post-431","page","type-page","status-publish","hentry","entry"],"_links":{"self":[{"href":"https:\/\/www.maxcanvas.org\/index.php?rest_route=\/wp\/v2\/pages\/431","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.maxcanvas.org\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.maxcanvas.org\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.maxcanvas.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.maxcanvas.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=431"}],"version-history":[{"count":4,"href":"https:\/\/www.maxcanvas.org\/index.php?rest_route=\/wp\/v2\/pages\/431\/revisions"}],"predecessor-version":[{"id":605,"href":"https:\/\/www.maxcanvas.org\/index.php?rest_route=\/wp\/v2\/pages\/431\/revisions\/605"}],"wp:attachment":[{"href":"https:\/\/www.maxcanvas.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=431"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}