# For Loops in Luxor

**URL:** <https://discourse.julialang.org/t/for-loops-in-luxor/54197>\
**Category:** Visualization\
**Tags:** question, package, loops, luxor\
**Created:** [January 29, 2021, 5:10pm UTC](https://discourse.julialang.org/t/for-loops-in-luxor/54197 "2021-01-29T17:10:18Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [January 29, 2021, 5:10pm UTC](https://discourse.julialang.org/t/for-loops-in-luxor/54197/1 "2021-01-29T17:10:18Z")

</div>

When I create a checker pattern using the rect function, it works perfectly:

```julia
@svg begin
	Drawing(2000,2000)
	background("white")
	sethue("black")
	rect(500,0,500,500,:fill)
	rect(1500,0,500,500,:fill)
	rect(0,500,500,500,:fill)
	rect(1000,500,500,500,:fill)
	rect(500,1000,500,500,:fill)
	rect(1500,1000,500,500,:fill)
	rect(0,1500,500,500,:fill)
	rect(1000,1500,500,500,:fill)
end

```

but when I try to do the same thing using a for loop, it doesn’t draw:

```julia
@svg begin
	Drawing(2000,2000)
	sethue("black")
	let
		x=500
		y=0
		map(0:4) do i
    		map(0:2) do i
        		rect(x,y,500,500)       
        		x=x+1000  
			end
    	y=y+50                   
    	x=abs(x-2500) 
		end
	end
end

```

Why is that?

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [January 29, 2021, 5:20pm UTC](https://discourse.julialang.org/t/for-loops-in-luxor/54197/2 "2021-01-29T17:20:10Z")

</div>

Firstly, `@svg` is a shortcut for `Drawing()` so you only need one or the other.

Secondly, you’re drawing stuff with units of 1000s and 500s. The default drawing size is 600x600, so you probably can’t see much of it…

Thirdly, working in global scope may have unforeseen consequences, as elsewhere in Julia.

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [January 29, 2021, 5:25pm UTC](https://discourse.julialang.org/t/for-loops-in-luxor/54197/3 "2021-01-29T17:25:14Z")

</div>

I thought that Drawing() set the canvas size, how would I change it form default (also notice that the top MWE works and is the same size canvas and squares).

I’m not familiar with global scope however, could you go into more on this?

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [January 29, 2021, 5:36pm UTC](https://discourse.julialang.org/t/for-loops-in-luxor/54197/4 "2021-01-29T17:36:57Z")

</div>

[Basic drawings](http://juliagraphics.github.io/Luxor.jl/stable/basics/#Drawings)

[Macro shortcuts](http://juliagraphics.github.io/Luxor.jl/stable/basics/#Quick-drawings-with-macros)

There are [occasional posts](https://discourse.julialang.org/search?q=%22global%20scope%22) about global scope on Discourse.

I’d draw a chessboard like this:

```julia
using Luxor

let 
	@svg begin
		tiling = Tiler(600, 600, 8, 8)
		sethue("black")
		for (pos, n) in tiling 
			if isodd(tiling.currentrow + tiling.currentcol) 
				box(pos, tiling.tilewidth, tiling.tileheight, :fillstroke)
			else
				box(pos, tiling.tilewidth, tiling.tileheight, :stroke)
			end
		end
	end 600 600 "/tmp/chessboard.svg"
end

```

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [January 29, 2021, 5:44pm UTC](https://discourse.julialang.org/t/for-loops-in-luxor/54197/5 "2021-01-29T17:44:08Z")

</div>

It works I get the if then statement. What if I want to combine components as functions?

```julia
function build_body()
	@svg begin
	
		sethue("Purple")
		ellipse(2,1,200,50,:fill)
		sethue("Black")
		ellipse(1,1,200,50,:stroke)
		sethue("Purple")
		ellipse(40,-6,130,66,:fill)
		sethue("black")
		ellipse(40,-6,130,66,:stroke)
		sethue("blue")
		ellipse(18,-23,60,26,:fill)
		sethue("purple")
		rect(-100,1,205,26,:fill)
		sethue("black")
		rect(-99,1,205,26,:stroke)
	end
end

function build_wheel(x,y)
	@svg begin
		sethue("black")
		circle(x,y,28,:fill)
		sethue("grey")
		circle(x,y,18,:fill)
	end
end

function build_car()
	build_body()
	build_wheel(5,15)
	build_wheel(100,15)
end

build_car()

```

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [January 29, 2021, 5:46pm UTC](https://discourse.julialang.org/t/for-loops-in-luxor/54197/6 "2021-01-29T17:46:42Z")

</div>

You can put everything on a single drawing.

```julia
using Luxor

function build_body()
		sethue("Purple")
		ellipse(2,1,200,50,:fill)
		sethue("Black")
		ellipse(1,1,200,50,:stroke)
		sethue("Purple")
		ellipse(40,-6,130,66,:fill)
		sethue("black")
		ellipse(40,-6,130,66,:stroke)
		sethue("blue")
		ellipse(18,-23,60,26,:fill)
		sethue("purple")
		rect(-100,1,205,26,:fill)
		sethue("black")
		rect(-99,1,205,26,:stroke)
end

function build_wheel(x,y)
		sethue("black")
		circle(x,y,28,:fill)
		sethue("grey")
		circle(x,y,18,:fill)
end

function build_car()
	build_body()
	build_wheel(5,15)
	build_wheel(100,15)
end

@svg begin
	build_car()
end 500 500 "/tmp/car.svg"

```

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [January 29, 2021, 5:52pm UTC](https://discourse.julialang.org/t/for-loops-in-luxor/54197/7 "2021-01-29T17:52:07Z")

</div>

still didn’t work,

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [January 29, 2021, 5:52pm UTC](https://discourse.julialang.org/t/for-loops-in-luxor/54197/8 "2021-01-29T17:52:51Z")

</div>

shows the last build\_wheel program. Do I need to do something so that the background isn’t part of each layer?

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [January 29, 2021, 5:54pm UTC](https://discourse.julialang.org/t/for-loops-in-luxor/54197/9 "2021-01-29T17:54:03Z")

</div>

Is this not what you want?

 ![Screenshot 2021-01-29 at 17.53.19](https://global.discourse-cdn.com/julialang/original/3X/f/e/fedee3d4fbe56a4511396d03d3f86dfc71fe8809.png)

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [January 29, 2021, 6:42pm UTC](https://discourse.julialang.org/t/for-loops-in-luxor/54197/10 "2021-01-29T18:42:16Z")

</div>

> [@cormullion](#):
>
> ```julia
> @svg begin
> build_car()
> end 500 500 "/tmp/car.svg"
> 
> ```

Ok, I tried it in VS Code, and it worked, but not in Pluto for some reason

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [January 29, 2021, 6:45pm UTC](https://discourse.julialang.org/t/for-loops-in-luxor/54197/11 "2021-01-29T18:45:35Z")

</div>

![Screenshot 2021-01-29 at 18.45.15](https://global.discourse-cdn.com/julialang/original/3X/5/5/55dc1ccd5f06f9ed074ef5e56118b9a5a7b92915.png)

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [January 29, 2021, 6:54pm UTC](https://discourse.julialang.org/t/for-loops-in-luxor/54197/12 "2021-01-29T18:54:00Z")

</div>

> [@cormullion](#):
>
> ```julia
> function build_wheel(x,y)
> sethue("black")
> circle(x,y,28,:fill)
> sethue("grey")
> circle(x,y,18,:fill)
> end
> 
> ```

I figured it out I forgot about the @svg

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [February 3, 2021, 7:51pm UTC](https://discourse.julialang.org/t/for-loops-in-luxor/54197/13 "2021-02-03T19:51:25Z")

</div>

@cormullion You car is so cute that I couldn’t resist to pirate it in [GMT.jl](https://github.com/GenericMappingTools/GMT.jl)  
Made it a bit simpler but looks the same. See [here](https://forum.generic-mapping-tools.org/t/matchbox-toy-cars/1295)

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [February 3, 2021, 8:29pm UTC](https://discourse.julialang.org/t/for-loops-in-luxor/54197/14 "2021-02-03T20:29:59Z")

</div>

That’s the Brett Knoss Speedster… 😀

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [February 3, 2021, 10:54pm UTC](https://discourse.julialang.org/t/for-loops-in-luxor/54197/15 "2021-02-03T22:54:49Z")

</div>

sure, I did intend to move the front wheels further forward. I’m not sure if luxor.jl allows creating an object that follows the cursor, but I thought I saw something like that in the GMT.jl documentatation.

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [February 3, 2021, 11:09pm UTC](https://discourse.julialang.org/t/for-loops-in-luxor/54197/16 "2021-02-03T23:09:51Z")

</div>

No, GMT is purely command driven too. No GUI. But should cost nothing to try other wheels positions. It’s just a question of changing the _xx_

---

<div class="post-metadata">

**Author:** ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)\
**Post date:** [February 3, 2021, 11:14pm UTC](https://discourse.julialang.org/t/for-loops-in-luxor/54197/17 "2021-02-03T23:14:51Z")

</div>

no mouse, sorry 😂

![Scotty-Talks-To-Mouse](https://global.discourse-cdn.com/julialang/original/3X/3/2/32b574be6a4e4f4ce658a47177de81981c315633.jpeg)

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [February 4, 2021, 12:07am UTC](https://discourse.julialang.org/t/for-loops-in-luxor/54197/18 "2021-02-04T00:07:48Z")

</div>

Lol, I didn’t mean to create content, I meant a final image, I meand the user something like.

```julia
ellipse(mouseX,mouseY,15,15) #python development code for a circle that follows the mouse.

```

---

<div class="post-metadata">

**Author:** ![joa-quim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/joa-quim/32/227_2.png) [@joa-quim](https://discourse.julialang.org/u/joa-quim)\
**Post date:** [February 4, 2021, 12:32am UTC](https://discourse.julialang.org/t/for-loops-in-luxor/54197/19 "2021-02-04T00:32:27Z")

</div>

When someone writes a ScratchCall.jl to interface with [Scratch](https://scratch.mit.edu/) it should be feasible.

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [February 4, 2021, 2:08am UTC](https://discourse.julialang.org/t/for-loops-in-luxor/54197/20 "2021-02-04T02:08:48Z")

</div>

Interesting, I was working on Visualisation and interactivity for Pluto, so if I could do this in HTML with the bind command, or in JavaScript, that should work too.

[Next page](https://discourse.julialang.org/t/for-loops-in-luxor/54197.md?page=2)
