I want to make brushes for my drawing software, i have code, but its kinda bad and not working correctly.
The problem is if i put down a image of the brush every frame, it will not work when moving the mouse fast. I tried fixing that. Heres my (not fully working) code.
The image is just a gradient from middle.
It is working kinda fine, but between the mouse beeing moved too fast and slow enought (there are 2 functions that switch) there is some error.
thanks anyone who is helping!
int dist, distnow, max = 10, size = 35;
boolean mouseClick = false;
boolean pressed = false;
PVector mouseClickA = new PVector(0,0);
PVector mouseClickB = new PVector(0,0);
PImage airbrush;
void setup() {
size(960,540);
noStroke();
frameRate(255);
airbrush = loadImage("b1.png");
}
void draw() {
dist += (int)dist(mouseX,mouseY,pmouseX,pmouseY);
distnow = (int)dist(mouseX,mouseY,pmouseX,pmouseY);
if(mousePressed) {
line(mouseX,mouseY,pmouseX,pmouseY);
mouseClickA.x = mouseX;
mouseClickA.y = mouseY;
mouseClickB.x = pmouseX;
mouseClickB.y = pmouseY;
fill(0,25);
noStroke();
if(dist > max && distnow-5 < max) image(airbrush,mouseX,mouseY,size,size);//circle(mouseX,mouseY,size);
if(distnow > max) {
dottedLine(mouseClickA,mouseClickB, max+3);
image(airbrush,mouseX,mouseY,size,size);//circle(mouseX,mouseY,size);
}
}
if(dist > max) dist = 0;
if(keyPressed) background(#CCCCCC);
fill(255);
rect(0,0,width-1,50);
fill(0);
text(dist+"\n"+distnow,10,20);
text(frameRate,50,20);
}
void dottedLine(PVector posA, PVector posB, int distance) {
posA = posA.copy();
posB = posB.copy();
PVector direction = posB.sub(posA);
int len = (int) (direction.mag() / distance);
direction.normalize();
direction.mult(distance);
fill(0,25);
for (int i = 0; i < len; i++) {
posA.add(direction);
image(airbrush,posA.x,posA.y,size,size);//circle(posA.x,posA.y,size);
}
}