Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8243641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:35:05+00:00 2026-06-07T21:35:05+00:00

I have a 2D array that is 30 across, 20 down. However, the viewport

  • 0

I have a 2D array that is 30 across, 20 down. However, the viewport only paints 15 across and 10 down. I had a game like that originally and I’ve been trying to achieve something like this:

enter image description here

Here’s my fiddle: http://jsfiddle.net/sTr7q/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T21:35:06+00:00Added an answer on June 7, 2026 at 9:35 pm

    Here I’ve made a rudimentary viewport for you:

    http://jsfiddle.net/kmHZt/

    You have to keep track of the viewport X and Y (vX, vY) that represent where you are in the world at large. I also kept the viewport width and height as vars instead of hard coding them in.

    You can use the arrow keys to change the viewport X and Y which will scroll the map, drawing only the correct tiles.

    Hope that helps! Let me know if you have any questions!

    EDIT: example of viewport moving with player: http://jsfiddle.net/kmHZt/10/

    var canvas, context, board, imageObj, tiles, board, display;
    var NUM_OF_TILES = 2;
    
    // viewport
    var vX = 0,
        vY = 0,
        vWidth = 15,
        vHeight = 10;
    
    var playerX = 0,
        playerY = 0;
    
    var worldWidth = 29,
        worldHeight = 19;
    
    function loadMap(map) {
        if (map == 1) {
            return [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 0], [0, 1, 1, 2, 1, 1, 1, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 0], [0, 1, 1, 2, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 0], [0, 1, 1, 2, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 0], [0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 1, 1, 0], [0, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 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        }
    }
    
    $(document).ready(function() {
    
    
        canvas = document.getElementById("canvas");
        context = canvas.getContext("2d");
    
        canvas.tabIndex = 0;
        canvas.focus();
        canvas.addEventListener('keydown', function(e) {
            console.log(e);
            var key = null;
            switch (e.which) {
            case 37:
                // Left
                if (playerX > 0) playerX--;
                break;
            case 38:
                // Up
                if (playerY > 0) playerY--;
                break;
            case 39:
                // Right
                if (playerX < worldWidth) playerX++;
                break;
            case 40:
                // Down
                if (playerY < worldHeight) playerY++;
                break;
            }
            // Okay! The player is done moving, now we have to determine the "best" viewport.
            // Ideally the viewport centers the player,
            // but if its too close to an edge we'll have to deal with that edge
    
            vX = playerX - Math.floor(0.5 * vWidth);
            if (vX < 0) vX = 0;
            if (vX+vWidth > worldWidth) vX = worldWidth - vWidth;
    
    
            vY = playerY - Math.floor(0.5 * vHeight);
            if (vY < 0) vY = 0;
            if (vY+vHeight > worldHeight) vY = worldHeight - vHeight;
    
    
            draw();
        }, false);
    
        var board = [];
    
        canvas.width = 512;
        canvas.height = 352;
    
        board = loadMap(1);
        imageObj = new Image();
        tiles = [];
    
        var loadedImagesCount = 0;
        for (x = 0; x <= NUM_OF_TILES; x++) {
            var imageObj = new Image(); // new instance for each image
            imageObj.src = "http://mystikrpg.com/canvas/img/tiles/t" + x + ".png";
    
            imageObj.onload = function() {
                // console.log("Added tile ... "+loadedImagesCount);
                loadedImagesCount++;
                if (loadedImagesCount == NUM_OF_TILES) {
                    // Onces all tiles are loaded ...
                    // We paint the map
                    draw();
                }
            };
            tiles.push(imageObj);
        }
    
    
        function draw() {
            context.clearRect(0,0,canvas.width, canvas.height);
            for (y = 0; y <= vHeight; y++) {
                for (x = 0; x <= vWidth; x++) {
                    theX = x * 32;
                    theY = y * 32;
                    context.drawImage(tiles[board[y+vY][x+vX]], theX, theY, 32, 32);
                }
            }
            context.fillStyle = 'red';
            context.fillRect((playerX-vX)*32, (playerY-vY)*32, 32, 32);
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array that looks something like this: Array ( [0] => apple
I have an array that looks like this: [ Object actions: Array[2] comments: Object
I have an array that looks like the following: array = [[1, 5], [4,
I have an array of CGColors that I need to display across a path.
I am trying to make a simple game that goes across a TCP network.
I have a numpy array that contains some image data. I would like to
I have an array that contains @sign in the object. I can't access that
I have an array that I populate with parents, children, children of children, children
I have an array that is produced from people wanting to reserve a time
I have one array that is filled by mysql_query. Values that are in this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.