JavaScript version of turtle graphics
Turtle.js’ website is now available: https://turtlejs.vercel.app/
Use npm
$ npm install @siyu971017/turtle.js
Or grab from jsDelivr CDN
<script src="https://cdn.jsdelivr.net/npm/@siyu971017/turtle.js/dist/turtle.umd.min.js"></script>
-
Import the turtle
import turtle from '@siyu971017/turtle.js';
-
Call the turtle-function after the target canvas element has loaded
var t = new turtle(canvasElement);
See Docs for more information
t.position().then(position => {
console.log('position:', position);
})
for (let i = 0; i < 3; i++) {
t.forward(50);
t.right(120);
}
for (let i = 0; i < 4; i++) {
t.forward(50);
t.right(90);
}
function drawRegularPolygon(sides) {
if (sides < 3) {
sides = 3;
}
for (let i = 0; i < sides; i++) {
t.forward(50);
t.right(360 / sides);
}
}
drawRegularPolygon(6);
t.shape('turtle');
t.forward(50);
t.shape('arrow');
t.backward(50);
t.shape('classic');