I'm trying to draw smooth curves instead of the normal straight line between points in p5js, using the current and previous mouse position (I can keep a list of past points if needed).
I've tried to adapt this answer to p5, but this does not work for some reason. Trying to rewrite it for p5.js just gives straight lines instead of curves.
let history = []
let value = 0
var p = 10
window.setup = setup;
window.draw = draw;
window.keyPressed = keyPressed;
function setup() {
createCanvas(window.innerWidth, window.innerHeight);
}
function drawLine(x1, y1, x2, y2, width, color) {
strokeWeight(p)
line(x1, y1, x2, y2);
}
async function keyPressed() {
if (key === " ") {
background(255)
for (let i = 0; i < history.length - 1; i++) {
window.setTimeout(function() {
drawLine(history[i][0], history[i][1], history[i + 1][0], history[i + 1][1])
}, await sleep(1))
// sleep(1000)
}
// drawing = 0
}
}
function draw() {
if (mouseIsPressed === true) {
stroke(0)
drawLine(mouseX, mouseY, pmouseX, pmouseY)
history.push([pmouseX, pmouseY], [mouseX, mouseY])
history = history.slice(history.length - 1000, history.length)
}
}
function mouseReleased() {
beginShape()
noFill()
stroke('red')
strokeWeight(2)
vertex(history[0][0], history[0][1]);
for (i = 1; i < history.length - 1; i++) {
var xc = (history[i][0] + history[i + 1][0]) / 2;
var yc = (history[i][1] + history[i + 1][1]) / 2;
quadraticVertex(xc, yc, history[i][0], history[i][1]);
}
vertex(history[history.length-1][0], history[history.length-1][1])
endShape()
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/1.6.0/p5.js" integrity="sha512-DWtDo/6AXxH1t9p7GCWqmC4XTVK/eje94XTV6QYB39rGllLN8Tr3izDf6lkmebgqRnYh4wtSFm4CvBoA9SrdpA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
How can I draw curved lines and properly translate the vanilla HTML5 canvas code to p5?