For Loops in Luxor

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

@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:

@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?

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.

3 Likes

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?

Basic drawings

Macro shortcuts

There are occasional posts about global scope on Discourse.

I’d draw a chessboard like this:

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
1 Like

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

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()

You can put everything on a single drawing.

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"
2 Likes

still didn’t work,

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

Is this not what you want?

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

I figured it out I forgot about the @svg

@cormullion You car is so cute that I couldn’t resist to pirate it in GMT.jl
Made it a bit simpler but looks the same. See here

2 Likes

That’s the Brett Knoss Speedster… :grinning:

1 Like

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.

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

no mouse, sorry :joy:

Scotty-Talks-To-Mouse

1 Like

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

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

When someone writes a ScratchCall.jl to interface with Scratch it should be feasible.

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.