Drawing Pixels from the Blockchain

Project Source Code

Get the project source code below, and follow along with the lesson material.

Download Project Source Code

To set up the project on your local machine, please follow the directions provided in the README.md file. If you run into any issues with running the project source code, then feel free to reach out to the author in the course's Discord channel.

This lesson preview is part of the Million Ether Homepage course and can be unlocked immediately with a \newline Pro subscription or a single-time purchase. Already have access to this course? Log in here.

This video is available to students only
Unlock This Course

Get unlimited access to Million Ether Homepage, plus 70+ \newline books, guides and courses with the \newline Pro subscription.

Thumbnail for the \newline course Million Ether Homepage

We have pixel-changed events coming over web sockets and we need to draw them on our canvas. What we need to do is capture each event, parse the color string, and then write the pixel to the canvas. Let's take each step at a time. First, take a look at the event that comes through. The x, y, and color are all strings, but we want them all to be integers. Actually, x and y are plain integers and we need to parse the color into three integers, RGB, each one being between zero and 255. Let's write a function that, given an event, will parse these values and write the pixel. We'll name this function, write pixel with event, and it takes an event as an argument. And then from that event, we'll extract the x, y, and color from event.return values. Then we want to write the pixel. We haven't written write pixel yet, but we will. We'll type write pixel, we'll parse in x, parse in y, and we need to parse the color. We'll write parse color too. Let's write parse color now. Pars color will take this raw color string, split the bytes, and convert it to a three element array. Each element corresponding to a channel, red, green, or blue. You don't need to worry too much about how this function works. Just know that it returns a three element array. Now we need to write this particular pixel to the canvas. What we're going to do is read the current image data, find the index for this pixel, and update its color. Let's write the write pixel function. It takes three arguments, the x position, the y position, and the color. And let's log those out. First, we'll grab the existing image data. And then we'll grab the width and the height from the canvas. And notice that the canvas is in scope here. Now finding the index is a little tricky because the image data is a one- dimensional array. How do we find the index in a one-dimensional array? First, we'll take the y value times the number of columns plus the x value times four. Y four because we have one byte per channel per pixel. If that seems confusing, try to work it out on paper. But honestly, understanding the layout of this image array in memory is not that crucial to building this app. This is how you find the index. Now we will assign the pixel in data each channel of our pixel in color, the first one being red, then green, then blue. And I want every pixel to be opaque, that is no transparency. So I'll set the alpha channel to 255. If you don't do this, your pixels will be invisible. And lastly, we put the image data back onto the canvas. You might read this and realize that we're rewriting the canvas on every new pixel. You could certainly batch these updates, but we're not going to for this video. Now, all we have to do is change our event subscriptions to send the events to write pixel with event. All right, we don't need this anymore. And whenever a new pixel changes, we'll write pixel with event there. Here we'll type events dot map and then pass that to write pixel with event. If your page reloads and all you see is a white screen, look closer. A pixel is a pixel, no matter how small. And if you zoom real close, you'll see pink on the wall. But if we add more pixels, it's easy, you see. We just type 340x0c. We have two pixels rendering. Let's refresh the page and they should still reappear because we're using the same function to load old events as reading in new events. But these pixels are too tiny to see. So here's an idea. Let's buy an entire image.