What's wrong with this use of setContext?

I’ve been trying to use setContext to draw some lines on to a dial, but its not working only the text shows around it

    self.txt = image(size,size)
    self.t = mesh()
    self.tr = self.t:addRect(pos.x,pos.y,size+35,size+35)
    for i=0,20 do
        local mth = ((math.pi*2)/20-((math.pi/20)/(20/2)))*i
        local p1 = vec2(pos.x+math.sin(mth)*size/2,
            pos.y+math.cos(mth)*size/2)
        local p2 = vec2(pos.x+math.sin(mth)*(size/2-4),
            pos.y+math.cos(mth)*(size/2-4))
        local p3 = vec2(math.sin(mth)*(self.size/2-4),
        math.cos(mth)*(self.size/2-4))
        setContext(self.txt)
            pushStyle()
                fontSize(5)
                strokeWidth(2)
                stroke(220,255)
                line(p1.x,p1.y,p2.x,p2.y)
                fill(255,255)
                text(self.sval+((self.eval-self.sval)/20)*i,p3.x+size/2,p3.y+size/2)
            popStyle()
        setContext()        
    end
    self.t.texture = self.txt

Any ideas?

Could be a bug, try Simeon’s advice here

http://twolivesleft.com/Codea/Talk/discussion/3020/odd-behaviour-with-cameras-and-sprites-in-1-5-3#Item_4

That’s not the issue… The line function isn’t drawing lines when I use setContext but it does it with text…

Set context doesn’t work with line() i don’t think

@Luatee nothing wrong with it in this simple example

-- testingline

-- Use this function to perform your initial setup
function setup()

bgimg=image(WIDTH,HEIGHT)
setContext(bgimg)
background(0, 0, 0, 255)
strokeWidth(2)
line(100,100,400,500)
text("hello",500,500)
setContext()
end

-- This function gets called once every frame
function draw()

    sprite(bgimg,WIDTH/2,HEIGHT/2)    
end


Beats me then… Ill keep looking

I had some trouble with setContext too, a few days ago, but then I couldn’t repeat it. If I can, I’ll let you know.

@Luatee - I get the feeling that something is going wrong with adding pos.x and pos.y to p1 and p2. I think pos.x and pos.y are referring to the global mesh coords with p1 and p2 being local inside the (smaller) self.text image. Try removing the references to pos.x and pos.y from your vec2 definitions and updating


line(p1.x,p1.y,p2.x,p2.y)

with


line(p1.x+size/2,p1.y+size/2,p2.x+size/2,p2.y+size/2)

following the format used to display the text

Don’t have my ipad to hand, but might give you a prompt in the right direction

@luatee: @West’s analysis is absolutely correct. Modulo a few details I had to provide (it’s always easier if you provide code that can be cut and pasted directly), the following works for me:

function setup()
    size = 100
    pos = vec2(WIDTH/2,HEIGHT/2)
    self = {}
    self.size = size
    self.sval = 3
    self.eval = 5
    self.txt = image(size,size)
    self.t = mesh()
    self.tr = self.t:addRect(pos.x,pos.y,size+35,size+35)
    for i=0,20 do
        local mth = ((math.pi*2)/20-((math.pi/20)/(20/2)))*i
        local p1 = size*vec2(.5,.5) + vec2(math.sin(mth)*size/2,
            math.cos(mth)*size/2)
        local p2 = size*vec2(.5,.5) + vec2(math.sin(mth)*(size/2-4),
            math.cos(mth)*(size/2-4))
        local p3 = vec2(math.sin(mth)*(self.size/2-4),
        math.cos(mth)*(self.size/2-4))
        setContext(self.txt)
            pushStyle()
                fontSize(5)
                strokeWidth(2)
                stroke(220,255)
                line(p1.x,p1.y,p2.x,p2.y)
                fill(255,255)
                text(self.sval+((self.eval-self.sval)/20)*i,p3.x+size/2,p3.y+size/2)
            popStyle()
        setContext()        
    end
    self.t.texture = self.txt
end

function draw()
    background(44, 25, 25, 255)
    self.t:draw()
end

Youre absolutely right is was the adding of the positions I only just realised after I went back to look and that was the case, thanks a lot guys helped through my hours of brain bashing