const turtle = { forward(distance) { const newX = x + Math.cos(degToRad(angle)) * distance; const newY = y + Math.sin(degToRad(angle)) * distance; if (penDown) { ctx.beginPath(); ctx.moveTo(x, y); ctx.lineTo(newX, newY); ctx.stroke(); } x = newX; y = newY; }, backward(distance) { this.forward(-distance); }, left(degrees) { angle -= degrees; }, right(degrees) { angle += degrees; }, penup() { penDown = false; }, pendown() { penDown = true; }, goto(newX, newY) { x = newX; y = newY; }, home() { x = canvas.width / 2; y = canvas.height / 2; angle = 0; }, circle(radius) { ctx.beginPath(); ctx.arc(x, y, radius, 0, Math.PI * 2); ctx.stroke(); }, color(newColor) { ctx.strokeStyle = newColor; }, width(size) { ctx.lineWidth = size; } }; window.turtle = turtle;