@siyu971017/turtle.js
v1.5.0
Published
JavaScript version of turtle graphics
Downloads
18
Readme
turtle.js
JavaScript version of turtle graphics
Turtle.js’ website is now available: https://turtlejs.vercel.app/
Install
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>
Usage
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);
Methods
See Docs for more information
Examples
Get turtle's position
t.position().then(position => {
console.log('position:', position);
})
Draw a triangle
for (let i = 0; i < 3; i++) {
t.forward(50);
t.right(120);
}
Draw a square
for (let i = 0; i < 4; i++) {
t.forward(50);
t.right(90);
}
Draw a regular polygon with any number of sides
function drawRegularPolygon(sides) {
if (sides < 3) {
sides = 3;
}
for (let i = 0; i < sides; i++) {
t.forward(50);
t.right(360 / sides);
}
}
drawRegularPolygon(6);
Change the shape
t.shape('turtle');
t.forward(50);
t.shape('arrow');
t.backward(50);
t.shape('classic');