We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi,
I'm quite new to coding (processing is my first language) and I'm learning it online. So with my basic knowledge I tried to set up a canvas which you can draw on but the below problems occured. Do you know how I could solve them?
My code is:
// I wanted to create a simple drawing surface where you can use several colors and erase your work
// You can switch between colors using the key 0 to 3.
// You can use an eraser using the key 4.
// You can use DELETE and BACKSPACE to clear the canvas.
// You can use UP and DOWN keys to change the size of your brush.
// I have no idea why the color function as a general variable gives a different color compared to when used inside the draw() function.
// I wanted to switch back to the general color using key '1'. If you know the problem let me know :)
// Another bug is that though I set a limit for the brush not to be smaller than 5 px:
// if you click DOWN a lot of times it doesnt get smaller then that but you have to click UP just as many times to start increasing it again.
color fillColor = color(500, 600, 80);
int r = 10;
//---setup---
void setup(){
size(800, 600);
colorMode(HSB, width, height, 100);
background(0, 0, 100);
frameRate(200);
smooth();
}
//---draw---
void draw() {
if (mousePressed == true) {
float d = constrain(r, 5, 800);
fill(fillColor);
noStroke();
ellipse(mouseX, mouseY, d, d);
switch(key) {
case '0':
fillColor = color(mouseX, mouseY, 80);
break;
case '1':
fillColor = color(500, 600, 80);
break;
case '2':
fillColor = color(300, 400, 64);
break;
case '3':
fillColor = color(52, 300, 71);
break;
case '4':
fillColor = color(0, 0, 100);
break;
}}
}
void keyReleased() {
if (key==DELETE || key==BACKSPACE) background(0, 0, 100);
if (key=='s' || key=='S') saveFrame("screenshot.png");
if (keyCode==UP) { r = r + 10; }
if (keyCode==DOWN) { r = r - 10; }
}
Answers
Thanks!
line 11 is executed before you switch the colourMode in setup().
you aren't constraining r. you are setting d to the constrained value of r which is a different thing. try setting r to d after line 28.
ops late again :)
Hi. The default colorMode is (RGB 255,255,255,255). Until you call
colorMode()
at line 18 this is what color is expecting. fillColor is initialised before the change in color mode so the values are interpreted in a different way. To keep the var global, just declare it at line 11color fillColor;
and set the value aftercoorMode()
call.fillColor = color(500, 600, 80);
at line 19.The other thing is:
d
is constrained, butr
is not… sor
keeps going down and need to be incremented the same amount before goes bigger than 5 and starts to changed
again. One way around it is:good luck.
1st, color is either the primitive datatype
color
orint
, or a conversion function called color():Function color() always return an
int
representing an ARGBcolor
value! @-)ARGB can always be used as a unit value, even though colorMode() is HSB!