@JustPete, you’ve found one of the classics: In the two if
statements in your render function, you’re using the assignment operator =
, when I think you mean to use the comparison operator ```==````.
export function render2D(index,x,y) {
if (x = x1) // should be 'if (x == x1)
if (y = y1) { // 'if (y == y1)
I admit to having done this more than once myself. It’s a problem in most popular computer languages – easy to do, and hard to find. “Bigger” development tools might warn you about it, and some modern languages make the operators easier to tell apart, but it’s really just one of those things you have to watch out for.
In beforeRender, you’re setting x1,y1 to be integers from 0 to width and 0 to height. In render() though, x and y come in normalized to the range 0 to 1 according to your mapping function. To get back to integer coordinates that can be easily compared, you need to multiply them by width and height:
if (floor(x * width) == x1)
if (floor(y * height) == y1) {
also, the arrangement of if statements – if (condition1) if (condition2) { do things; }
– looks like it ought to work but it doesn’t. I have no idea if it’s supposed to.
Anyway, there’s a surprising amount of wiggle room in language specification. Best practice is to be very explicit about what you want done, and put in all the braces. (Or make this a compound conditional using the logical AND operator &&
. Like this: if ((condition1) && (condition2)) { do things;}
)
Here’s your render function with these modifications made. Your design was correct – it works perfectly.
export function render2D(index,x,y) {
if (floor(x * width) == x1) {
if (floor(y * height) == y1) {
hsv(hue,1,1)
}
}
else {
hsv(0,0,0)
}
}