我正在尝试在p5js中绘制平滑曲线,而不是普通的直线连接点,使用当前和上一个鼠标位置(如果需要,我可以保留过去点的列表)。
我已经尝试将this answer适应到p5中,但由于某些原因它并不起作用。试图为p5.js重新编写它只会得到直线而不是曲线。
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>
我该如何绘制曲线并正确地将原始的HTML5画布代码转换为p5?