Skip to content

Latest commit

 

History

History
89 lines (70 loc) · 2.07 KB

README.md

File metadata and controls

89 lines (70 loc) · 2.07 KB

turtle.js

JavaScript version of turtle graphics

npm version npm bundle size npm downloads jsdelivr hits Known Vulnerabilities

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

  1. Import the turtle

    import turtle from '@siyu971017/turtle.js';
  2. 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');