# Programming Question

**URL:** <https://forum.electromage.com/t/programming-question/1852>\
**Category:** Patterns and Code\
**Created:** [December 22, 2021, 9:35pm UTC](https://forum.electromage.com/t/programming-question/1852 "2021-12-22T21:35:58Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![wizard](https://forum.electromage.com/user_avatar/forum.electromage.com/wizard/32/4_2.png) [@wizard](https://forum.electromage.com/u/wizard)\
**Post date:** [December 22, 2021, 11:17pm UTC](https://forum.electromage.com/t/programming-question/1852/3 "2021-12-22T23:17:39Z")

</div>

Hi @Vitaliy ,  
Here’s my take on a 2D canvas in a pattern, I’ve used something similar in many patterns.

[2D canvas example.epe](https://forum.electromage.com/uploads/short-url/ft7MslG3G0PqFeGM7ODDuGMN4Sa.epe) (7.9 KB)

```javascript
/* Rendering into a "canvas" example
 *
 * In this example, an array is created to represent a 2D canvas with
 * x and y values in world coordinates, which are values from 0 to 1 exclusive.
 * The canvas is then scaled and drawn to the LEDs. The LEDs could match 1:1
 * or could be some other size, or even a non uniform layout.
 * The canvas is set up with 2 arrays, one for values, one for hues, which
 * are then fed to hsv() during render2D.
 * 
 * This example draws a dot traveling around the circumference of a circle,
 * leaving a fading trail of color.
 */

var width = 8
var height = 8
var numPixels = width * height
var canvasValues = array(numPixels) //make a "canvas" of brightness values
var canvasHues = array(numPixels) //likewise for hues
var fade = .95

//find the pixel index within a canvas array
//pixels are packed in rows, then columns of rows
function getIndex(x, y) {
  return floor(x*width) + floor(y*height)*width
}

function isIndexValid(index) {
  return index >= 0 && index < numPixels
}

export function beforeRender(delta) { 
  //fade out any existing pixels
  canvasValues.mutate(p => p*fade) //TODO fade based on delta for consistent fade

  //draw into the canvas here
  //this draws a pixel moving in a circle
  //radius 1/3rd, centered at 0.5, 0.5
  var a = time(.01) * PI2
  var r = 1/3
  var x = sin(a) * r + 0.5 
  var y = cos(a) * r + 0.5
  
  //optionally, you can make the pixels "wrap" around to the other side if out of bounds
  // x = mod(x,.99999)
  // y = mod(y,.99999)
  
  //calc this pixel's index in the canvas based on position of our coordinate
  var index = getIndex(x, y)
  
  //check that the coordinate is within bounds of the canvas before using it
  if (isIndexValid(index)) {
    canvasValues[index] = 1
    canvasHues[index] = time(.015)
  }
}

export function render2D(index, x, y) {
  index = getIndex(x, y) //calc this pixel's index in the canvas based on position
  h = canvasHues[index]
  v = canvasValues[index]
  hsv(h, 1, v*v)
}

```

More context here:

> [@HSVtoRGB & Framebuffer with attenuation](https://forum.electromage.com/t/hsvtorgb-framebuffer-with-attenuation/1169/6):
>
> @Scruffynerf , At some point I want to add a bitmap/raster API that would give you a 2D surface to draw into, and then map/pull from going to LEDs (a simple 1:1 if they happen to match, but not necessary). In the meantime, I’ve been playing around with a fractal that lends itself to drawing pixels rather than math in render, and came up with this framework which isn’t totally dissimilar to what everyone else has been doing for 2D, but packs pixels into a single dimensional array: /\* Rendering…

---

_[View the full topic](https://forum.electromage.com/t/programming-question/1852)._
