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

  • Home
  • SEARCH
  • 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 9008267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:54:10+00:00 2026-06-16T01:54:10+00:00

Using the Javascript canvas element, is it possible to copy a 2-dimensional array of

  • 0

Using the Javascript canvas element, is it possible to copy a 2-dimensional array of RGB values to a canvas?

<html>
<body>
<canvas id="canvas" height="200" width="200"></canvas>
<script type = "text/javascript">

//copy the following array to the canvas:
var arr1 = [
[[255, 255, 255],[200, 200, 0],[200, 200, 200]],
[[200, 0, 0],[200, 200, 0],[200, 200, 0]],
[[200, 200, 0],[200, 0, 0],[200, 200, 0]]
]

<script>
</body>
</html>
  • 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-16T01:54:11+00:00Added an answer on June 16, 2026 at 1:54 am

    You can use getImageData/putImageData. Just calculate the right index (it’s RGBA values in one big array): http://jsfiddle.net/zjypd/ (the jsFiddle has an enlarged canvas to show the pixels).

    var arr1 = [
        [[255, 255, 255],[200, 200, 0],[]],
        [[200, 0, 0],[200, 200, 0],[200, 200, 0]],
        [[200, 200, 0],[200, 0, 0],[200, 200, 0]]
    ];
    
    var ctx = document.querySelector("canvas").getContext("2d");
    
    var height = arr1.length;
    var width = arr1[0].length;
    
    var h = ctx.canvas.height;
    var w = ctx.canvas.width;
    
    var imgData = ctx.getImageData(0, 0, w, h);
    var data = imgData.data;  // the array of RGBA values
    
    for(var i = 0; i < height; i++) {
        for(var j = 0; j < width; j++) {
            var s = 4 * i * w + 4 * j;  // calculate the index in the array
            var x = arr1[i][j];  // the RGB values
            data[s] = x[0];
            data[s + 1] = x[1];
            data[s + 2] = x[2];
            data[s + 3] = 255;  // fully opaque
        }
    }
    
    ctx.putImageData(imgData, 0, 0);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im using JavaScript, the HTML5 canvas-element and WebGL to make a simple 3D-game in
I am drawing an image in HTML 5 using canvas in JavaScript. I need
I have made a snake game using javascript and HTML5 Canvas element. It can
I have made a game using HTML5 Canvas Element, Javascript for facebook. You can
We're using Javascript to draw polylines on a <canvas> element, based on some spatial
I am using javascript to draw to a canvas element inside a div, contained
Anyone have experience with game development using javascript and the html5 canvas element? So
<html> <body> <script src=raphael.js type=text/javascript></script> <script>// Each of the following examples create a canvas
I am working with the HTML5 canvas element. I am using the following javascript
In my HTML I have: <div id=cowcloud> <canvas id=cows width=1024 height=253> <p>Browser doesn't support

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.