turtle-basic.js

total 0
used 0
limit 0
/* title: Turtle Graphics Drawing categories: basic dragging files: head point pointlist stage mouse dragging stroke ../point_src/constrain-distance.js ../point_src/functions/range.js ../point_src/split.js ../point_src/relative-xy.js ../point_src/relative.js --- > A most basic version of a "turtle". This very simple example of turtle drawing we move a _pen_ using coordindate based plotting with a _down_ and _up_. 1. Drag the point 2. Run the `walk()` function It processes the `steps()` forever, plotting _lines_ for each _down_ and _up_ command */ addControl('steps', { field: 'range' , stage: this , min: 1 , max: 200 , onchange(ev, unit) { /*slider changed. */ let sval = ev.currentTarget.value unit.value = sval stage.stepCount = Number(sval) } }) addButton('button', { label: 'walk' , onclick(ev) { stage.walk(stage.stepCount) } }) class MainStage extends Stage { // canvas = document.getElementById('playspace'); canvas = 'playspace' steps() { return [ ['down'] , ['forward', 1] // unit of width , ['rotate', 30] // degree // , ['forward', 1] // degree , ['up'] // degree // , ['goto', 0] ] } mounted(){ this.points = new PointList( [250 , 50, 20] // , [250 , 170, 20, -90] // , [250 , 290, 20] ).cast() this.dragging.add(...this.points) this.stepCount = 1 this.stepTick = 0 this.lines = [] } draw(ctx){ this.clear(ctx) this.points.pen.indicator(ctx, 'green') if(this._isDrawing) { this.downAt.pen.line(ctx, this.points.last()) } this.lines.forEach(pair=>{ pair[0].pen.line(ctx, pair[1]) }) } walk(count=this.stepCount){ range(count).forEach(()=>{ this.performStep(this.ctx, this.stepTick++, 1) }) } performStep(ctx, tick, stepCount=1) { let items = this.steps() let item = items[Math.floor(tick % items.length)] range(stepCount).forEach(()=>{ this.performAction(item) }) } performAction(item) { let func = `${item[0]}_Action` let args = item.slice(1,) console.log('Running Action', func, args) return this[func].apply(this, args) } forward_Action(distance) { let item = this.points.last() item.relative.forward(item.radius * 2 * distance) } down_Action() { /* pen down actuate drawing on step.*/ let item = this.points.last() this.downAt = item.copy() this._isDrawing = true } up_Action() { /* stash line */ let item = this.points.last() this.lines.push([this.downAt, item.copy()]) this._isDrawing = false } rotate_Action(v) { let item = this.points.last() item.rotation += v } goto_Action() {} } stage = MainStage.go(/*{ loop: true }*/)
Run
Meta Data
title Turtle Graphics Drawing
imports ()
files ('head', 'point', 'pointlist', 'stage', 'mouse', 'dragging', 'stroke', '../point_src/constrain-distance.js', '../point_src/functions/range.js', '../point_src/split.js', '../point_src/relative-xy.js', '../point_src/relative.js')
unused_keys ()
unknown_keys ('categories',)
categories ['basic', 'dragging']
filepath_exists True
path turtle-basic.js
filepath turtle-basic.js
clean_files ('../point_src/core/head.js', '../point_src/pointpen.js', '../point_src/compass.js', '../point_src/center.js', '../point_src/point-content.js', '../point_src/pointdraw.js', '../point_src/relative-xy.js', '../point_src/pointcast.js', '../point_src/point.js', '../point_src/pointlistdraw.js', '../point_src/pointlistgradient.js', '../point_src/pointlistshape.js', '../point_src/pointlistgenerator.js', '../point_src/unpack.js', '../point_src/pointlist.js', '../point_src/pointlistpen.js', '../point_src/stage-hooks.js', '../point_src/stage-resize.js', '../point_src/functions/resolve.js', '../point_src/stage.js', '../point_src/events.js', '../point_src/automouse.js', '../point_src/functions/clamp.js', '../point_src/distances.js', '../point_src/protractor.js', '../point_src/text/beta.js', '../point_src/dragging.js', '../point_src/setunset.js', '../point_src/stroke.js', '../point_src/constrain-distance.js', '../point_src/functions/range.js', '../point_src/split.js', '../point_src/relative.js')
markdown {'html': '<blockquote>\n<p>A most basic version of a "turtle".</p>\n</blockquote>\n<p>This very simple example of turtle drawing we move a <em>pen</em> using coordindate\nbased plotting with a <em>down</em> and <em>up</em>.</p>\n<ol>\n<li>Drag the point</li>\n<li>Run the <code>walk()</code> function</li>\n</ol>\n<p>It processes the <code>steps()</code> forever, plotting <em>lines</em> for each <em>down</em> and <em>up</em> command</p>', 'content': 'title: Turtle Graphics Drawing\ncategories: basic\n dragging\nfiles:\n head\n point\n pointlist\n stage\n mouse\n dragging\n stroke\n ../point_src/constrain-distance.js\n ../point_src/functions/range.js\n ../point_src/split.js\n ../point_src/relative-xy.js\n ../point_src/relative.js\n---\n\n> A most basic version of a "turtle".\n\nThis very simple example of turtle drawing we move a _pen_ using coordindate\nbased plotting with a _down_ and _up_.\n\n1. Drag the point\n2. Run the `walk()` function\n\nIt processes the `steps()` forever, plotting _lines_ for each _down_ and _up_ command'}