An animated spiral
Log in to post a comment.
// You can find the Turtle API reference here: https://turtletoy.net/syntax Canvas.setpenopacity(1); const GOLDEN_RATIO = 1.61803; const STEPS = 200; const INVERT_POINT = STEPS / 2; const STEP_SIZE = 2; const SLEEP_TIME = 10; // Global code will be evaluated once. const turtle = new Turtle(); turtle.penup(); turtle.goto(0, 0); turtle.pendown(); turtle.left(90); let _length = STEP_SIZE; let _direction = 1; let _timesInverted = 0; const turn = (direction = _direction) => turtle.right(direction * 90); function step(length) { turtle.forward(length); } function invert() { console.log('invert'); _timesInverted++; let stepSize = STEP_SIZE / (2 * _timesInverted); turn(-_direction); turn(1); step(stepSize); turn(1); sleep(); step(stepSize); turn(_direction); _direction *= -1; _length += STEP_SIZE * _direction * 2; turn(); } function drawArm() { step(_length); turn(); } function drawCorner() { if (_currentIndex > 0 && _currentIndex % INVERT_POINT === 0) { invert(); sleep(); return; } drawArm(); sleep(); drawArm(); _length += STEP_SIZE * _direction; } function sleep(ms = SLEEP_TIME) { var date = new Date(); var curDate = null; do { curDate = new Date(); } while (curDate-date < ms); } function walk(i) { _currentIndex = i < STEPS - 1 ? i : 0; drawCorner(); sleep(); // return i < STEPS - 1 return i < STEPS * 2 - 1; }