GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing inventwithpython.com — JavaScript Turtle Graphics Press F1 or click here to show/hide this help Welcome to JavaScript Turtle Graphics!Type your commands at the prompt and see them faithfully executed by our eponymous turtle. Press F1 to toggle between the prompt and this help screen. What's turtle graphics? From Wikipedia: "Turtle graphics is a term in computer graphics for a method of programming vector graphics using a relative cursor (the "turtle") upon a Cartesian plane. Turtle graphics is a key feature of the Logo programming language."
Here, you'll be programming in JavaScript instead of the Logo language, but you still get to watch a triangle race around the screen!
The interactive prompt below exposes the valiable
The above methods all return
...and also some randomness functions for convenience.
Depth notes IJavaScript provides a number of ways to do loops, and you're certainly free to use them here. Alternatively, the turtle gives you a couple of its own ways to do things over and over again. This allows you to do loops the "turtle" way, which isn't necessarily better, just fitting with the rest of the API.
Depth notes II
Colors can take any form accepted by CSS or canvas.
This includes Depth notes IIISome drawings finish quickly, while others take a long time. This is because the drawing is rendered sequentially, piece by piece, and sometimes there are lots of pieces. To speed things up, when the number of pieces gets quite long, more and more of them are batched together between renders. When the drawing approaches the end, this batching decreases and then stops. You'll see the turtle initially tear through the drawing at a high rate of speed, and then slow to a more leisurely pace at the end. Your machine is actually working harder at the beginning of the drawing than at the end. Depth notes IVWho am I? My name is Greg Reimer. I build web applications for fun and profit. Find me on github and LinkedIn. This turtle graphics project is also on github under the MIT license. If you write an awesome sample program that you think should be included here, send me a pull request! Sample CodeSunburst run this examplet.reset()
.color('rgba(255,255,255,0.15)')
.repeat(720, function(i){
if(i%72 < 36){
t.fd(2000).bk(2000);
}
t.rt(.5);
})
Interesting moiré run this examplet.reset()
.repeat(1000, function(i){
t.fd(i).rt(90.1)
})
Phi rays run this examplevar phi = (Math.sqrt(5) + 1)/2;
t.reset().color('rgba(255,255,255,0.2)')
.repeat(987, function(){
t.fd(2000).pu().bk(2000).pd().rt(360*phi);
});
Random paths run this examplet.reset()
.color('rgba(255,255,255,0.1)')
.repeat(200000, function(){
t.fd(3).rt(t.rand.uni(-10,10))
if (t.get.oob()) t.home().rt(t.rand.uni(360))
})
Fun with spirograph run this examplet.reset()
.repeat(10, function(){
t.repeat(36, function(i){
t.color('hsla('+(i*10)+',50%,50%,0.3)')
.repeat(36, function(){
t.fd(27).rt(10)
}).rt(10);
}).rt(1);
});
More fun with Phi run this examplevar phi = (Math.sqrt(5) + 1)/2;
t.reset().repeat(1000, function(i){
t.pu().rt(360*phi).fd(Math.log(i)*40).pd();
t.repeat(50, function(j){
t.color('hsla('+((360 * (i/1000))-120)+',50%,50%,'+ Math.pow(1.1,-j) +')').fd(2.5);
});
t.pu().xy(0,0).pd();
});
Cool thingy run this examplevar phi = (Math.sqrt(5) + 1)/2;
t.reset().repeat(3000, function(i){
t.pu().rt(360*phi).fd(Math.log(i)*40).pd();
t.repeat(25, function(j){
t.color('hsla('+((360 * (i/3000))-120)+',50%,50%,'+ Math.pow(1.2,-j) +')').fd(5).lt(2);
});
t.pu().xy(0,0).pd();
});
Circles run this examplevar circle = (function(){
function circ(size, steps, level) {
t.repeat(2, function(){
if (level > 0) { circ(size/2, steps, level-1); }
t.repeat(steps, function(){ t.fd((size/steps)*2).rt(180/steps); });
});
}
return function(size, steps, level){
var r = (size/Math.PI)*2;
t.pu().lt(90).fd(r).rt(90).pd().rt((180/steps)/2);
circ(size, steps, level);
t.lt((180/steps)/2).pu().rt(90).fd(r).lt(90).pd();
}
})();
t.reset().color('rgba(255,255,255,0.5)');
circle(500, 20, 8);
Sierpinski triangle 1 run this examplefunction triangle(len, level){
if (level < 1) {
t.repeat(3, function(){
t.fd(len).rt(120);
});
} else {
t.repeat(3, function(){
t.pd();
triangle(len/2, level-1);
t.pu().fd(len).rt(120);
})
}
}
t.reset().xy(-300, -200).rt(30).clean();
triangle(600, 7);
Sierpinski triangle 2 run this examplevar triangle = (function(){
function frag(len, depth, left){
if (left === undefined) left = !(depth % 2);
if (depth > 1) {
frag(len/2, depth-1, !left);
t[left?'lt':'rt'](60);
frag(len/2, depth-1, left);
t[left?'lt':'rt'](60);
frag(len/2, depth-1, !left);
} else {
t.fd(len);
}
}
return frag;
})();
t.reset().pu().bk(250).rt(90).bk(350).pd().color('hsl(30, 100%, 70%)');
triangle(700, 8);
Koch snowflake run this examplevar koch = (function(){
function frag(len, depth){
if (depth > 1) {
frag(len/3, depth-1); t.rt(60);
frag(len/3, depth-1); t.lt(120);
frag(len/3, depth-1); t.rt(60);
frag(len/3, depth-1);
} else {
t.fd(len);
}
}
return function(len, depth){
frag(len,depth); t.lt(120);
frag(len,depth); t.lt(120);
frag(len,depth); t.lt(120);
}
})();
t.reset().bk(300).clean().rt(30).color('#0f0');
koch(550, 6);
Koch snowflake with subtriangles randomly oriented run this examplevar kochRandom = (function(){
function frag(len, depth){
if (depth > 1) {
var r = t.rand.chance(1/2);
frag(len/3, depth-1); t[r?'rt':'lt'](60);
frag(len/3, depth-1); t[r?'lt':'rt'](120);
frag(len/3, depth-1); t[r?'rt':'lt'](60);
frag(len/3, depth-1);
} else {
t.fd(len);
}
}
return function(len, depth){
frag(len,depth); t.lt(120);
frag(len,depth); t.lt(120);
frag(len,depth); t.lt(120);
}
})();
t.reset().bk(300).clean().rt(30).color('#90f');
kochRandom(550, 6);
Lots of randomized Koch snowflakes overlaid run this examplevar kochRandom = (function(){
function frag(len, depth){
if (depth > 1) {
var r = t.rand.chance(1/2);
frag(len/3, depth-1); t[r?'rt':'lt'](60);
frag(len/3, depth-1); t[r?'lt':'rt'](120);
frag(len/3, depth-1); t[r?'rt':'lt'](60);
frag(len/3, depth-1);
} else {
t.fd(len);
}
}
return function(len, depth){
frag(len,depth); t.lt(120);
frag(len,depth); t.lt(120);
frag(len,depth); t.lt(120);
}
})();
t.reset().bk(300).clean().rt(30);
t.repeat(60, function(i){
t.color('hsla('+i+',50%,50%,0.1)');
kochRandom(550, 6);
});
Tree run this examplefunction tree(length, branches, depth, origDepth){
if (origDepth === undefined) origDepth = depth;
var l = t.rand.uni(length*.95,length*1.05);
t.color('rgba(255,255,255,'+(depth+1)/origDepth+')').fd(l);
var angle = 180/branches;
if (depth > 0) {
t.lt(90);
for (var i=0; i<branches; i++){
t.rt((angle/2));
var angleJitter = t.rand.uni(-5,5);
t.rt(angleJitter);
tree(length*(3/4), branches, depth-1, origDepth);
t.lt(angleJitter);
t.rt(angle/2);
}
t.lt(90);
}
t.pu().bk(l).pd();
}
t.reset().bk(280).clean();
tree(180,4,6);
Heighway dragon run this examplefunction dragon(length, iters){
var d = [true];
for (var i=0; i<iters; i++) {
var len = d.length;
d.push(true);
for (var j=len-1; j>=0; j--) {
d.push(!d[j]);
}
length = Math.sqrt((length * length) / 2);
}
var c = 0;
for (var i=0; i<d.length; i++) {
c += .01;
t.color('hsl('+c+',100%,70%)').fd(length)[d[i]?'rt':'lt'](90);
}
}
t.reset().xy(250,170).clean().lt(95);
dragon(470,15);
Another sample run this examplet.reset()
.color('rgba(255,255,255,0.1)')
.repeat(360, function(i){
t.pu().home().pd()
.repeat(80, function(){
t.fd(20).rt(t.get.y()+i)
})
})
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |