Filling a canvas with a pattern

Does anyone have a cross-platform method for filling a canvas with a pattern or gradient? Thom performs this magic with wild abandon in the Xojo IDE, but I have never seen an example of how it is accomplished. Thom, care to copy and paste your graphics method for us all to pirate? Your karma would go WAY up.

Cheers.

-bill k

Put the following in a module. Simply draw the resulting gradient in the paint event of a canvas.

[code] Dim p as Picture
p = newpicture(w,h,32)

Dim i As Integer
Dim samt,eamt As Double

For i = 0 To h
samt = 1 - (i / h)
eamt = i / h
p.graphics.forecolor = rgb((s.red * samt) + (e.red * eamt),(s.green *samt) + (e.green * eamt),(s.blue * samt) + (e.blue * eamt))
p.graphics.drawline -1,i,w+1,i
Next

Return p[/code]

For example in the paint event of a canvas:

g.DrawPicture(p,0,0,g.width,g.height,0,0,p.width.p.height)

Sorry meant to say your arguments for the above method should be

CreateGradient(w as integer,h as integer, s as color, e as color) as Picture

w = width
h = height
s = start color of the gradient
e = end color of the gradient.

May I suggest if you want to fill an entire canvas I would make your gradient just 1 pixel wide x height of canvas and then scale it in the paint event. Gradient is generated quicker and reduces overhead.

for pattern fill… create a “tile” of a repeating pattern and just use drawpicture to fill your canvas

I use a similar method in PaintDS to fill irregular shapes with patterns… but that involves masking and a few other tricks :slight_smile:

I get the impression that the one pixel wide gradient image is what Thom is doing in the IDE (based on the contents of the Resources folder in the application), but what about patterns? I assume that I would have to use a nested for::next loop and draw the picture across the entire canvas? Sounds slow.

I would assume it is pretty quick. The paint event is rapid. I have written a sourceview control which uses a canvas an I paint all by gradient backdrop, headings, sourcelistitems, gradient selection and icons in the paint event and it is smooth. I wouldn’t like to try it on Windows though.