67 lines
1.2 KiB
JavaScript
67 lines
1.2 KiB
JavaScript
let angle,x,y,drawing=true;
|
|
function setup(){
|
|
createCanvas(windowWidth,windowHeight); //canvas is size of window
|
|
resetDefaults();
|
|
turtle();
|
|
}
|
|
|
|
function resetDefaults(){
|
|
|
|
angleMode(DEGREES); //instead of p5.js's default RADIANS
|
|
|
|
angle=0; //facing up
|
|
x = width/2; //start turtle in center
|
|
y = height/2;
|
|
}
|
|
|
|
function forward(d,startx=x, starty = y, _angle = angle){
|
|
|
|
let newX = startx+d*sin(_angle);
|
|
let newY = starty+d*cos(_angle);
|
|
|
|
if (drawing){
|
|
line(startx,starty,newX,newY);
|
|
}
|
|
|
|
x=newX
|
|
y=newY
|
|
}
|
|
function back(d,startx=x, starty = y, _angle =360- angle){
|
|
|
|
let newX = startx+d*sin(_angle);
|
|
let newY = starty+d*cos(_angle);
|
|
|
|
if (drawing){
|
|
line(startx,starty,newX,newY);
|
|
}
|
|
|
|
x=newX
|
|
y=newY
|
|
}
|
|
function left(_angle){
|
|
angle=180+_angle;
|
|
}
|
|
function right(_angle){
|
|
angle=_angle;
|
|
}
|
|
|
|
function pendown(){
|
|
drawing=true;
|
|
}
|
|
function penup(){
|
|
drawing=false;
|
|
}
|
|
function penSize(weight=1){
|
|
strokeWeight(weight);
|
|
}
|
|
function penColor(c){
|
|
stroke(c);
|
|
}
|
|
function randint(max=100){
|
|
//default returns int between 0 and 100
|
|
return int(random(max))
|
|
}
|
|
function windowResized(){ //resizes canvas if window is resized
|
|
resizeCanvas(windowWidth, windowHeight);
|
|
}
|