-1

I was creating a mouse-based painter app. I wanna a brush whose color is chosen by a color picker in p5js. And I think I finished coding, but it's not working at all... and is there anybody who can help me? I wanna see where I did make mistake....! thanx, advance!

```
var brushSize;
let activeBrush = 0;

function setup() {
  brushSize = 10;
  createCanvas(800, 800);
  colorPicker = createColorPicker('#ed225d');
  colorPicker.position(600, 20);
  background(220);


function draw() {
  colorMode(RGB);
  brushColor = colorPicker.color();
  noStroke();
 
function keyTyped() {
  if (key == '=') { //becomes bigger and smaller by 10% with [=] and [-] keys, respectively, but not working...
     brushSize = brushSize + brushSize*0.1;
  } else if (key == '-') {
     brushSize = brushSize - brushSize*0.1;
  } else if (key == 'c') { //when [C] is pressed, everything on canvas gets deleted, but it's not working at all..
        noStroke();//clear button
        fill(220);
        rectMode(CORNER);
        rect(0, 0, 800, 800);
  } else if (key == 's') { //when [S] is pressed, current content on canvas is saved as an image file ,, but it's not working at all..
    let b = createCanvas(800, 800);
    saveCanvas(b, 'myCanvas', 'jpg');
     } 
  } 
}
function mouseDragged(){
fill(colorPicker);
ellipse(mouseX, mouseY, brushSize, brushSize)}} // plus why this isn't working..?? TT
```
rose
  • 49
  • 6

1 Answers1

1

It's likely that, wherever mouseX and mouseY are coming from in the OP code, they are wrong. Painting on drag is straight-forward. From the requirements stated in the OP, there's no need for a draw function, just add to the canvas on drag.

Here's some working code to use as a starter....

const sketch = function(p) {

  let colorPicker;
  let brushSize = 20;
  
  p.setup = function() {
    p.createCanvas(600, 600);
    colorPicker = p.createColorPicker('red');
    colorPicker.position(0, 0);
  };



  p.keyPressed = function(e) {
    let key = e.key;
    if (key === '=') brushSize += brushSize * 0.1;
    else if (key === '-') brushSize -= brushSize * 0.1;
    else if (key === 'c') p.clear();
  }

  p.mouseDragged = function(e) {
    color = colorPicker.color()
    p.fill(color);
    p.stroke(color);
    p.ellipse(e.clientX, e.clientY, brushSize, brushSize / 2)
  }
 
};

let myp5 = new p5(sketch);
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.4.1/p5.min.js"></script>
danh
  • 62,181
  • 10
  • 95
  • 136
  • thank a lot!! do u know how to make it work with function keyTyped()..?? the '=', 'c' and '-' – rose Jun 23 '22 at 17:10
  • 1
    @rose - edited to demonstrate handling keys. The color will change to red, green or blue as you hold down the r, g, or b keys. – danh Jun 23 '22 at 17:39
  • thanx a lot! but i wanna know how to erase the screen when i press the 'c' button and how to grow the brush size when i press '=' button ..! would u check my code up there?? – rose Jun 23 '22 at 19:20
  • 1
    It's just like any other logic that we'd place in the keyPressed method: set variables that are in the same scope as the mouseDragged method based on the key pressed. (in the case of clear, you can call that right away when the c key is pressed). Edited again to demonstrate. – danh Jun 23 '22 at 20:13
  • thnx!!!! I understood clearly oh but here, I wonder what if I wanna choose the brush's color with the color picker..?? ```p.fill(stroke(colorPicker.color())); p.stroke(stroke(colorPicker.color())); p.ellipse(e.clientX, e.clientY, brushSize, brushSize / 2) } ``` i wroted it in this way but there's saying 'Error: [object Arguments]is not a valid color representation.' – rose Jun 24 '22 at 04:26
  • 1
    @rose - edited another time to demo color picker. Generally, the SO approach is to ask smaller, more focussed questions one at a time. You can open another one if you still need help. – danh Jun 24 '22 at 19:39
  • oh i didn't know that ! thanx i opened a little question related this one! could u see it a bit? thanx always!! https://stackoverflow.com/questions/72751611/how-to-change-filter-with-key-pressed-function – rose Jun 25 '22 at 06:04