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 9216885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:30:51+00:00 2026-06-18T02:30:51+00:00

So the challenge that I’ve created for myself is as such. I have a

  • 0

So the challenge that I’ve created for myself is as such.

I have a source photo:

Source Photo

That I am mapping color values and creating a pixelated representation of it using divs

Here’s the result:

Result Photo

The code that I’m accomplishing this with is:

'use strict';

var imageSource = 'images/unicorn.jpg';

var img = new Image();
img.src = imageSource;
var canvas = $('<canvas/>')[0];
canvas.width = img.width;
canvas.height = img.height;
canvas.getContext('2d').drawImage(img, 0, 0, img.width, img.height);
var context = canvas.getContext('2d');

console.log('img height: ' + img.height);
console.log('img width: ' + img.width);

var pixelDensity = 70;

var timerStart = new Date();


for (var i = pixelDensity/2; i < img.height; i += (img.height/pixelDensity) ) {
    $('.container').append($('<div class="row">'));
    for(var j = pixelDensity/2; j < img.width; j += img.height/pixelDensity) {
        var value = context.getImageData(j, i, 1, 1).data;
        var colorValue = 'rgb(' + value[0] + ', ' + value[1] + ', ' + value[2] + ')';
        $('.row:last').append($('<div class="block">').css({'background-color' : colorValue}));
    }
}

var timerStop = new Date();

console.log(timerStop - timerStart + ' ms');

The pixelDensity variable controls how close together the color samples are. The smaller the number, the fewer the samples, taking less time to produce the result. As you increase the number, the samples go up and the function slows down considerably.

I’m curious to know what is making this thing take such a long time. I’ve looked at slightly similar projects – most notably Jscii – which process the image data much faster, but I can’t figure out what the difference is that’s offering the added performance.

Thanks for your help!

  • 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-18T02:30:52+00:00Added an answer on June 18, 2026 at 2:30 am

    The main issue is that you append DOM elements to a page right in a loop. This would run much faster if you’ll create wrapper element with all your data before actually adding it to the page.

    Edit:
    I’ve also noticed you call context.getImageData for every pixel, this is what takes most of the time. Instead, you should cache image data in variable and get color values from it. You’ll also need to cache loop conditions as @Travis J mentioned and round them:

    var wrapper = $('<div class="container">');
    var imgData = context.getImageData(0, 0, img.width, img.height).data;
    var getRGB = function(i) { return [imgData[i], imgData[i+1], imgData[i+2]]; };
    var start = Math.round(pixelDensity/2),
        inc = Math.round(img.height/pixelDensity);
    
    for (var i = start; i < img.height; i += inc) {
        var row = $('<div class="row">');
        for(var j = start; j < img.width; j += inc) {
            var colorValue = getRGB((i * (img.width*4)) + (j*4));
            row.append($('<div class="block">').css({'background-color' : 'rgb('+(colorValue.join(','))+')'}));
        }
        wrapper.append(row);
    }
    
    $('body').append(wrapper);
    

    This would reduce time from 6-9 seconds to 600-1000 milliseconds. You can also use plain javascript to manipulate DOM elements instead of jquery to make it even faster.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a challenge that I am trying to solve and I can't work
Challenge: I have this code that fails to compile. Can you figure out what's
I have been set a challenge to create an indexer that takes all words
I have a bit of a unique challenge today. I have a client that
I'm teaching myself programming and today's challenge is to write a program that can
I have an interesting challenge that I'm wondering if anyone here can give me
I'm looking for some input for a challenge that I'm currently facing. I have
I have a challenge that I am trying to solve using classes. I am
I have a small SQL based challenge that i'm trying to solve to better
I have a challenge. We have a database that was originally designed to be

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.