JavaScript version of turtle graphics
npm install @siyu971017/turtle.js
$ npm install @siyu971017/turtle.js
`
Or grab from jsDelivr CDN
`html
`
Usage
1. Import the turtle
`js
import turtle from '@siyu971017/turtle.js';
`
2. Call the turtle-function after the target canvas element has loaded
`js
var t = new turtle(canvasElement);
`
Methods
See Docs for more information
Examples
$3
`js
t.position().then(position => {
console.log('position:', position);
})
`
$3
`js
for (let i = 0; i < 3; i++) {
t.forward(50);
t.right(120);
}
`
$3
`js
for (let i = 0; i < 4; i++) {
t.forward(50);
t.right(90);
}
`
$3
`js
function drawRegularPolygon(sides) {
if (sides < 3) {
sides = 3;
}
for (let i = 0; i < sides; i++) {
t.forward(50);
t.right(360 / sides);
}
}
drawRegularPolygon(6);
`
$3
`js
t.shape('turtle');
t.forward(50);
t.shape('arrow');
t.backward(50);
t.shape('classic');
``