Jack
Jack

Reputation: 2285

Setting Canvas Style to a Changing Variable

Why does this work,

var color = Math.sin( time ) * 128;
color = 128;
context.fillStyle = "rgb(" + color + ", " + color + ", " + color + ")";

But this does not?

var color = Math.sin( time ) * 128;
color += 128;
context.fillStyle = "rgb(" + color + ", " + color + ", " + color + ")";

For some reason, the variable stops working once it becomes dynamic.

Upvotes: 0

Views: 84

Answers (1)

Jack
Jack

Reputation: 2285

Figured this out. I'm sending a floating value where canvas is looking for an integer.

It should be Math.floor(Math.sin( time ) * 128);

Upvotes: 1

Related Questions