Hi @Goose,
Welcome! Yeah, starting out changing variables or constants used in existing patterns is a good way to tweak things without much code experience.
To get an idea of how this works to make pixels, I would check out this video where I go over the basics in the older V1 interface which didn’t have code, just math:
After that, you can check out what changed in V2 video to get an overview of the new code features:
Next, I recommend this video where I go through the thought process of writing the KITT pattern:
Of course none of that will teach you how to code, but should give you an idea of what Pixelblaze is doing and some of the fundamental tools.
Yes, this the language a subset of JavaScript ES6. The basic stuff is all there, the same kind of things you will find in most programming languages. Those basics make up much of what you’ll need to know to do just about anything you want. There are some docs about the language features if you scroll below the editor, and are also available here.
I would recommend some JavaScript tutorials, but many would focus on advanced language features that aren’t available (even things like console.log
. The C language is also very close, they share much of the same syntax and keywords (things like for
loops), and could be a good place to start.
Here’s the 5 minute version:
1. Expressions and Statements
Expressions are core to much of what goes on. These are basically just math. These are often composed in statements that store the result in a variable. Code like this:
h = t1 + index/pixelCount
What is happening is that the expression on the right of the =
is evaluated to a value, and stored in a variable called h
. On the right side, index/pixelCount
is evaluated first since there are an order of operations that make division happen before addition. The index
and pixelCount
are also variables that have been defined previously.
So the above says: “take index, divide by pixelCount, add t1, and store in h”
Code is run one line at a time, so this block of code:
a = 5
b = 3
a = b -1
will result with a = 2
2. Functions
Functions are a tool that lets you compose groups of statements into an abstraction. You can then call the function instead of repeating those statements.
A function looks like this:
function myFunctionName(input) {
return input + 1
}
The return keyword will cause the function to resolve to the value of the expression to the right of it. The input
in the parenthesis is called an argument and is provided when the function is called.
This function will add 1 to whatever its given. It can be called like this, and these statements are equivalent:
a = 6
a = 5 + 1
a = myFunctionName(5)
a = myFunctionName(4 + 1)
In each case, a
ends up being 6.
Functions can also be used as a way for systems to communicate information. In Pixelblaze, there are a few special exported functions the system looks for, namely render
and beforeRender
. In Pixelblaze you define these functions, and the engine calls them to make stuff happen. render
is where pixels get rendered, and it is called once for each pixel. You are given the index
of the pixel, which is a variable that is numbered for every pixel, starting at 0, then 1, 2, 3, etc. Another global variable is also provided by the system called pixelCount, which is the total number of pixels configured on the Settings tab.
The basic idea is that you implement a render function that takes the current pixel index, and does something interesting with it. Often some math is involved Eventually the code in render will call either hsv()
or rgb()
functions to get data to the pixel.
3. Control Flow
Statements and functions are great, but what if you wanted to do something only when certain conditions were met? Thats where control flow statements come in.
The if
keyword lets you write conditional blocks that only execute if the given expression is true*. e.g.
if (index > 5) {
v = .5
}
In this case, v will be set only when index > 5. Otherwise it will be left alone.
*
trueness in this language is any non-zero value, and the >
operator (or any other comparison operator) results in a value that is 0 or 1.
You can add the else keyword to cover the opposite case:
if (index > 5) {
v = .5
} else {
v = 1
}
What if you wanted to do something a bunch of times, like maybe once per pixel?
To repeat a block of code, you can use while
or for
- these can be tricky though. Often you will see a pattern go through an array using a for
loop like this code in KITT:
for (i = 0; i < pixelCount; i++) {
pixels[i] -= delta * fade
pixels[i] = max(0, pixels[i])
}
This is going to run the code between the braces several times, and the i
variable is going to change each time. I go over that briefly around 9 minutes in to the KITT video.
Let us know how it goes! Stuck? We can help! Find an awesome tutorial? Let us know!