FeaturesTemplatesShowcaseTownie
AI
BlogDocsPricing
Log inSign up
andreterron
andreterronpng
Public
Like
png
Home
Code
2
README.md
main.tsx
Branches
1
Pull requests
Remixes
History
Environment variables
Val Town is a collaborative website to build and scale JavaScript apps.
Deploy APIs, crons, & store data – all from the browser, and deployed in milliseconds.
Sign up now
Code
/
README.md
Code
/
README.md
Search
7/13/2023
Viewing readonly version of main branch: v6
View latest version
README.md

Playing with Pixels in JavaScript

Programmatically generate PNG images, pixel by pixel!

This val uses PNGLib and Val Town's Express API to create and host PNG images. It can be particularly useful if you want to dynamically generate an image – for any website or API that accepts png urls.

Usage

  1. Copy and paste the code below or fork one of the examples
  2. Click the 🔒 lock to make your val public
  3. Visualize your image in via its Express endpoint: Open the [⋮] menu > Endpoints > Copy express endpoint
Create val
// Code from @andreterron.hostpng_example let my_image = @andreterron.png({ width: 256, height: 256, }, (p) => { const w = p.width, h = p.height; // Create colors (red, green, blue, alpha) const white = p.color(255, 255, 255, 255); // PNG starts filled with the first color const black = p.color(0, 0, 0, 255); for (let y = 0; y < h; y++) { for (let x = 0; x < w; x++) { // Get the buffer index for this position let index = p.index(x, y); // Set the color for the specific pixel p.buffer[index] = (x * y) / (w * h) * 24 % 2 >= 1 ? black : white; } } });

Code from @andreterron.hostpng_example, and it generates this image:

this example

Other Examples

@andreterron.gradient

Gradient

@andreterron.random_colors

@andreterron.women_and_power

Migrated from folder: A_Make_A_Val_Day/_23_07_13_PNG/png

Go to top
X (Twitter)
Discord community
GitHub discussions
YouTube channel
Bluesky
Product
FeaturesPricing
Developers
DocsStatusAPI ExamplesNPM Package Examples
Explore
ShowcaseTemplatesNewest ValsTrending ValsNewsletter
Company
AboutBlogCareersBrandhi@val.town
Terms of usePrivacy policyAbuse contact
© 2025 Val Town, Inc.